diff --git a/src/main/java/com/cedarsoftware/util/DateUtilities.java b/src/main/java/com/cedarsoftware/util/DateUtilities.java index 313580a6b..a9870539e 100644 --- a/src/main/java/com/cedarsoftware/util/DateUtilities.java +++ b/src/main/java/com/cedarsoftware/util/DateUtilities.java @@ -241,7 +241,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool } // For the remaining String, match the time portion (which could have appeared ahead of the date portion) - String hour = null, min = null, sec = "00", milli = "0"; + String hour = null, min = null, sec = "00", fracSec = "0"; remains = remains.trim(); matcher = timePattern.matcher(remains); remnant = matcher.replaceFirst(""); @@ -254,7 +254,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool sec = matcher.group(3); } if (matcher.group(4) != null) { - milli = matcher.group(4).substring(1); + fracSec = "0." + matcher.group(4).substring(1); } if (matcher.group(5) != null) { tz = stripBrackets(matcher.group(5).trim()); @@ -266,7 +266,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool } ZoneId zoneId = StringUtilities.isEmpty(tz) ? defaultZoneId : getTimeZone(tz); - ZonedDateTime zonedDateTime = getDate(dateStr, zoneId, year, month, day, hour, min, sec, milli); + ZonedDateTime zonedDateTime = getDate(dateStr, zoneId, year, month, day, hour, min, sec, fracSec); return zonedDateTime; } @@ -278,7 +278,7 @@ private static ZonedDateTime getDate(String dateStr, String hour, String min, String sec, - String nanos) { + String fracSec) { // Build Calendar from date, time, and timezone components, and retrieve Date instance from Calendar. int y = Integer.parseInt(year); int d = Integer.parseInt(day); @@ -297,7 +297,7 @@ private static ZonedDateTime getDate(String dateStr, int h = Integer.parseInt(hour); int mn = Integer.parseInt(min); int s = Integer.parseInt(sec); - int ns = Integer.parseInt(nanos); + long nanoOfSec = convertFractionToNanos(fracSec); if (h > 23) { throw new IllegalArgumentException("Hour must be between 0 and 23 inclusive, time: " + dateStr); @@ -309,11 +309,16 @@ private static ZonedDateTime getDate(String dateStr, throw new IllegalArgumentException("Second must be between 0 and 59 inclusive, time: " + dateStr); } - ZonedDateTime zdt = ZonedDateTime.of(y, month, d, h, mn, s, ns, zoneId); + ZonedDateTime zdt = ZonedDateTime.of(y, month, d, h, mn, s, (int) nanoOfSec, zoneId); return zdt; } } + private static long convertFractionToNanos(String fracSec) { + double fractionalSecond = Double.parseDouble(fracSec); + return (long) (fractionalSecond * 1_000_000_000); + } + private static ZoneId getTimeZone(String tz) { if (tz != null) { if (tz.startsWith("-") || tz.startsWith("+")) {