Introduction
For are many reasons that you would need to retrieve the current date and time in a java application.
- When you create some sort of transaction in your application, like a customer placing an order, you need to stamp it with date and time to record when the order was placed
- When we receive data from another system, we want to know when we received that in case there may be problems with the data and we may need to let other people know for problem resolution
- When we generate data within our system, we need to know when the data was generated for archiving as at some point its likely we will want to remove the old data.
So there can be these and many more reasons that we need a data/time or a timestamp for our data and processes in java. So lets have a look at the different ways to get the current data and time in Java.
java.util.Date
Getting the current data in java is as easy and create an object from the java.util.Date class.
Date date = new Date();
and heres a simple junit test to show what we get if we print this out to the console.
@Test
public void dateTest1() throws Exception {
Date date = new Date();
System.out.println(date);
}
Output
Thu Oct 10 11:24:57 BST 2019
We can add some code to format it in the way we want rather than the default using SimpleDateFormat.
@Test
public void dateTest2() throws Exception {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String formattedDateString = formatter.format(date);
System.out.println(formattedDateString);
}
and heres the output of our junit test for that
10/Oct/2019 11:35:33
System.currentTimeMillis()
For situation where you may need to do calculations and comparision down to the millisecond you can access the current time in milliseconds using System.currentTimeMillis().
The output of this is the number of milliseconds since the UNIX epoch, which sounds fancy but simply gives us a way of defining a specific point in time.
Unix time (also known as Epoch time, POSIX time,[1] seconds since the Epoch,[2] or UNIX Epoch time[3]) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, that is the time 00:00:00 UTC on 1 January 1970, minus leap seconds.
https://en.wikipedia.org/wiki/Unix_time
@Test
public void currentTimeMillis() throws Exception {
System.out.println(System.currentTimeMillis());
}
The output of running System.currentTimeMillis() in a junit test is shown below
1570704456698
If we want to format the value we can pass it as a parameter to the Date class and then format in the same way as previously
@Test
public void currentTimeMillisFormatted() throws Exception {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String formattedDateString = formatter.format(date);
System.out.println(formattedDateString);
}
with the output of
10/Oct/2019 11:54:37
The Date/Time API
Java 8 introduced the new Date / Time API, as there were certain issues with the java.util.Date and java.util.Calendar from the previous java releases. You should still be familiar with the old ways of processing dates, as theres likely to be legacy pre java 8 code around for a long time that you will need to work with, but for new code you should definitely be using the Date / Time API from java 8 onwards.
The main classes we need to look at are
LocalDate
LocalDate is a date/time object that represents the date only, so we can use that to get the current date.
LocalDate date = LocalDate.now();
If we create a junit test to print that out
@Test
public void localDateTest1() throws Exception {
LocalDate date = LocalDate.now();
System.out.println(date);
}
we get the following output
2019-10-10
so lets add some formatting using DateTimeFormatter
@Test
public void localDateTest2() throws Exception {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String formattedDateString = formatter.format(date);
System.out.println(formattedDateString);
}
and we get the following output when we run it
10-Oct-2019
LocalTime
LocalTime is a date/time object that represents the time only, so we can use that to get the current time.
LocalTime time = LocalTime.now();
Lets create a junit test to print that out also
@Test
public void localTimeTest1() throws Exception {
LocalTime time = LocalTime.now();
System.out.println(time);
}
and the output of that is
12:23:10.757
and lets add some formatting using DateTimeFormatter
@Test
public void localTimeTest2() throws Exception {
LocalTime time = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTimeString = formatter.format(time);
System.out.println(formattedTimeString);
}
and the output of that is
12:25:56
LocalDateTime
LocalDateTime is a date/time object that represents the date and time, so we can use that to get the current date and time together.
LocalDateTime dateTime = LocalDateTime.now();
Again lets create a test to print that out in the default format
@Test
public void localDateTimeTest1() throws Exception {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);
}
with output
2019-10-10T12:31:38.050
Now lets format using DateTimeFormatter
@Test
public void localDateTimeTest2() throws Exception {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
String formattedTimeString = formatter.format(dateTime);
System.out.println(formattedTimeString);
}
with output
10-Oct-2019 12:48:15
ZoneDateTime
ZonedDateTime is a date/time object that represents the date and time with a timezone, so we can use that to get the current date and time together for a specific timezone.
ZonedDateTime dateTime = ZonedDateTime.now();
ZonedDateTime dateTimeParis = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
The above gives is the date and time in our current timezone (London), and by passing a ZoneId we can get the current time in Paris, which is an hour ahead of London. Again lets create a test to print those out in the default format
@Test
public void zonedDateTimeTest1() throws Exception {
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime);
ZonedDateTime dateTimeParis = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(dateTimeParis);
}
with output that shows that Paris is 1 hour ahead of hour current time. It also shows the offset from GMT, as we are in summer time at the moment.
2019-10-10T13:01:14.154+01:00[Europe/London]
2019-10-10T14:01:14.504+02:00[Europe/Paris]
Now lets format using DateTimeFormatter to format those
@Test
public void zonedDateTimeTest2() throws Exception {
ZonedDateTime dateTime = ZonedDateTime.now();
ZonedDateTime dateTimeParis = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
String formattedTimeString = formatter.format(dateTime);
System.out.println("London: " + formattedTimeString);
formattedTimeString = formatter.format(dateTimeParis);
System.out.println("Paris: " + formattedTimeString);
}
with output
London: 10-Oct-2019 13:06:13
Paris: 10-Oct-2019 14:06:13
Conclusion
So as you can see there are a number of different ways to get the current date and time in java, and also its easy to format that to display it in the way you need to. We have also seen that there are the old pre java 8 date and time classes, which you should move away from using if you are developing java8+ code, and the new java8 date / time API classes, which are definitely the way forward.