Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure joda compatibility in custom date formats #38171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1604,13 +1604,16 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
} else if (isLocalDateSet) {
return localDate.atStartOfDay(zoneId);
} else if (isLocalTimeSet) {
return of(LOCALDATE_EPOCH, localTime, zoneId);
return of(getLocaldate(accessor), localTime, zoneId);
} else if (accessor.isSupported(ChronoField.YEAR)) {
if (accessor.isSupported(MONTH_OF_YEAR)) {
return getFirstOfMonth(accessor).atStartOfDay(zoneId);
} else {
return Year.of(accessor.get(ChronoField.YEAR)).atDay(1).atStartOfDay(zoneId);
}
} else if (accessor.isSupported(MONTH_OF_YEAR)) {
// missing year, falling back to the epoch and then filling
return getLocaldate(accessor).atStartOfDay(zoneId);
} else if (accessor.isSupported(WeekFields.ISO.weekBasedYear())) {
if (accessor.isSupported(WeekFields.ISO.weekOfWeekBasedYear())) {
return Year.of(accessor.get(WeekFields.ISO.weekBasedYear()))
Expand All @@ -1630,6 +1633,18 @@ public static ZonedDateTime from(TemporalAccessor accessor) {
throw new IllegalArgumentException("temporal accessor [" + accessor + "] cannot be converted to zoned date time");
}

private static LocalDate getLocaldate(TemporalAccessor accessor) {
if (accessor.isSupported(MONTH_OF_YEAR)) {
if (accessor.isSupported(DAY_OF_MONTH)) {
return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), accessor.get(DAY_OF_MONTH));
} else {
return LocalDate.of(1970, accessor.get(MONTH_OF_YEAR), 1);
}
}

return LOCALDATE_EPOCH;
}

@SuppressForbidden(reason = "ZonedDateTime.of is fine here")
private static ZonedDateTime of(LocalDate localDate, LocalTime localTime, ZoneId zoneId) {
return ZonedDateTime.of(localDate, localTime, zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ public void testTimeZoneFormatting() {
formatter3.parse("20181126T121212.123-0830");
}

public void testCustomTimeFormats() {
assertSameDate("2010 12 06 11:05:15", "yyyy dd MM HH:mm:ss");
assertSameDate("12/06", "dd/MM");
assertSameDate("Nov 24 01:29:01 -0800", "MMM dd HH:mm:ss Z");
}

// this test requires tests to run with -Djava.locale.providers=COMPAT in order to work
// public void testCustomTimeFormats() {
// assertSameDate("2010 12 06 11:05:15", "yyyy dd MM HH:mm:ss");
// assertSameDate("12/06", "dd/MM");
// assertSameDate("Nov 24 01:29:01 -0800", "MMM dd HH:mm:ss Z");
// public void testCustomLocales() {
//
// // also ensure that locale based dates are the same
// assertSameDate("Di., 05 Dez. 2000 02:55:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));
Expand Down