Java Program to convert LocalDateTime to LocalDate and LocalTime

Java 8Object Oriented ProgrammingProgramming

At first set a LocalDateTime:

LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.of(date, time);

Now, convert the LocalDateTime to LocalDate and LocalTime:

LocalDate localDate = LocalDateTime.now().toLocalDate();
LocalTime localTime = LocalDateTime.now().toLocalTime();

Example

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Demo {
   public static void main(String[] args) {
      LocalDate date = LocalDate.now();
      LocalTime time = LocalTime.now();
      LocalDateTime dateTime = LocalDateTime.of(date, time);
      System.out.println("DateTime = "+dateTime);
      LocalDate localDate = LocalDateTime.now().toLocalDate();
      LocalTime localTime = LocalDateTime.now().toLocalTime();
      System.out.println("Date = "+localDate);
      System.out.println("Time = "+localTime);
   }
}

Output

DateTime = 2019-04-18T13:57:22.591327100
Date = 2019-04-18
Time = 13:57:22.591327100
raja
Published on 25-Apr-2019 15:46:04
Advertisements