How to get Current Date and Time in Java With Examples

Java provides two classes to get current date and time – Date and Calendar.

Get Current Date in Java

Here is the simple program showing how we can use these classes to get the current date and time in Java.

Output of the above program is:

Few points to note while getting the current date in java:

  • Calendar.getInstance() is more expensive method, so use it only when you need Calendar object. Else use Date object.
  • The number of milliseconds is the number of milliseconds since January 1, 1970, 00:00:00 GMT.
  • Notice the difference between milliseconds in Calendar and Date object, it’s because of the time taken for java program to create Date and Calendar object. Date object is created first, hence it’s milliseconds value is lower.
  • You can use SimpleDateFormat class to format the date in different ways.

Update: If you are working on Java 8, then you should consider using new Date Time API. For more details, please read Java Date Time API Tutorial.

By admin

Leave a Reply