Date Type Conversion
DateTypeConvert converts legacy Date, Calendar, SQL date types, epoch milliseconds, and Java 8 java.time values. Supply a ZoneId whenever a conversion needs a civil time zone.
Instant instant = Instant.now();
ZoneId zone = ZoneId.of("Asia/Shanghai");
LocalDateTime local = new DateTypeConvert(instant).to(LocalDateTime.class, zone);
Calendar calendar = new DateTypeConvert(instant).to(Calendar.class, zone);
Important semantics
- Epoch value
0is valid and represents1970-01-01T00:00:00Z. - Date-only strings are parsed as dates instead of being forced through a date-time formatter.
- Parsing and output formatting use separate formatters. Parsing is strict, so invalid values such as
2023-02-29raise a parsing exception instead of being silently adjusted. - Converting
LocalDatetoLocalDatereturns the original value without applying zone rules, so a skipped civil date in the supplied zone cannot change the date. - A
Calendarresult uses the requested zone, not an unrelated system-default calendar. - A
Calendarinput retains its own time zone when noZoneIdis supplied; an explicitly suppliedZoneIdtakes precedence. ZonedDateTimeandOffsetDateTimeinputs are returned unchanged when the requested target is the same type, preserving their original zone or offset.- Converting
LocalDateTimeto an instant is strict: a daylight-saving gap is rejected, and an overlap is rejected as ambiguous instead of silently choosing an offset. OffsetTimehas no date. It may be converted only toOffsetTimeorLocalTime; instant-based conversions are rejected rather than attaching the system's current date.LocalTimeis returned unchanged when requested asLocalTime. It can become anOffsetTimeonly with an explicit fixed offset; other date-dependent conversions are rejected.
Instant epoch = new DateTypeConvert(0L).to(Instant.class, ZoneOffset.UTC);
assertEquals(Instant.EPOCH, epoch);
If no input has been set or a conversion is inherently ambiguous, the API throws an exception instead of silently inventing missing context.