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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Get Current Date And Time In Java?

Published on Jul 31,2019 27 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 J2EE and SOA training and certification 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.

Comments
0 Comments

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.