In the past we learned how to get current date and time in Java using Date and Calendar classes. Here we will see how can we get current date & time in Java 8.
Java 8 introduces a new date and time API java.time.* which has several classes, but the ones that we can use to get the date and time are: java.time.LocalDate, java.time.LocalTime & java.time.LocalDateTime.
Example: Program to get current date and time in Java 8
class DateExample {
   public static void main(String[] args) {
	/* Obtains the current date from the system clock in the 
	 * default time-zone.
	 */
	System.out.println("Current Date: "+java.time.LocalDate.now()); 
		
	/* Obtains the current time from the system clock in the 
	 * default time-zone.
	 */
	System.out.println("Current Time: "+java.time.LocalTime.now());  
		
	//current date and time
	System.out.println("Current Date & Time: "+java.time.LocalDateTime.now());  
   }
}
Output:
Current Date: 2017-09-27 Current Time: 13:12:04.103 Current Date & Time: 2017-09-27T13:12:04.103
Leave a Reply