Ads

Tuesday, 31 October 2017

java.util.GregorianCalendar class in java


GregorianCalendar Class


This class is a concrete implementation of a Calendar class that implements the normal Gregorian calendar with which you are familiar.

The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone.

Constructors


GregorianCalendar()
Constructs a default GregorianCalendar using the current time in the default time zone with the default locale.


GregorianCalendar(int year, int month, int date)
Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

Methods


boolean isLeapYear(int year)

This is a useful method of GregorianCalendar.
Determines if the given year is a leap year.

void add(int field, int amount)
Adds the specified (signed) amount of time to the given time field, based on the calendar's rules.

protected void computeFields()

Converts UTC as milliseconds to time field values.

protected void computeTime()

Overrides Calendar Converts time field values to UTC as milliseconds.

boolean equals(Object obj)

Compares this GregorianCalendar to an object reference.

int get(int field)

Gets the value for a given time field.

int getActualMaximum(int field)

Returns the maximum value that this field could have, given the current date.

int getActualMinimum(int field)

Returns the minimum value that this field could have, given the current date.

int getGreatestMinimum(int field)

Returns the highest minimum value for the given field if varies.

Date getGregorianChange()

Gets the Gregorian Calendar change date.

int getLeastMaximum(int field)

Returns the lowest maximum value for the given field if varies.

int getMaximum(int field)

Returns maximum value for the given field.

Date getTime()

Gets this Calendar's current time.

long getTimeInMillis()

Gets this Calendar's current time as a long.

TimeZone getTimeZone()

Gets the time zone.

int getMinimum(int field)

Returns minimum value for the given field.



Example


import java.util.*;
class Caldemo
{
    public static void main(String args[])
    {
         Calendar cal=Calendar.getInstance();
         System.out.println("Date"+cal.get(Calendar.DATE);
         System.out.println("Month"+cal.get(Calendar.MONTH);
         cal.set(Calendar.MONTH,6);
         System.out.println("Month:"+cal.get(Calendar.MONTH);
         Date d=cal.getTime();
         System.out.println(d);
         cal.clear(Calendar.MONTH);
         System.out.println("MONTH"+cal.get(Calendar.MONTH));
         GregorianCalendar ge=new GregorianCalendar();
         System.out.println(ge.isLeapYear(2016));
     }
}
        



 
Post a Comment

Click Here