Introduction
Most applications have the need for timestamping events or showing date/times, among many other use-cases:
- When we publish blogs on a website, the date of posting gets written down into a database and shown to the reader.
- When we make an action, we'd want to know the time of it to be available so that we can keep track of them.
- When we buy something online or make a transaction, our banks offer us the transaction list with the exact timestamps for us to review.
Long story short, getting the current date and time in Java is very important and has a myriad of usages, and thankfully, it's really easy to attain it for any kind of use.
In this article we'll show the many different ways you can get the current date and time in Java.
System.currentTimeMillis()
If you'd like to get a single numeric value of milliseconds passed since the UNIX epoch, it's as easy as:
System.currentTimeMillis();
Printing this value out would result in something similar to this:
1543105352845
When converting this number back to a human-readable date, it represents:
Sunday, 25. November 2018 0:22:32.845
And to do this in Java, we need only a couple of lines of code:
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
Running this piece of code would yield:
2018-11-25 at 01:22:12 CET
Note: Keep in mind that this method returns the current value depending on your system time.
java.util.Date
In Java, getting the current date is as simple as instantiating the Date
object from the Java package java.util
:
Date date = new Date(); // this object contains the current date value
We can format this date easily:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date));
And running this piece of code would yield:
25-11-2018 00:41:29
The Calendar API
Amongst Java's myriad of classes is the Calendar
class, which is used to convert dates and time between specific instants and the calendar fields.
Getting the current date and time is really easy using a calendar:
Calendar calendar = Calendar.getInstance(); // gets current instance of the calendar
Again, we can easily format this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(calendar.getTime()));
Since SimpleDateFormat
only works with Date
objects, we're calling the getTime()
method of the Calendar
class to format it.
Running this piece of code would yield:
25-11-2018 00:43:39
The Date/Time API
Java 8 introduced us to a whole new API, which was included in the build to replace java.util.Date
and java.util.Calendar
.
It's still useful to know how to get the current date and time using the previous two classes since not all applications have yet migrated to Java 8.
The Date/Time API provides multiple classes that we can rely on to get the job done:
LocalDate
LocalDate
represents just a date, without time. This means that we can only get the current date, but without the time of the day:
LocalDate date = LocalDate.now(); // gets the current date
We can format this object:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(date.format(formatter));
Running this piece of code would yield:
25-11-2018
LocalTime
LocalTime
is the opposite of LocalDate
in that it represents only a time, without the date. This means that we can only get the current time of the day, without the actual date:
LocalTime time = LocalTime.now(); // gets the current time
We can easily format this object:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(time.format(formatter));
Running this piece of code would yield:
00:55:58
LocalDateTime
And finally, a LocalDateTime
, the most used Date/Time class in Java, represents the combination of the previous two - holding the value of both the date and the time:
LocalDateTime dateTime = LocalDateTime.now(); // gets the current date and time
We can easily format this object:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(dateTime.format(formatter));
Running this piece of code would yield:
25-11-2018 00:57:20
ZonedDateTime
Alongside the previous classes, the ZonedDateTime
class also offers this functionality:
ZonedDateTime dateTime = ZonedDateTime.now(); // gets the current date and time
We can easily format this object:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(dateTime.format(formatter));
Running this piece of code would yield:
25-11-2018 00:58:56
Joda-Time
Joda-Time is a tool that was originally developed to counter the problems with the old Java time and date classes.
With the release of Java 8, these problems have been tackled, and Joda-Time has served its purpose, without being used today very often.
Again, if your project isn't updated to Java 8, Joda-Time is still a great tool to use as an alternative.
To use it in your project, it's easiest to simply add a Maven dependency:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${version}</version>
</dependency>
Working with Joda-Time is very similar to working with Java's Date/Time API:
DateTime dateTime = new DateTime(); // initializes with the current date and time
Note: When initializing Joda-Time's DateTime
class for the first time, there are known performance issues that occur due to the loading of chronology descriptors.
It's easy to format this object:
DateTimeFormatter formatter = DateTimeFormatter.forPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(fmt.print(dateTime));
Running this piece of code would yield:
25-11-2018 01:08:42
Conclusion
There are many cases where someone would need to get the current date and/or time in Java, and we've covered all approaches there are as of now to do so, including the older classes - java.util.Date
and java.util.Calendar
as well as the newer java.time
classes that arrived with the new Date/Time API.
Additionally, we've covered Joda-Time and its approach to getting the current date and time.
If you'd like to read about Converting a String to a Date in Java, we've got it covered!