samedi 2 avril 2016

How to Convert String to LocalDateTime in Java 8 - Example Tutorial

Suppose you have a date time String "2016-03-04: 11:01:20" and you want to convert this into a LocalDateTime object of Java 8 new date and time API, how do you do that? Well, if you have worked previously with String and Date then you know that you can parse String to Date in Java. Prior to Java 8, we used to use SimpleDateFormat, which was mutable and not thread-safe, and advised not to be shared between threads. In Java 8, you can use the DateTimeFormatter class to convert String to LocalDate, LocalTime or LocalDateTime. Unlike SimpleDateFormat, the DateTimeFormatter is immutable and thread-safe and you can use it as a static variable for sharing and reuse. It also comes with pre-defined date time format e.g. ISO-8601 format. The ISO_LOCAL_DATE_TIME formats or parses a date-time without an offset, such as '2016-03-04T10:15:30'.
Read more »
Share:

0 commentaires:

Enregistrer un commentaire