localdatetime from date code example

Example 1: date to localdate

dateToConvert.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();

Example 2: convert localdatetime to localdate

import java.time.LocalDate;
import java.time.LocalDateTime;
 
public class Main {
    public static void main(String[] args) {
 
        LocalDateTime localDateTime = LocalDateTime.now();
         
        LocalDate localDate = localDateTime.toLocalDate();
         
        System.out.println(localDate);
    }
}

Example 3: formatting specific date and time in localdatetime

String dateTime = "1995-07-25 15:03:45";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(dateTime, dateTimeFormatter);

Tags:

Java Example