beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java 8 – Get Current Date and Time

By Chaitanya Singh | Filed Under: Java 8 Features

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 Cancel reply

Your email address will not be published. Required fields are marked *

Java 8 Features

  • Lambda Expressions
  • Method References
  • Functional Interfaces
  • Java 8 Interface changes
  • Java 8 Stream API
  • Stream Filter
  • Java forEach()
  • Collectors Class
  • StringJoiner Class
  • Optional Class
  • Arrays.parallelSort()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap