DateUtility: support conversion from/to LocalDate and LocalDateTime
292315
Change-Id: I0006d944286e37126aba5bff2041bd4a0a9c3dda
Reviewed-on: https://git.eclipse.org/r/c/scout/org.eclipse.scout.rt/+/181130
Tested-by: Scout Bot <scout-bot@eclipse.org>
Reviewed-by: Beat Schwarzentrub <bsh@bsiag.com>
(cherry picked from commit 28217576faa72a6a6a902599ceccf68f301b82ae)
Reviewed-on: https://git.eclipse.org/r/c/scout/org.eclipse.scout.rt/+/180986
Tested-by: Timon Gygax <timon.gygax@bsi-software.com>
Reviewed-by: Timon Gygax <timon.gygax@bsi-software.com>
diff --git a/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/platform/util/date/DateUtilityTest.java b/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/platform/util/date/DateUtilityTest.java
index e2915ae..bcde6a2 100644
--- a/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/platform/util/date/DateUtilityTest.java
+++ b/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/platform/util/date/DateUtilityTest.java
@@ -13,6 +13,9 @@
import static org.junit.Assert.*;
import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.Month;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
@@ -979,6 +982,27 @@
assertDateEquals("2015-03-29 02:00:00.000", dateOf("2015-03-29 02:00:00.000")); // fails with exception
}
+ @Test
+ public void testToLocalDate() {
+ Date date = dateOf("2021-05-28 00:00:00.000");
+ assertEquals(LocalDate.of(2021, Month.MAY, 28), DateUtility.toLocalDate(date));
+ }
+
+ @Test
+ public void testToLocalDateTime() {
+ Date date = dateOf("2021-05-28 10:58:25.000");
+ assertEquals(LocalDateTime.of(2021, Month.MAY, 28, 10, 58, 25), DateUtility.toLocalDateTime(date));
+ }
+
+ @Test
+ public void testToDate() {
+ LocalDate localDate = LocalDate.of(2021, Month.MAY, 28);
+ assertEquals(dateOf("2021-05-28 00:00:00.000"), DateUtility.toUtilDate(localDate));
+
+ LocalDateTime localDateTime = LocalDateTime.of(2021, Month.MAY, 28, 10, 58, 25);
+ assertEquals(dateOf("2021-05-28 10:58:25.000"), DateUtility.toUtilDate(localDateTime));
+ }
+
public static void assertDateEquals(String expectedDate, Date date) {
assertDateEquals(null, expectedDate, date);
}
diff --git a/org.eclipse.scout.rt.platform/src/main/java/org/eclipse/scout/rt/platform/util/date/DateUtility.java b/org.eclipse.scout.rt.platform/src/main/java/org/eclipse/scout/rt/platform/util/date/DateUtility.java
index b09845e..3558c5c 100644
--- a/org.eclipse.scout.rt.platform/src/main/java/org/eclipse/scout/rt/platform/util/date/DateUtility.java
+++ b/org.eclipse.scout.rt.platform/src/main/java/org/eclipse/scout/rt/platform/util/date/DateUtility.java
@@ -14,6 +14,9 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
@@ -779,6 +782,55 @@
}
/**
+ * Converts a {@link Date} to a {@link LocalDate} using the default time-zone of the system.
+ */
+ public static LocalDate toLocalDate(Date d) {
+ if (d == null) {
+ return null;
+ }
+ return d.toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDate();
+ }
+
+ /**
+ * Converts a {@link Date} to a {@link LocalDateTime} using the default time-zone of the system.
+ */
+ public static LocalDateTime toLocalDateTime(Date d) {
+ if (d == null) {
+ return null;
+ }
+ return d.toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDateTime();
+ }
+
+ /**
+ * Converts a {@link LocalDate} to a {@link Date} using the default time-zone of the system. The time will be set to
+ * midnight.
+ */
+ public static Date toUtilDate(LocalDate localDate) {
+ if (localDate == null) {
+ return null;
+ }
+ return Date.from(localDate.atStartOfDay()
+ .atZone(ZoneId.systemDefault())
+ .toInstant());
+ }
+
+ /**
+ * Converts a {@link LocalDateTime} to a {@link Date} using the default time-zone of the system.
+ */
+ public static Date toUtilDate(LocalDateTime localDateTime) {
+ if (localDateTime == null) {
+ return null;
+ }
+ return Date.from(localDateTime
+ .atZone(ZoneId.systemDefault())
+ .toInstant());
+ }
+
+ /**
* Calculates the number of days between <code>start</code> and <code>end</code>. If the end date is before the start
* date, the result will be positive as well. Example:
* <ul>