Java2all.com  




Share This Topic -

Calendar & Gregorian class :

The Calendar class allows you to interpret date and time information.

This class defines several integer constants that are used when you get or set components of the calendar. These are listed here.

AM

AM_PM

APRIL

AUGUST

DATE

DAY_OF_MONTH

DAY_OF_WEEK

DAY_OF_WEEK_IN_MONTH

DAY_OF_YEAR

DECEMBER

DST_OFFSET

ERA

FEBRUARY

FIELD_COUNT

FRIDAY

HOUR

HOUR_OF_DAY

JANUARY

JULY

JUNE

MARCH

MAY

MILLISECOND

MINUTE

MONDAY

MONTH

NOVEMBER

OCTOBER

PM

SATURADAY

SECOND

SEPTEMBER

SUNDAY

THURSDAY

TUESDAY

UNDERIMBER

WEDNESDAY

WEEK_OF_MONTH

WEEK_OF_YEAR

YEAR

ZONE_OFFSET

The Calendar class does not have public constructors. Instead, you may use the static getInstance() method to obtain a calendar initialized to the current date and time.

 

One of its forms is shown here:

Calendar getInstance()

EX :

import java.util.Calendar;
 public class Cal1 
{

  public static void main(String[] args) 
  {
    
    Calendar cal = Calendar.getInstance();
    
    System.out.println("DATE is : " + cal.get(cal.DATE));   
    System.out.println("YEAR is : " + cal.get(cal.YEAR));   
    System.out.println("MONTH is : " + cal.get(cal.MONTH));   
    System.out.println("DAY OF WEEK is : " + cal.get(cal.DAY_OF_WEEK));   
    System.out.println("WEEK OF MONTH is : " + cal.get(cal.WEEK_OF_MONTH));   
    System.out.println("DAY OF YEAR is : " + cal.get(cal.DAY_OF_YEAR));   
    System.out.println("DAY OF MONTH is : " + cal.get(cal.DAY_OF_MONTH));   
    System.out.println("WEEK OF YEAR is : " + cal.get(cal.WEEK_OF_YEAR)); 
    System.out.println("HOUR is : " + cal.get(cal.HOUR)); 
    System.out.println("MINUTE is : " + cal.get(cal.MINUTE)); 
    System.out.println("SECOND is : " + cal.get(cal.SECOND));  
    System.out.println("DAY OF WEEK IN MONTH is : " + cal.get(cal.DAY_OF_WEEK_IN_MONTH));  
    System.out.println("Era is : " + cal.get(cal.ERA));
    System.out.println("HOUR OF DAY is : " + cal.get(cal.HOUR_OF_DAY)); 
    System.out.println("MILLISECOND : " + cal.get(cal.MILLISECOND));  
    System.out.println("AM_PM : " + cal.get(cal.AM_PM));// Returns 0 if AM and 1 if PM  
  }
}

 
Output :
 
DATE is : 23
YEAR is : 2012
MONTH is : 9
DAY OF WEEK is : 3
WEEK OF MONTH is : 4
DAY OF YEAR is : 297
DAY OF MONTH is : 23
WEEK OF YEAR is : 43
HOUR is : 2
MINUTE is : 32
SECOND is : 32
DAY OF WEEK IN MONTH is : 4
Era is : 1
HOUR OF DAY is : 14
MILLISECOND : 595
AM_PM : 1
  • The GregorianCalendar class is a subclass of Calendar.
  • It provides the logic to manage date and time information according to the rules of the Gregorian calendar.
  • This class provides following constructors:       

GregorianCalendar()

GregorianCalendar(int year, int month, int date)

GregorianCalendar(int year, int month, int date, int hour, int minute, int sec)

GregorianCalendar(int year, int month, int date, int hour, int minute)

  • The first form creates an object initialized with the current date and time.
  • The other forms allow you to specify how various date and time components are initialized.
  • The class provides all of the method defined by Calendar and also adds the isLeapYear() method shown here:

Boolean isLeapYear()

This method returns true if the current year is a leap year. Otherwise, it returns false.

EX :

import java.util.*;

public class gcal1 
{ 
  public static void main(String[] args) 
  {    
    GregorianCalendar c1 = new GregorianCalendar() ;
     
    System.out.println("Is leap yer " + c1.isLeapYear(c1.get(c1.YEAR))); //here we get current year and check for leap year
    System.out.println("DATE is : " + c1.get(c1.DATE));   
    System.out.println("YEAR is : " + c1.get(c1.YEAR));   
    System.out.println("MONTH is : " + c1.get(c1.MONTH));   
    System.out.println("DAY OF WEEK is : " + c1.get(c1.DAY_OF_WEEK));   
    System.out.println("WEEK OF MONTH is : " + c1.get(c1.WEEK_OF_MONTH));   
    System.out.println("DAY OF YEAR is : " + c1.get(c1.DAY_OF_YEAR));   
    System.out.println("DAY OF MONTH is : " + c1.get(c1.DAY_OF_MONTH));   
    System.out.println("WEEK OF YEAR is : " + c1.get(c1.WEEK_OF_YEAR)); 
    System.out.println("HOUR is : " + c1.get(c1.HOUR)); 
    System.out.println("MINUTE is : " + c1.get(c1.MINUTE)); 
    System.out.println("SECOND is : " + c1.get(c1.SECOND));  
    System.out.println("DAY OF WEEK IN MONTH is : " + c1.get(c1.DAY_OF_WEEK_IN_MONTH));  
    System.out.println("Era is : " + c1.get(c1.ERA));
    System.out.println("HOUR OF DAY is : " + c1.get(c1.HOUR_OF_DAY)); 
    System.out.println("MILLISECOND : " + c1.get(c1.MILLISECOND));  
    System.out.println("AM_PM : " + c1.get(c1.AM_PM));// Returns 0 if AM and 1 if PM  */
  }
}

 
Output :
 
Is leap yer true
DATE is : 23
YEAR is : 2012
MONTH is : 9
DAY OF WEEK is : 3
WEEK OF MONTH is : 4
DAY OF YEAR is : 297
DAY OF MONTH is : 23
WEEK OF YEAR is : 43
HOUR is : 2
MINUTE is : 37
SECOND is : 26
DAY OF WEEK IN MONTH is : 4
Era is : 1
HOUR OF DAY is : 14
MILLISECOND : 939
AM_PM : 1