Integrated different event date formattings based on locales

This commit is contained in:
Benjamin Barth
2016-03-29 13:30:50 +02:00
parent 3d06b11413
commit f2d684bc2b
2 changed files with 15 additions and 42 deletions
@@ -45,5 +45,13 @@
<inherits name="com.sap.sailing.gwt.ui.SourceLeaderboard"/>
<inherits name='com.sap.sse.gwt.CommonControlCSS'/>
<replace-with class="com.sap.sailing.gwt.home.shared.utils.dateformatter.CustomEventDatesFormatter">
<when-type-is class="com.sap.sailing.gwt.home.shared.utils.dateformatter.DefaultEventDatesFormatter"/>
<any>
<when-property-is name="locale" value="en"/>
<when-property-is name="locale" value="de"/>
</any>
</replace-with>
<entry-point class="com.sap.sailing.gwt.home.shared.SwitchingEntryPoint" />
</module>
@@ -2,7 +2,8 @@ package com.sap.sailing.gwt.home.shared.utils;
import java.util.Date;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.core.client.GWT;
import com.sap.sailing.gwt.home.shared.utils.dateformatter.DefaultEventDatesFormatter;
/**
* Formatter for event dates and date ranges
@@ -10,50 +11,14 @@ import com.google.gwt.i18n.client.DateTimeFormat;
*
*/
public class EventDatesFormatterUtil {
private final static DateTimeFormat dayFormat = DateTimeFormat.getFormat("dd");
private final static DateTimeFormat dayAndMonthFormat = DateTimeFormat.getFormat("MMMM dd");
private final static DateTimeFormat monthFormat = DateTimeFormat.getFormat("MMMM");
private final static DateTimeFormat yearFormat = DateTimeFormat.getFormat("yyyy");
private final static DefaultEventDatesFormatter FORMATTER = GWT.create(DefaultEventDatesFormatter.class);
public static String formatDateRangeWithYear(Date from, Date to) {
final String result;
final String fromMonth = monthFormat.format(from);
final String toMonth = monthFormat.format(to);
if (fromMonth.equals(toMonth)) {
// same month
final String fromDay = dayFormat.format(from);
final String toDay = dayFormat.format(to);
if (fromDay.equals(toDay)) {
// same day
result = fromMonth + " " + fromDay + ", " + yearFormat.format(from);
} else {
result = fromMonth + " " + fromDay + " - " + toDay + ", "
+ yearFormat.format(from);
}
} else {
result = dayAndMonthFormat.format(from) + " - " + dayAndMonthFormat.format(to) + ", "
+ yearFormat.format(from);
}
return result;
return FORMATTER.formatDateRangeWithYear(from, to);
}
public static String formatDateRangeWithoutYear(Date from, Date to) {
final String result;
final String fromMonth = monthFormat.format(from);
final String toMonth = monthFormat.format(to);
if (fromMonth.equals(toMonth)) {
// same month
final String fromDay = dayFormat.format(from);
final String toDay = dayFormat.format(to);
if (fromDay.equals(toDay)) {
// same day
result = fromMonth + " " + fromDay;
} else {
result = fromMonth + " " + fromDay + " - " + toDay;
}
} else {
result = dayAndMonthFormat.format(from) + " - " + dayAndMonthFormat.format(to);
}
return result;
return FORMATTER.formatDateRangeWithoutYear(from, to);
}
}