Skip to content

Commit f19011e

Browse files
committed
fixed issue with dates before 1912
1 parent 72e48cb commit f19011e

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

roda-ui/roda-wui/src/main/java/org/roda/wui/client/common/utils/FormUtilities.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.gwt.event.logical.shared.ValueChangeHandler;
2626
import com.google.gwt.i18n.client.DateTimeFormat;
2727
import com.google.gwt.i18n.client.LocaleInfo;
28+
import com.google.gwt.i18n.client.TimeZone;
2829
import com.google.gwt.json.client.JSONArray;
2930
import com.google.gwt.json.client.JSONException;
3031
import com.google.gwt.json.client.JSONObject;
@@ -443,6 +444,9 @@ private static void addDatePicker(FlowPanel panel, final FlowPanel layout, final
443444
final boolean mandatory, final Callable<Void> onChange) {
444445
// Top label
445446
final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
447+
final TimeZone utcZone = TimeZone.createTimeZone(0);
448+
final long HALF_DAY_MS = 43200000L; // 12 hours in milliseconds
449+
446450
Label mvLabel = new Label(getFieldLabel(mv));
447451
mvLabel.addStyleName("form-label");
448452
if (mandatory) {
@@ -459,7 +463,9 @@ private static void addDatePicker(FlowPanel panel, final FlowPanel layout, final
459463
public String format(DateBox dateBox, Date date) {
460464
if (date == null)
461465
return null;
462-
return dateTimeFormat.format(date);
466+
// Shift time to midday (+12h) to avoid timezone edge-cases when formatting to UTC
467+
Date safeDate = new Date(date.getTime() + HALF_DAY_MS);
468+
return dateTimeFormat.format(safeDate, utcZone);
463469
}
464470
});
465471

@@ -477,7 +483,12 @@ public String format(DateBox dateBox, Date date) {
477483
@Override
478484
public void onValueChange(ValueChangeEvent<Date> valueChangeEvent) {
479485
FormUtilities.callOnChange(onChange);
480-
String newValue = dateTimeFormat.format(mvDate.getValue());
486+
Date date = mvDate.getValue();
487+
String newValue = null;
488+
if (date != null) {
489+
Date safeDate = new Date(date.getTime() + HALF_DAY_MS);
490+
newValue = dateTimeFormat.format(safeDate, utcZone);
491+
}
481492
mv.set("value", newValue);
482493
if (mandatory && (newValue != null && !"".equals(newValue.trim()))) {
483494
mvDate.removeStyleName("isWrong");
@@ -496,7 +507,8 @@ public void onValueChange(ValueChangeEvent<String> event) {
496507
try {
497508
Date date = dateTimeFormat.parse(value.trim());
498509
mvDate.setValue(date);
499-
mv.set("value", value);
510+
Date safeDate = new Date(date.getTime() + HALF_DAY_MS);
511+
mv.set("value", dateTimeFormat.format(safeDate, utcZone));
500512
} catch (IllegalArgumentException iae) {
501513
if (event.getValue() == null || "".equals(event.getValue().trim())) {
502514
mv.set("value", null);

0 commit comments

Comments
 (0)