Java Data Type How to - Create Local date time from Clock








Question

We would like to know how to create Local date time from Clock.

Answer

import java.time.Clock;
import java.time.LocalDateTime;
/*  w w  w .j  a  v a  2s.c o m*/
public class Main {
  public static void main(String[] args) {
    // Current Time
    Clock clock = Clock.systemUTC();
    System.out.println(Clock.systemDefaultZone());

    LocalDateTime dateAndTime = LocalDateTime.now(clock);
    System.out.println(dateAndTime);
    System.out.println(LocalDateTime.now());

  }
}

The code above generates the following result.