Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How To Get Current Date And Time In Java?

Last updated on Jun 11,2021 21.5K Views


Ever wondered how to get current date and time in Java? Well this article will give you different ways to do the same. Following pointers will be covered in this article,

So let us get started with this article on How To Get Current Date And Time In Java.

How To Get Current Date And Time In Java

When the need to get the current date in the programming language of java arises, the following methods are put to use:

Using Date Class

The pattern required by the user is specified while creating the SimpleDateFormat instance. The usage of format() method of the DateFormat class is made to pass the date object as a parameter to the method.

DateFormat dform = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date obj = new Date();
System.out.println(dform.format(obj)); 

The code displays the date and time in the format mentioned, i.e. 25/07/2019 22:12:32.

Moving on with this article on How To Get Current Date And Time In Java.

Using Calendar Class:

The pattern desired by the user is specified.

An object of Calendar class is created by using the getInstance method(). The format method is called and Calendar.getTime() is passed as a parameter to the method.

DateFormat dfor = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Calendar obj = Calendar.getInstance();
System.out.println(dfor.format(obj.getTime()));

Example:

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
//getting current date and time using Date class
DateFormat dfor = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date obj = new Date();
System.out.println(dfor.format(obj));
/*getting current date time using calendar class 
*An Alternative of above*/
Calendar cobj = Calendar.getInstance();
System.out.println(dfor.format(cobj.getTime()));
}
} 

Output:
25/07/19 09:23:35

25/07/19 09:23:35

There are various other methods by which the current date can be accessed in java:

time.Clock

The Clock.SystemUTC.instant() method outputs the current date and time.

System.out.println(java.time.Clock.systemUTC().instant());

sql.Date

The current date injava can be displayed by using the instance ofjava.sql.Date class.

long millis=System.currentTimeMillis();  
java.sql.Date dates=new java.sql.Date(millis);  
System.out.println(dates);

Moving on with this article on How To Get Current Date And Time In Java.

Date/ Time API

A new API was introduced in Java 8. The sole reason for this was to replace java.util.Date and java.util.calendar. It consists of the following classes:

LocalDateTime

This method returns the instance of the LocalDateTime class and prints both date and time.

System.out.println(java.time.LocalDateTime.now());

LocalDate 

This method simply returns the current Date.

LocalDate date = LocalDate.now();

LocalTime 

This method simply returns the current time and can be formatted.

LocalTime time = LocalTime.now();

ZonedDateTime

This method is used according to the time zone and can be implemented in the following way:

ZonedDateTime dateTime = ZonedDateTime.now();

This too can be formatted.

Java provides the user with methods that are very effective to retrieve the current date in java.

Thus we have come to an end of this article on ‘How To Get Current Date And Time In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Get Current Date And Time In Java?

edureka.co