Java Program to create LocalDateTime from Clock

Java 8Object Oriented ProgrammingProgramming

At first, set the Clock:

Clock clock = Clock.systemUTC();

Now, create LocalDateTime:

LocalDateTime dateTime = LocalDateTime.now(clock);

Example

import java.time.Clock;
import java.time.LocalDateTime;
public class Demo {
   public static void main(String[] args) {
      Clock clock = Clock.systemUTC();
      System.out.println("Clock = "+Clock.systemDefaultZone());
      LocalDateTime dateTime = LocalDateTime.now(clock);
      System.out.println("LocalDateTime = "+dateTime);
   }
}

Output

Clock = SystemClock[Asia/Calcutta]
LocalDateTime = 2019-04-19T09:29:50.605820900
raja
Published on 25-Apr-2019 16:13:15
Advertisements