Formatting and parsing date and time with Java 8
✓ Published0🌍 Public
PP7h
Last edited Apr 3, 2016
Created on Apr 3, 2016
This example demonstrates how to format and parse date-time strings using the Java 8 `java.time` API, focusing on the distinction between `LocalDateTime` and `ZonedDateTime` when time-zone offsets are present. It defines a custom pattern with `DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss ZZ yyyy")` to parse a fixed date string and to format the current time. The code shows that parsing a string with a time-zone offset requires `ZonedDateTime`, while formatting without an offset works with `LocalDateTime`. It also illustrates two equivalent syntaxes for formatting: calling `formatter.format(now)` and `now.format(formatter)`.
AI-generated description