GregorianCalender class to working with date and time

GregorianCalender class to working with date and time


Posted in : Core Java Posted on : October 9, 2010 at 5:09 PM Comments : [ 0 ]

This section contains the detail about the GregorianCalender class in java to working with date and time.

GregorianCalender class to working with date and time

A set of method is provided by abstract calendar class that allows you to convert a time in milliseconds to a number of useful information such as year, month, day, hour, minute and second.

The  subclasses of 'calendar' will provide the specific functionality to interpret time information according to their own rule. An example of such a class is GregorianCalender.

The 'calendar' class method "getInstance()" returns a 'GregorianCalender' initialized with current date and time in the default locale and time zone. 'Gregoriancalendar ' define two fields : AD and BC. These represent the two eras defined by the 'Gregorian calender'.

There are also many constructors for GregorianCalendar objects :

  Constructor         Description      
GregorianCalendar() It constructs a default GregorianCalendar using the
current time in the default time zone with the default locale.
GregorianCalendar(int year, int month, int date)     It constructs a GregorianCalendar with the given date
set in the default time zone with the default locale.
GregorianCalendar(int year, int month, int date,
 int hour, int minute)
It constructs a GregorianCalendar with the given date and time
 set for the default time zone with the default locale.
GregorianCalendar(int year, int month, int date,
int hour, int minute, int second)
It constructs a GregorianCalendar with the given date and time
set for the default time zone with the default locale.
GregorianCalendar(Locale aLocale) It constructs a GregorianCalendar based on the current time in
the default time zone with the given locale.
GregorianCalendar(TimeZone zone) It constructs a GregorianCalendar based on the current time in
the given time zone with the default locale.
GregorianCalendar(TimeZone zone, Locale aLocale) It constructs a GregorianCalendar based on the current time in
the given time zone with the given locale.

Given below few useful methods provided by the GregorianCalendar class :

  Methods     Description   
void add(int field, int amount) This method adds the specified (signed) amount of time to the given time field,
based on the calendar's rules.
protected void computeFields()  This method converts UTC as milliseconds to time field values.
protected void computeTime() This method overrides Calendar Converts time field values to UTC as milliseconds.
boolean equals(Object obj) This method compares this GregorianCalendar to an object reference.
int get(int field) This method gets the value for a given time field.
int getActualMaximum(int field) This method return the maximum value that this field could have, given the current date.
int getActualMinimum(int field) This method return the minimum value that this field could have, given the current date.
int getGreatestMinimum(int field) This method returns highest minimum value for the given field if varies.
Date getGregorianChange() This method gets the Gregorian Calendar change date.
int getLeastMaximum(int field) This method returns lowest maximum value for the given field if varies.
int getMaximum(int field) This method returns maximum value for the given field.
Date getTime() This method gets this Calendar's current time.
long getTimeInMillis() This method gets this Calendar's current time as a long.
TimeZone getTimeZone() This method gets the time zone.
int getMinimum(int field) This method returns minimum value for the given field.
int hashCode()  This method override hashCode.
boolean isLeapYear(int year) This method determines if the given year is a leap year.
void roll(int field, boolean up)  This method adds or subtracts (up/down) a single unit of time on the given time field
without changing larger fields.
void set(int field, int value) This method sets the time field with the given value.
void set(int year, int month, int date) This method sets the values for the fields year, month, and date.
void set(int year, int month, int date, int hour, int minute) This method sets the values for the fields year, month, date, hour, and minute.
void set(int year, int month, int date, int hour, int minute, int second) This method sets the values for the fields year, month, date, hour, minute, and second.
void setGregorianChange(Date date) This method sets the GregorianCalendar change date.
void setTimeInMillis(long millis)  This method sets this Calendar's current time from the given long value.
void setTimeZone(TimeZone value) This method sets the time zone with the given time zone value.
String toString() This method return a string representation of this calendar.
void setTime(Date date) This method sets this Calendar's current time with the given Date.

Example :


import java.util.*;

public class GregorianCalendarDemo {
	public static void main(String args[]) {
	String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
			"Aug", "Sep", "Oct", "Nov", "Dec" };

	int year;
	GregorianCalendar gcalendar = new GregorianCalendar();

	System.out.print("Date: ");
	System.out.print(months[gcalendar.get(Calendar.MONTH)]);
	System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
	System.out.println(year = gcalendar.get(Calendar.YEAR));
	System.out.print("Time: ");
	System.out.print(gcalendar.get(Calendar.HOUR) + ":");
	System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
	System.out.println(gcalendar.get(Calendar.SECOND));

	// Test if the current year is a leap year
	if (gcalendar.isLeapYear(year)) {
		System.out.println("The current year is a leap year");
	} else {
		System.out.println("The current year is not a leap year");
	}
  }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac GregorianCalendarDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java GregorianCalendarDemo
Date: Oct 9 2010
Time: 2:47:18
The current year is not a leap year

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics