Calendar in java

The abstract Calendar class provides a set of methods that allows you to convert a time in milliseconds to a number of useful components. Some examples of the type of information that can be provided are year, month, day, hour, minute, and second. It is intended that subclasses of Calendar will provide the specific functionality to interpret time information according to their own rules. This is one aspect of the Java class library that enables you to write programs that can operate in international environments. An example of such a subclass is GregorianCalendar

Calendar provides no public constructors.

MethodDescription
abstract void add(int which, int val)Adds val to the time or date component specified
 by which. To subtract, add a negative value. which
 must be one of the fields defined by Calendar, such
 as Calendar.HOUR.
  
boolean after(Object calendarObj)Returns true if the invoking Calendar object
 contains a date that is later than the one specified
 by calendarObj. Otherwise, it returns false.
  
boolean before(Object calendarObj)Returns true if the invoking Calendar object contains
 a date that is earlier than the one specified by
 calendarObj. Otherwise, it returns false.
  
final void clear( )Zeros all time components in the invoking object.
  
final void clear(int which)Zeros the time component specified by which in
 the invoking object.
  
Object clone( )Returns a duplicate of the invoking object.
  
boolean equals(Object calendarObj)Returns true if the invoking Calendar object
 contains a date that is equal to the one specified
 by calendarObj. Otherwise, it returns false.
 Method Description
 int get(int calendarField)Returns the value of one component of the invoking
   object. The component is indicated by calendarField.
   Examples of the components that can be requested
   are Calendar.YEAR, Calendar.MONTH,
   Calendar.MINUTE, and so forth.
   
 static Locale[ ] getAvailableLocales( )Returns an array of Locale objects that contains
   the locales for which calendars are available.
   
 static Calendar getInstance( )Returns a Calendar object for the default locale and
   time zone.
   
 static Calendar getInstance(TimeZone tz)Returns a Calendar object for the time zone
   specified by tz. The default locale is used.
 static Calendar getInstance(Locale locale)Returns a Calendar object for the locale specified
   by locale. The default time zone is used.
   
 static Calendar getInstance(TimeZone tz,Returns a Calendar object for the time zone
  Locale locale)specified by tz and the locale specified by locale.
   
 final Date getTime( )Returns a Date object equivalent to the time of the
   invoking object.
 TimeZone getTimeZone( )Returns the time zone for the invoking object.
   
 final boolean isSet(int which)Returns true if the specified time component is set.
   Otherwise, it returns false.
   
 void set(int which, int val)Sets the date or time component specified by which
   to the value specified by val in the invoking object.
   which must be one of the fields defined by
   Calendar, such as Calendar.HOUR.
   
 final void set(int year, int month,Sets various date and time components of the
  int dayOfMonth)invoking object.
   
 final void set(int year, int month,Sets various date and time components of the
  int dayOfMonth, int hours,invoking object.
  int minutes) 
 final void set(int year, int month,Sets various date and time components of the
  int dayOfMonth, int hours,invoking object.
  int minutes, int seconds) 
   
 final void setTime(Date d)Sets various date and time components of the
   invoking object. This information is obtained from
   the Date object d.
   
 void setTimeZone(TimeZone tz)Sets the time zone for the invoking object to that
   specified by tz.

Calendar defines the following int constants, which are used when you get or set components of the calendar:

ALL_STYLES FRIDAYPM 
AM HOURSATURDAY 
     
AM_PM HOUR_OF_DAYSECOND 
     
APRIL JANUARYSEPTEMBER 
     
AUGUST JULYSHORT 
     
DATE JUNESUNDAY 
     
DAY_OF_MONTH LONGTHURSDAY 
     
DAY_OF_WEEK MARCHTUESDAY 
DAY_OF_WEEK_IN_MONTH MAYUNDECIMBER 
     
DAY_OF_YEAR MILLISECONDWEDNESDAY 
     
DECEMBER MINUTEWEEK_OF_MONTH 
     
DST_OFFSET MONDAYWEEK_OF_YEAR 
ERA MONTHYEAR 
     
FEBRUARY NOVEMBERZONE_OFFSET 
     
FIELD_COUNT OCTOBER  

Table of Contents

Demonstrate Calendar

import java.util.Calendar;

class CalendarDemo {

public static void main(String args[]) { String months[] = {

"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

//Create a calendar initialized with the

//current date and time in the default

//locale and timezone.

Calendar calendar = Calendar.getInstance();

//Display current time and date information. System.out.print("Date: "); System.out.print(months[calendar.get(Calendar.MONTH)]); System.out.print(" " + calendar.get(Calendar.DATE) + " "); System.out.println(calendar.get(Calendar.YEAR));

System.out.print("Time: ");

System.out.print(calendar.get(Calendar.HOUR) + ":");

System.out.print(calendar.get(Calendar.MINUTE) + ":");

System.out.println(calendar.get(Calendar.SECOND));

//Set the time and date information and display it. calendar.set(Calendar.HOUR, 10); calendar.set(Calendar.MINUTE, 29); calendar.set(Calendar.SECOND, 22);
System.out.print("Updated time: "); System.out.print(calendar.get(Calendar.HOUR) + ":"); System.out.print(calendar.get(Calendar.MINUTE) + ":"); System.out.println(calendar.get(Calendar.SECOND));

}

}

Output

Date: Jan 1 2007

Time: 11:24:25

Updated time: 10:29:22

Leave a Comment