How can I parse/format dates with LocalDateTime? (Java 8)

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern. String str = “1986-04-08 12:30”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”); LocalDateTime dateTime = LocalDateTime.parse(str, formatter); Formatting date and … Read more

Format LocalDateTime with Timezone in Java8

LocalDateTime is a date-time without a time-zone. You specified the time zone offset format symbol in the format, however, LocalDateTime doesn’t have such information. That’s why the error occured. If you want time-zone information, you should use ZonedDateTime. DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(“yyyyMMdd HH:mm:ss.SSSSSS Z”); ZonedDateTime.now().format(FORMATTER); => “20140829 14:12:22.122000 +09”

How to convert a LocalDate to an Instant?

The Instant class represents an instantaneous point on the time-line. Conversion to and from a LocalDate requires a time-zone. Unlike some other date and time libraries, JSR-310 will not select the time-zone for you automatically, so you must provide it. LocalDate date = LocalDate.now(); Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant(); This example uses the default time-zone of … Read more

LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]

tl;dr Is there a simple way to convert a LocalDate (introduced with Java 8) to java.util.Date object? By ‘simple’, I mean simpler than this Nope. You did it properly, and as concisely as possible. java.util.Date.from( // Convert from modern java.time class to troublesome old legacy class. DO NOT DO THIS unless you must, to inter … Read more

What’s the difference between ZonedDateTime and OffsetDateTime?

Q: What’s the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: “OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds … Read more

What is the difference between ZoneOffset.UTC and ZoneId.of(“UTC”)?

The answer comes from the javadoc of ZoneId (emphasis mine) … A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. There are two distinct types of ID: Fixed offsets – a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times Geographical regions … Read more