Java Data Type How to - Create LocalDateTime in various way








Question

We would like to know how to create LocalDateTime in various way.

Answer

import java.time.LocalDateTime;
import java.time.Month;
//from  w w  w. j av  a2  s  .  c om
public class Main {

  public static void main(String[] args) {
    LocalDateTime currentDateTime = LocalDateTime.now();
    System.out.println(currentDateTime); // 2014-06-27T09:04:47.527

    // 2014-10-02 12:30
    LocalDateTime secondAug2014 = LocalDateTime.of(2014, 10, 2, 12, 30);
    System.out.println(secondAug2014); // 2014-10-02T12:30

    // 2014-12-24 12:00
    LocalDateTime christmas2014 = LocalDateTime.of(2014, Month.DECEMBER, 24, 12, 0);
    System.out.println(christmas2014); // 2014-12-24T12:00
  }
}

The code above generates the following result.