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.Date
object 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.Date
object from this time as Date class has an overloaded constructor which accepts a long
value 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.Calendar
class. 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.Date
object. 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.time
package introduced in java 8. This package has a class LocalDateTime
which provides utility methods to get current date, create a desired date etc. now()
method of the above mentioned LocalDateTime
class returns a LocalDateTime
object 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.Timestamp
to 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.Calendar
class. 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.Date
object. Calling getTime()
again gives us the current time in milliseconds which can be passed to the java.sql.Timestamp
constructor.
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 :
- no-arg constructor of
java.util.Date
calls its another constructor which accepts along
value as argument. The no-arg constructor passes the current time in milliseconds usingSystem.currentTimeMills()
to the other constructor. - Calling
getInstance()
method onjava.util.Calendar
class creates a calendar object with current date-time values. java.util.Calendar
class can also provide individual date components such as Date, Month, Year, Day of week and so on using itsget()
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.
java.time.LocalDateTime
is immutable and thread-safe which means there is no need of any explicit synchronization handling when using this class in a multi-threaded environment.- Though
now()
method ofjava.time.LocalDateTime
is static but it returns an instance of this class. java.sql.Timestamp
‘s constructor which takes along
value as argument calls thesetTime()
method ofjava.util.Date
class with the suppliedlong
value as argument.java.sql.Timestamp
is the child class ofjava.util.Date
.
Have any more ways or have any suggestions for the above stated ways….Comment in the space below to tell us !!!!