Using a Local Date, Time, and Datetime - Java Date Time

Java examples for Date Time:Local Date Time

Description

Using a Local Date, Time, and Datetime

Demo Code

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    // Create a local date and time
    LocalDate ld = LocalDate.of(2019, Month.MAY, 11);
    LocalTime lt = LocalTime.of(8, 52, 23);
    System.out.println("ld: " + ld);
    System.out.println("ld.isLeapYear(): " + ld.isLeapYear());
    System.out.println("lt: " + lt);
    // w ww. ja  v a2  s . c o  m
    // Create a local datetime from the local date and time
    LocalDateTime ldt = LocalDateTime.of(ld, lt);
    System.out.println("ldt: " + ldt);
    
    // Add 2 months and 25 minutes to the local datetime
    LocalDateTime ldt2 = ldt.plusMonths(2).plusMinutes(25) ;
    System.out.println("ldt2: " + ldt2);
    
    // Derive the local date and time from the localdatetime
    LocalDate ld2 = LocalDate.from(ldt2);
    LocalTime lt2 = LocalTime.from(ldt2);
    System.out.println("ld2: " + ld2);
    System.out.println("lt2: " + lt2);
  }
}

Result


Related Tutorials