Skip to content

Commit

Permalink
refector work on DateUtilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Jan 21, 2024
1 parent 5234295 commit 2702558
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions src/main/java/com/cedarsoftware/util/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ public static Date parseDate(String dateStr) {
remains = remains.trim();
matcher = timePattern.matcher(remains);
remnant = matcher.replaceFirst("");
boolean noTime = false;

if (remnant.length() < remains.length()) {
hour = matcher.group(1);
min = matcher.group(2);
Expand All @@ -236,36 +238,27 @@ public static Date parseDate(String dateStr) {
tz = stripBrackets(matcher.group(5).trim());
}
} else {
matcher = null; // indicates no "time" portion
noTime = true; // indicates no "time" portion
}

verifyNoGarbageLeft(remnant);

// Set Timezone into Calendar if one is supplied
Calendar c = Calendar.getInstance();
if (tz != null) {
if (tz.startsWith("-") || tz.startsWith("+")) {
ZoneOffset offset = ZoneOffset.of(tz);
ZoneId zoneId = ZoneId.ofOffset("GMT", offset);
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
c.setTimeZone(timeZone);
} else {
try {
ZoneId zoneId = ZoneId.of(tz);
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
c.setTimeZone(timeZone);
} catch (Exception e) {
TimeZone timeZone = TimeZone.getTimeZone(tz);
if (timeZone.getRawOffset() != 0) {
c.setTimeZone(timeZone);
} else {
throw e;
}
}
}
}
c.clear();
// Set Timezone into Calendar
Calendar c = initCalendar(tz);

return getDate(dateStr, c, noTime, year, month, day, hour, min, sec, milli);
}

private static Date getDate(String dateStr,
Calendar c,
boolean noTime,
String year,
int month,
String day,
String hour,
String min,
String sec,
String milli) {
// Build Calendar from date, time, and timezone components, and retrieve Date instance from Calendar.
int y = Integer.parseInt(year);
int m = month - 1; // months are 0-based
Expand All @@ -278,7 +271,7 @@ public static Date parseDate(String dateStr) {
throw new IllegalArgumentException("Day must be between 1 and 31 inclusive, date: " + dateStr);
}

if (matcher == null) { // no [valid] time portion
if (noTime) { // no [valid] time portion
c.set(y, m, d);
} else {
// Regex prevents these from ever failing to parse.
Expand All @@ -304,6 +297,33 @@ public static Date parseDate(String dateStr) {
return c.getTime();
}

private static Calendar initCalendar(String tz) {
Calendar c = Calendar.getInstance();
if (tz != null) {
if (tz.startsWith("-") || tz.startsWith("+")) {
ZoneOffset offset = ZoneOffset.of(tz);
ZoneId zoneId = ZoneId.ofOffset("GMT", offset);
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
c.setTimeZone(timeZone);
} else {
try {
ZoneId zoneId = ZoneId.of(tz);
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
c.setTimeZone(timeZone);
} catch (Exception e) {
TimeZone timeZone = TimeZone.getTimeZone(tz);
if (timeZone.getRawOffset() != 0) {
c.setTimeZone(timeZone);
} else {
throw e;
}
}
}
}
c.clear();
return c;
}

private static void verifyNoGarbageLeft(String remnant) {
// Clear out day of week (mon, tue, wed, ...)
if (StringUtilities.length(remnant) > 0) {
Expand Down

0 comments on commit 2702558

Please sign in to comment.