Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 01 Apr 2016
Calender is an abstract class. It provides a set of methods to manipulate the date and time. The subclass of calender class provides the specific implementation to the abstract methods defined by calender to meet their own requirements.

For example Java GregorianCalendar is a subclass of calender class. The calender class does not define its constructor. Therefore an object of an abstract calender class can’t be created. The calender defines several methods. some commonly used methods are as follows:

Calender Methods:
Method Description
static Calender getInstance() This method returns the object of calender class for the default location and time zone.
Int get(int const) This method returns the value of the requested component of the invoking object. This component is specified be ‘const’. It can be any one of the integer constants. For example Calender.DATE, Calender.HOUR etc.
Void set(int const,int value) This method sets the value of the date or time component specified by const.This const must be one of the constant defined by calender class. For example Calender.HOUR.
final void set(int year, int month, int dayOfMonth) Sets various date and time components of the invoking object.
final void set(int year, int month, int dayOfMonth, int hours, int minutes) Sets various date and time components of the invoking object.
final void set(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) Sets various date and time components of the invoking object.
TimeZonegetTimeZone()
Void setTimeZone(TimeZone+zObj)
These methods get and set the time zone for the invoking object. +zObj parameter of setTimeZone() method specifies the TimeZone object to be set.
Boolean equals(Object calObj) This method returns true if both the invoking calender object and the one specified by calObj Object. Otherwise returns false.
Boolean after(Object calObj) This method returns true if the invoking calender object contains a date that comes after the date specified by calObj object. Otherwise returns false.
Boolean before(Object calObj) This method returns true if the invoking calender object contains a date that comes before the date specified by calObj object. Otherwise returns false.

Calendar defines the following int constants, which are used when you get or set components of the calendar:
ALL_STYLESFRIDAYPM
AMHOURSATURDAY
AM_PMHOUR_OF_DAYSECOND
APRILJANUARYSEPTEMBER
AUGUSTJULYSHORT
DATEJUNESUNDAY
DAY_OF_MONTHLONGTHURSDAY
DAY_OF_WEEKMARCHTUESDAY
DAY_OF_WEEK_IN_MONTHMAYUNDECIMBER
DAY_OF_YEARMILLISECONDWEDNESDAY
DECEMBERMINUTEWEEK_OF_MONTH
DST_OFFSETMONDAYWEEK_OF_YEAR
ERAMONTHYEAR
FEBRUARYNOVEMBERZONE_OFFSET
FIELD_COUNTOCTOBER
DateAndTime
import java.util.Calendar;

class ExampleOfDateAndTime
{
    public static void main(String arg[])
    {
        String months[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY", "AUG", "SEPT", "OCT", "NOV", "DEC"};
        
        /*Create a calendar initialized with the
        current date and time in the default
        locale and timezone.*/
        Calendar cal = Calendar.getInstance();
        
        //Display current time and date information.
        System.out.println("Date :");
        System.out.print(months[cal.get(java.util.Calendar.MONTH)]);
        System.out.print(" " + cal.get(Calendar.DATE) + " ");// LINE D
        System.out.println(cal.get(Calendar.YEAR));
        
        System.out.println("Current Time: "); // LINE A
        System.out.print(cal.get(Calendar.HOUR) + ":");
        System.out.print(cal.get(Calendar.MINUTE) + ":");
        System.out.println(cal.get(Calendar.SECOND));
        
        //Set the time and date information and display it.
        cal.set(Calendar.HOUR, 10); // LINE C
        cal.set(Calendar.MINUTE, 29); // LINE C
        cal.set(Calendar.SECOND, 22); // LINE C
        
        System.out.println("Updated time"); // LINE B
        System.out.print(cal.get(Calendar.HOUR) + ":");
        System.out.print(cal.get(Calendar.MINUTE) + ":");
        System.out.println(cal.get(Calendar.SECOND));
    
    }
}
OUTPUT

Date :
APR 9 2014
Current Time:
7:40:20
Updated time
10:29:22

DESCRIPTION

In the above program we have created a String array months and stored all the months and we created a Calendar reference cal and invoked all the respective methods at LINE A to print the current date and time later we updated the time to 10:29:22 at LINE B and printed it.

THINGS TO TRY
  • Set the time at LINE C to 06:06:06 as shown below and print it.
    cal.set(Calendar.HOUR, 06);
         cal.set(Calendar.MINUTE, 06);
        cal.set(Calendar.SECOND, 06);
  • At LINE D
    System.out.println(" "+ cal.get(Calendar.DATE)+ " ");
    If we miss the space in between the double quotes it shows an error called empty character literal (or) unclosed character literal. check out this.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.