diff --git a/java/pom.xml b/java/pom.xml index cae85ab8a..9b5db223e 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -69,11 +69,6 @@ commons-io 2.11.0 - - joda-time - joda-time - 2.12.2 - commons-net commons-net diff --git a/java/src/main/java/com/genexus/specific/java/GXutil.java b/java/src/main/java/com/genexus/specific/java/GXutil.java index 7b3ea4fed..83a63dad1 100644 --- a/java/src/main/java/com/genexus/specific/java/GXutil.java +++ b/java/src/main/java/com/genexus/specific/java/GXutil.java @@ -4,7 +4,9 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; +import java.sql.Timestamp; import java.text.SimpleDateFormat; +import java.time.*; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -23,39 +25,53 @@ import com.genexus.util.CacheAPI; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.DateTimeZone; public class GXutil implements IExtensionGXutil { private static Logger logger = LogManager.getLogger(GXutil.class); - private static DateTimeZone Java2JodaTimeZone(TimeZone tz) { - DateTimeZone jodaTZ = DateTimeZone.getDefault(); + private ZonedDateTime getZonedDateTime(Date value, TimeZone tz){ + ZonedDateTime zdt; try { - jodaTZ = DateTimeZone.forID(tz.getID()); - } catch (IllegalArgumentException _) { - try { - jodaTZ = DateTimeZone.forTimeZone(tz); - } catch (IllegalArgumentException e) { - logger.error(String.format("Failed to find TimeZone: %s. Using default Timezone", tz.getID()), e); - } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(value); + int year = calendar.get(Calendar.YEAR); + int month = calendar.get(Calendar.MONTH) + 1; + int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + int minute = calendar.get(Calendar.MINUTE); + int second = calendar.get(Calendar.SECOND); + int nanoOfSecond = calendar.get(Calendar.MILLISECOND) * 1000000; + + zdt = ZonedDateTime.of(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond), tz.toZoneId()); } - return jodaTZ; + catch(Exception e) { + zdt = ZonedDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()); + logger.error(String.format("Failed to find TimeZone: %s. Using default Timezone", tz.getID()), e); + } + return zdt; } @Override public Date DateTimeToUTC(Date value, TimeZone tz) { - org.joda.time.DateTime ret = new org.joda.time.DateTime(value.getTime()); - ret = new org.joda.time.DateTime(Java2JodaTimeZone(tz).convertLocalToUTC(ret.getMillis(), false)); - return ret.toDate(); + if (tz.getID() == "GMT") + return value; + + ZonedDateTime zdt = getZonedDateTime(value, tz); + Timestamp t = Timestamp.valueOf(zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()); + return new Date(t.getTime()); } @Override public Date DateTimeFromUTC(Date value, TimeZone tz) { + if (tz.getID() == "GMT") + return value; + if (com.genexus.CommonUtil.emptyDate(value)) return value; - org.joda.time.DateTime ret = new org.joda.time.DateTime(value.getTime()); - ret = new org.joda.time.DateTime(Java2JodaTimeZone(tz).convertUTCToLocal(ret.getMillis())); - return ret.toDate(); + + ZonedDateTime zdtUTC = getZonedDateTime(value, TimeZone.getTimeZone("UTC")); + Timestamp t = Timestamp.valueOf(zdtUTC.withZoneSameInstant(ZoneId.of(tz.getID())).toLocalDateTime()); + return new Date(t.getTime()); } @Override diff --git a/java/src/test/java/com/genexus/util/TestDateMethods.java b/java/src/test/java/com/genexus/util/TestDateMethods.java index b43a480dc..3ce3b9150 100644 --- a/java/src/test/java/com/genexus/util/TestDateMethods.java +++ b/java/src/test/java/com/genexus/util/TestDateMethods.java @@ -83,25 +83,34 @@ public void testCtotex() { } @Test - public void testValidTimezone() { + public void testDateTimeToUTC() { Connect.init(); + TimeZone timezone = TimeZone.getTimeZone("America/Montevideo"); + String dateTime = "2023-03-22 15:00:00"; // input DateTime - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + long expectedDiff = 10800000; + ConvertDateTime(dateTime, timezone, expectedDiff, true); + + dateTime = "2011-02-20 1:00:00"; // input DateTime during summer time + expectedDiff = 7200000; + ConvertDateTime(dateTime, timezone, expectedDiff, true); + } + + @Test + public void DateTimeFromUTC() { + Connect.init(); TimeZone timezone = TimeZone.getTimeZone("America/Montevideo"); - try { - Date mvdDate = dateFormat.parse(dateTime); // convert String to Date - Date utcDate = CommonUtil.DateTimeToUTC(mvdDate, timezone); + String dateTime = "2023-02-22 18:00:00"; // input DateTime + long expectedDiff = -10800000; + ConvertDateTime(dateTime, timezone, expectedDiff, false); - long diff = utcDate.getTime() - mvdDate.getTime(); - long expectedDiff = 10800000; - Assert.assertEquals("Timezone offset invalid", expectedDiff, diff); - } catch (Exception e) { - Assert.fail(); - } - } + dateTime = "2011-02-20 3:00:00"; // input DateTime during summer time + expectedDiff = -7200000; + ConvertDateTime(dateTime, timezone, expectedDiff, false); + } /** * DateTimeToUTC must not fail if the Timezone does not exists. @@ -110,20 +119,29 @@ public void testValidTimezone() { public void testInvalidTimezone() { Connect.init(); - String dateTime = "2023-03-22 15:00:00"; // input DateTime - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + TimeZone timezone = TimeZone.getTimeZone("America/DoesnotExists"); - TimeZone timezone = TimeZone.getTimeZone("America/DoesnotExists"); + String dateTime = "2023-03-22 15:00:00"; // input DateTime + long expectedDiff = 0; + ConvertDateTime(dateTime, timezone, expectedDiff, true); + ConvertDateTime(dateTime, timezone, expectedDiff, false); + } - try { - Date mvdDate = dateFormat.parse(dateTime); // convert String to Date - Date utcDate = CommonUtil.DateTimeToUTC(mvdDate, timezone); + private void ConvertDateTime(String dateTime, TimeZone timezone, long expectedDiff, boolean toUTC) { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + Date mvdDate = dateFormat.parse(dateTime); // convert String to Date + Date dateConverted; + if (toUTC) + dateConverted = CommonUtil.DateTimeToUTC(mvdDate, timezone); + else + dateConverted = CommonUtil.DateTimeFromUTC(mvdDate, timezone); + + long diff = dateConverted.getTime() - mvdDate.getTime(); + Assert.assertEquals("Timezone offset invalid", expectedDiff, diff); + } catch (Exception e) { + Assert.fail(); + } + } - long diff = utcDate.getTime() - mvdDate.getTime(); - long expectedDiff = 0; - Assert.assertEquals("Timezone offset invalid", expectedDiff, diff); - } catch (Exception e) { - Assert.fail(); - } - } } \ No newline at end of file