DEV Community

Discussion on: YYYY vs yyyy - The day the Java Date Formatter hurt my brain

Collapse
 
stealthmusic profile image
Jan Wedel

Parsing dates with patterns is like parsing emails or HTML. Usually, you shouldn’t do it.

Pre Java 8, I also used those formatters but I regularly ran into bugs like yours or undiscovered edge cases.

Since Java 8, we can simply use Instant::toString and Instant::parse to convert from and to valid ISO time stamps. Replace Instant by OffsetDateTime or ZonedDateTime when you want to preserve offsets or even time zones (which is usually also
not a good idea).