Example Converting String to LocalDateTime to LocalDate or LocalTime or Timestamp

LocalDateTime is an immutable date-time object that represents a date-time, with default format as yyyy-MM-dd-HH-mm-ss.nnn. We will see in below post how to convert String to LocalDateTime, LocalDateTime to LocalDate, LocalDateTime to LocalTime or LocalDateTime to java.sql.Timestamp

Example 1) Convert String to LocalDateTime:

For converting String to LocalDateTime, LocalDateTime class provides a static parse() method that takes string input arguments to create LocalDateTime instance.


Example 1.2) Convert String to LocalDateTime + DateTimeFormatter

Lets see another example to convert a String to LocalDateTime Using DateTimeFormatter



 Example 2)  Convert LocalDateTime to LocalDate:

To convert LocalDateTime to LocalDate, LocalDateTime have toLocalDate() method that takes LocalDateTime as input arguments to create LocalDate instance.



 Example 3)  Convert LocalDateTime to LocalTime:

To convert LocalDateTime to LocalTime, LocalDateTime class has toLocalTime() method takes LocalDateTime as input arguments to create LocalTime instance.



 Example 4) Convert LocalDateTime to java.sql.Timestamp:

For converting LocalDateTime  instance to java.sql.Timestamp we have valueOf() Method in Timestapmp class that takes LocalDateTime  as input arguments to create java.sql.Timestamp instance.


Also Check Reference:

DateTimeFormatter Java 8 Document


Leave a Reply

Your email address will not be published. Required fields are marked *