How to get Current Date and Time in Java with Example

You can get today's date or current date and time in Java by creating a new Date() object. A java.util.Date object represents the current date and time instance in Java. Now, if you want to date in DDMMYYYY format or any other date format, you can easily convert the Date object to String by using SimpleDateFormat class. Since the Date instance contains both date and time, if you only need a date then you can further convert the java.util.Date to java.sql.Date. Similarly, if you need just time then you can convert the java.util.Date to java.sql.Time, they represent the date and time part of java.util.Date object in Java. 

You can also use the java.util.Calendar class to get today's date and time. Further, if you are using Java 8 then you can use new Date and Time API classes like LocalDate, LocalTime, and LocalDateTime to get the current date, time, and date-time in Java.


Java always had issues with Date, Time, and Timezone. There initial two attempts in terms of old date and time API from JDK 1 and Calendar API from JDK 1.4 was plagued with thread-safety and usability issues. Thankfully, they have got it right a third time with the new Date and Time API of Java 8. 

I strongly suggest every Java developer learn Java 8, particularly the new Date and Time API. Don't just get busy with lambdas and stream. Unfortunately, not all Java 8 books cover this important topic so make sure you read Java SE 8 for Impatient by Cay S. Horstmann to learn this important development.




Java Program to get current date and time

Here is our sample Java program to print the current date and time in Java.  In this program, I have demonstrated that java.util.Date instance represents both date and time, and if you want to extract the Date part, you can simply convert the util date to java.sql.Date in Java.



Similarly, you can extract the time part from the java.util.Date you can convert it to java.sql.Time class. An interesting thing to know is that both java.sql.Date and java.sql.Time is subclasses of java.util.Date, but they don't follow the Liskov Substitution principle

You cannot pass java.sql.Date or java.sql.time, where a java.util.date instance is needed.

Since Date is nothing but a number of milliseconds from the epoch (1st January 1970, midnight GMT), it doesn't hold timezone information, but when you print the date, its toString(,) method is called, which is overridden to print the date in the local timezone. You can convert Date to String by using SimpleDateFormat as well, e.g., a date in DDMMYYY format.


package hello;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class JavaDatePrimer{

public static void main(String[] args) {

// current date and time in Java
Date today = new Date();
System.out.println("current date and time: " + today);

// displaying only current date in Java
Date currentDate = new java.sql.Date(today.getTime());
System.out.println("current date: " + currentDate);

// displaying only current time in Java
Date currentTime = new java.sql.Time(today.getTime());
System.out.println("current time: " + currentTime);


// today's date in ddMMyyyy in Java
DateFormat df = new SimpleDateFormat("ddMMyyyy");
String todayDate = df.format(today);
System.out.println("Today's date in ddMMyyyy format: " + todayDate);

// today's date in mm/dd/yyyy format
df = new SimpleDateFormat("MM/dd/yyyy");
todayDate = df.format(today);
System.out.println("Today's date in MM/dd/yyyy format: " + todayDate);
}
}

Output
current date and time: Mon Feb 15 18:21:44 SGT 2016
current date: 2016-02-15
current time: 18:21:44
Today's date in ddMMyyyy format: 15022016
Today's date in MM/dd/yyyy format: 02/15/2016


Important points to Remember  about Date and Time in Java

  1. Use M for the month; small case m is for a minute
  2. Use y for Year, capital case Y is invalid
  3. Be careful while using SimpleDateFormat; it is not thread-safe and should not be shared among multiple threads.
  4. The java.util.Date instance doesn't maintain timezone, but when you print the date object, it's toString() print the date in your local timezone.
  5. Prefer a new Date and Time API from Java 8 if you are lucky to be running in JDK 8. 


That's all about how to get the current date and time in Java. You can also use the java.util.Calendar class to get the current date and time in Java as well. For further reading on date and time, you can read Core Java for Impatient, one of the best books to learn core Java.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.