In this tutorial we look at the classes in java that are helpful in manipulating dates. Manipulating dates in java can be challenging, especially if you are building applications that cater to multiple timezones. Parsing dates, printing a date in UTC, printing a date in a different timezone, comparing dates, adding time to date… we will be looking at all of these in this tutorial. We first begin with the introduction to the classes and then follow up with examples. You may want to directly jump to the examples, however, we strongly recommend that you understand the classes first, since that will make the examples very easy to understand.
java.util.Date - This class is used to represent a specific time with a precision of millisecond. If you create a new Date object, it obtains the current system time using System.currentTimeMillis(). This is the number of milliseconds since epoch time (midnight, January 1, 1970 UTC). Here’s the tricky part, date stores time as number of seconds since epoch, it is a UTC time. However, when you print the date, you get the string representation of the date in the local timezone. Also note that the Date class is not designed to manipulate hour, month, year etc or retrieve them. Use this class only to create data or compare two dates. For all other uses, see the Calendar or GregorianCalendar class.
java.util.Calendar - Calendar class allows manipulating data using its various fields such as minute, hour, day etc. As in the java.util.Date object, time is represented as number of milliseconds since epoch time. To get an instance of the Calendar use the Calendar.getInstance() method which returns a locale sensitive Calendar instance. Various set and get methods can be used to set and get time values. The current time is represented in two ways - 1. as number of milliseconds since epoch and 2. as local fields such as YEAR, MONTH, DAY, HOUR, MINUTE etc. The conversion between the two types is achieved by using the timezone offset (getOffset()) and the daylight savings fields.
the MONTH fields starts from 0 (JANUARY).
The DAY_OF_MONTH starts from 1
the DAY_OF_WEEK starts from SUNDAY with a value of 1.
HOUR starts from 0
java.util.GregorialCalendar - A GregorianCalendar is the default implementation of the Abstract Calendar class. Next let us look at examples
public void createDateAndPrintIt() {
// create and prints the current date. It prints the data in the local
// timezone
System.out.println(new java.util.Date());
// prints Sun Jul 28 20:09:36 IST 2013
// get the number of milli seconds since epock
System.out.println(new java.util.Date().getTime());
// prints 1375022617792
}
public void createCalendarAndPrintIt() {
Calendar cal = Calendar.getInstance();
System.out.println(cal);
// java.util.GregorianCalendar[time=1375025205859,areFieldsSet=true,areAllFieldsSet=true,lenient=true,
// zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],
// firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=6,WEEK_OF_YEAR=31,WEEK_OF_MONTH=5,DAY_OF_MONTH=28,
// DAY_OF_YEAR=209,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=56,SECOND=45,MILLISECOND=859,ZONE_OFFSET=19800000,DST_OFFSET=0]
System.out.println(cal.getTime());
// prints Sun Jul 28 20:56:45 IST 2013
}
public void manipulateCalendar() {
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());// Sun Jul 28 21:53:55 IST 2013
// add a day
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(cal.getTime());// Mon Jul 29 21:53:55 IST 2013
cal.add(Calendar.DAY_OF_WEEK, 1);
System.out.println(cal.getTime());// Tue Jul 30 21:53:55 IST 2013
cal.add(Calendar.HOUR, 1);
System.out.println(cal.getTime());// Tue Jul 30 22:53:55 IST 2013
cal.add(Calendar.HOUR_OF_DAY, 1);
System.out.println(cal.getTime());// Tue Jul 30 23:53:55 IST 2013
cal.add(Calendar.MINUTE, 100);
System.out.println(cal.getTime());// Wed Jul 31 01:33:55 IST 2013
// demonstrate leniency
cal.add(Calendar.DAY_OF_MONTH, 32);
System.out.println(cal.getTime());// Sun Sep 01 01:33:55 IST 2013
}
(Note: This article’s original links is here )