How to Get Current Date And Time In Java

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.

and heres a simple junit test to show what we get if we print this out to the console.

Output

We can add some code to format it in the way we want rather than the default using SimpleDateFormat.

and heres the output of our junit test for that

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 timePOSIX 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

The output of running System.currentTimeMillis() in a junit test is shown below

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

with the output of

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.

If we create a junit test to print that out

we get the following output

so lets add some formatting using DateTimeFormatter

and we get the following output when we run it

LocalTime

LocalTime is a date/time object that represents the time only, so we can use that to get the current time.

Lets create a junit test to print that out also

and the output of that is

and lets add some formatting using DateTimeFormatter

and the output of that is

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.

Again lets create a test to print that out in the default format

with output

Now lets format using DateTimeFormatter

with output



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.

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

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.

Now lets format using DateTimeFormatter to format those

with output

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.

References

Oracle Java Date Time API Reference

Leave a Comment

Your email address will not be published. Required fields are marked *