1.

Get current date and time in Java

Answer»

The current date and time can be obtained using many METHODS in Java. One of these is using the LocalDateTime.now() method. The instance of the LocalDateTime class is RETURNED by this method which can then be printed.

A program that demonstrates this is given as FOLLOWS:

import java.time.format.DateTimeFormatter;   import java.time.LocalDateTime;    PUBLIC class Demo {      public static void MAIN(String[] args)  {      DateTimeFormatter datetime = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");     LocalDateTime curDateTime = LocalDateTime.now();     System.out.println("The current date time is: " + datetime.format(curDateTime));    }    }

The output of the above program is as follows:

The current date time is: 2018/12/15 12:18:54


Discussion

No Comment Found