Java - Date Time Local Datetime

Introduction

LocalDateTime class represents a date and a time without a time zone.

LocalDateTime provides several methods to create, manipulate, and compare datetimes.

LocalDateTime is a combination of LocalDate and LocalTime.

LocalDateTime = LocalDate + LocalTime

The following code creates some LocalDateTime objects:

Demo

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) {
    // Get the current local date time
    LocalDateTime ldt1 = LocalDateTime.now();

    // A local date time 2012-05-10T16:14:32
    LocalDateTime ldt2 = LocalDateTime.of(2012, Month.MAY, 10, 16, 14, 32);

    // Construct a local date time from a local date and a local time
    LocalDate ld1 = LocalDate.of(2012, 5, 10);
    LocalTime lt1 = LocalTime.of(16, 18, 41);
    LocalDateTime ldt3 = LocalDateTime.of(ld1, lt1); // 2012-05-10T16:18:41
    System.out.println(lt1);//from www.  j  a  va2  s  .  co m
    System.out.println(ldt1);
    System.out.println(ldt2);
    System.out.println(ldt3);
  }
}

Result

Related Topics