How to get current date in java / Various ways to get the current date in java

Most applications require calculating current date for different reasons. It may be for logging events, for saving the creation, updation or deletion time of entities, recording the login details of a user and so on. Java provides different methods to calculate current date and time. Following article explains different methods to obtain current date and/or time in java.

Method 1 : Using java.util.Date class

This is the simplest method to get the present date and time details in java. Just create a java.util.Dateobject using its no-arg constructor. This will give you many details about the current instant such as the time, date, day of week, timezone and year.

 Date currentDate = new Date();
 System.out.println(currentDate );

Output :

Mon Aug 08 21:56:29 IST 2016

You can also retrieve the current time in milliseconds and create a java.util.Dateobject from this time as Date class has an overloaded constructor which accepts a longvalue as argument.

 Date currentDate  = new Date(System.currentTimeMillis());
 System.out.println(currentDate);

Above code also has the same output.

Method 2 : Using java.util.Calendar

Current date can also be retrieved using java.util.Calendarclass. Initialize a Calendar object using java.util.Calendar‘s static getInstance()method. By default this will create a calendar object with current date-time instant. Call its getTime()method to gain access to a java.util.Dateobject. This object will have the current date as above.

 Calendar cal = Calendar.getInstance();
 System.out.println(cal.getTime());

Output :

Mon Aug 08 21:56:29 IST 2016

Method 3 : Using LocalDateTime class (java 8)

This method uses the new java.timepackage introduced in java 8. This package has a class LocalDateTimewhich provides utility methods to get current date, create a desired date etc. now()method of the above mentioned LocalDateTime class returns a LocalDateTimeobject initialized to the current date and time values which are then accessed using its toLocalDate()method.

 LocalDateTime dateTime = LocalDateTime.now();
 System.out.println(dateTime.toLocalDate());

Output :

2016-08-08

Method 4 : Using java.sql.Timestamp

This method uses java.sql.Timestampto get current date and time. This class has a constructor which takes a value of type long and converts it into a date. Hence if we pass it the current time value in milliseconds, then it will give us current date and time. For calculating current time in milliseconds, we create an instance of java.util.Calendarclass. This class when initialized gives the calendar object with current date-time values. Then we call getTime()method on this calendar object which gives a java.util.Dateobject. Calling getTime()again gives us the current time in milliseconds which can be passed to the java.sql.Timestampconstructor.

 

 Calendar calendar = Calendar.getInstance();
 long currentTime = calendar.getTime().getTime();
 Timestamp currentTimeStamp = new Timestamp(currentTime);

Output :

2016-08-08 18:51:40.564

Let’s tweak in :

  1. no-arg constructor of java.util.Datecalls its another constructor which accepts a longvalue as argument.  The no-arg constructor passes the current time in milliseconds using System.currentTimeMills()to the other constructor.
  2. Calling getInstance()method on java.util.Calendarclass creates a calendar object with current date-time values.
  3. java.util.Calendarclass can also provide individual date components such as Date, Month, Year, Day of week and so on using its get()method which accepts an integer which corresponds to Calendar’s static fields as :
     Calendar cal = Calendar.getInstance();
     System.out.println(c.get(Calendar.DAY_OF_MONTH));
    

    The above code will print 8 for any date of August month.

  4. java.time.LocalDateTimeis immutable and thread-safe which means there is no need of any explicit synchronization handling when using this class in a multi-threaded environment.
  5. Though now()method of java.time.LocalDateTime is static but it returns an instance of this class.
  6. java.sql.Timestamp‘s constructor which takes a longvalue as argument calls the setTime()method of java.util.Dateclass with the supplied long value as argument.
  7. java.sql.Timestampis the child class of java.util.Date.

Have any more ways or have any suggestions for the above stated ways….Comment in the space below to tell us !!!!

sdasdasd

4 Comments

Leave a Reply