Introduction
Java provides many methods to represent date and time. We use Date and Calendar to get the current date in Java. Parts of the date can be formatted using DateFormat or SimpleDateFormat for display. As of Java 8, to convert and get the current date (now) between different time zones, it is recommended to use ZonedDateTime; however same functionality can be achieved using Date and Calendar both as well.
Get the current date using java.util.Date
To get the current date, just create a new object of java.util.Date. Make sure you don’t import java.sql.Date.
Date dt = new Date();
System.out.println(dt);
To get the current date of a different timezone, you should set the timezone. If you don’t know the time zones, you can print the list and then select the one you want.
String[] tzList = TimeZone.getAvailableIDs();
System.out.println(Arrays.toString(tzList));
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("US/Alaska"));
System.out.println(sdf.format(dt));
Get current date using Calendar
We import java.util.Calendar and then get the calendar instance.
Calendar cal = Calendar.getInstance();
Now use getTime() method to get the current date:
Date calDate = cal.getTime();
getTime() converts Calendar instance into Date object
System.out.println(calDate);
To get the current date of a different time zone, use the same code as the Date object by passing the calDate instead of the dt object to the sdf object. System.out.println(sdf.format(calDate)); Here is the full example for your reference:
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class CalendarExample {
/**
* @param args
*/
public static void main(String[] args) {
// There are multiple ways to get current date
// The most common method is to use new Date()
Date dt = new Date();
System.out.println(dt);
// Make sure you import java.util.Date and not java.sql.Date
// Another popular method is to use Calendar instance
Calendar cal = Calendar.getInstance();
Date calDate = cal.getTime(); //.getTime() converts Calendar instance into Date
System.out.println(calDate);
//We can get the current date of a different time zone also.
// Get list of time zones
String[] tzList = TimeZone.getAvailableIDs();
System.out.println(Arrays.toString(tzList));
// Set the format in which you want to get the current date for the selected time zone
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
// set the time zone
sdf.setTimeZone(TimeZone.getTimeZone("US/Alaska"));
System.out.println(sdf.format(dt));
//We can use Calendar as well for the same
sdf.setTimeZone(TimeZone.getTimeZone("GMT+11"));
System.out.println(sdf.format(calDate));
}
}
Conclusion
We have seen the usage of Date and Calendar to get the current date (now) or today’s date in Java. We have used a format that displays the day of the week, month in letters, date, time, time zone, and year. We can use any format using SimpleDateFormat , and the date & time will be displayed accordingly.
People are also reading:
Leave a Comment on this Post