The Calendar class is an abstract class used to convert dates. We can use this class to convert a Date object to fields, such as YEAR, MONTH, HOUR, and so on. We can also use these fields to update a Date object.
Calendar () Creates a calendar with the default TimeZone and Locale
Calendar (TimeZone , Locale) Creates a calendar with the given TimeZone and Locale
getTime() Returns the date and time
setTime(Date) Sets the date and time
set(byte,int) Sets the specified field to the specified value
set(int,int,int) Sets the year, month, and date
clear(byte) Clears the specified field
isSet(int) Returns true if the specified field is set
setFirstDayOfWeek(byte) Sets the first day of the week
getFirstDayOfWeek() Returns the first day of the week
Calendar provides a class method, getInstance which returns a Calendar object whose time fields have been initialized with the current date and time:
Calendar k = Calendar.getInstance();
A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style. Calendar defines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has value MONTH == JANUARY for all calendars.
Calendar fields can be changed using three methods: set(), add(), and roll().
Set (f, value)
This method changes field f to value. In addition, it sets an internal member variable to indicate that field f has been changed. Although field f is changed immediately, the calendar's milliseconds is not recomputed until the next call to get(), getTime(), or getTimeInMillis() is made. As a result of changing a field using set(), other fields may also change, depending on the field, the field value, and the calendar system. Calling set (Calendar.MONTH, Calendar. SEPTEMBER) sets the calendar to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 if getTime () is then called. However, a call to set(Calendar.DAY_OF_MONTH, 30) before the call to getTime() sets the calendar to September 30,1999, since no recomputation occurs after set() itself.
In addition, unlike set(), add() forces an immediate recomputation of the calendar's milliseconds and all fields.
Example: Consider a Gregorian calendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000. It sets the MONTH field to September, since adding 13 months to August gives September of the next year. Since DAY_OF_MONTH cannot be 31 in September in a Gregorian calendar, it also sets the DAY_OF_MONTH to 30, the closest possible value.
The Date class encapsulates date and time information and allows Date objects to be accessed in a system independent manner. Date provides methods for accessing specific date and time measurements, such as year, month, day, date, hours, minutes, and seconds, and for displaying dates in a variety of standard formats.
Since Date class is not able to handle internationalization and also cannot deal with the differences in daylight saving time in different locations. All the methods for converting time between binary and human readable form are thus handled by the Calendar, Gregorian calendar, TimeZone, SimpleTimeZone, and Locale classes.
Dates can be compared to each other by using their UTC values (the UTC value is the number of seconds since January I, 1970). There is a difference between UTC and standard astime (UT/GMT): one is based on an atomic clock and the other is based on astronomical observations.
Date() Constructs a date using today's date and time
Date (1ong) Constructs a date using a single UTC value
after(Date) Returns true if the date is later than the specified date
before(Date) Returns true if the date is earlier than the specified date
equals(Object) Returns true if the date and the specified date are equal
getTime() Returns the time as a single UTC value
hashCode() Computes a hash code for the date
setTime(long) Sets the time using a single UTC value
toString() Converts a date to text using UNIX ctime() conventions
public static void main(String args)
Date d=new Date();
Calendar c = Calendar.getInstance();
System.out.println("Current Date and Time is : "+d);
c.set(Calendar.YEAR, 2003 + 1900);
System.out.print("The new date set is "+ c.get (Calendar.DAY_OF_MONTH) +"/");
System.out.println(c.get(Calendar.YEAR) - 1900);
System.out.print("Current time is "+ c.get (Calendar.HOUR_OF_DAY)+":");