Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : C words : calendar.
For newbies, dealing with dates and times are the probably the most confusing aspect of Java. There are three reasons for this:
| class | Purpose |
|---|---|
| com.mindprod.business.BigDate | A simpler date class for pure date calculations when you don't want the complication of TimeZones and times. Not part of Sun's libraries. You can download it from http://mindprod.com/products.html#BIGDATE |
| java.text.DateFormat | Used to convert a date to or from a String. Contains an associated TimeZone. |
| java.text.SimpleDateFormat | Used to convert a date to or from a String when you want precise control over the format. Contains an associated TimeZone. |
| java.util.Calendar | Abstract class that is the mother of all Calendars such as GregorianCalendar. It owns the dozens of magic date constants such as Calendar.JANUARY = 0; Calendar.SUNDAY = 1; and Calendar.YEAR = 1. |
| java.util.Date | Sun's first attempt at a Date class. I refer to it as the lemon of Java. It is now almost completely deprecated. It now just basically just a wrapper around a UTC date/timestamp long milliseconds since 1970. Unfortunately, it is still not completely gone. |
| java.util.GregorianCalendar | Used to do date calculations. Each GregorianCalendar contains a UTC timestamp, and a TimeZone. |
| java.util.TimeZone | Contains the name of a timezone and how many hours difference from UTC that timezone is. It also contains the rules for when daylight savings begins and ends. Timezones are named after cities. They are not the usual names. |
import java.text.DateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; /** * display inauguration day as a String */ public class Inauguration1 { static public void main (String [] args) { // inauguration day is Thursday 2005 January 20 noon Eastern Standard Time. TimeZone est = TimeZone.getTimeZone ( "America/New_York" ); GregorianCalendar inauguration = new GregorianCalendar ( est ); inauguration.set( 2005, Calendar.JANUARY, 20 , 12, 0, 0 ); // locale specific: e.g.Jan 20, 2005 DateFormat df = DateFormat.getDateInstance(); // set timezone df.setCalendar( inauguration ); // set timestamp String dateString = df.format( inauguration.getTime()); System.out.println( dateString ); } }
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; // display inauguration day as a String public class Inauguration2 { static public void main (String [] args) { // inauguration day is Thursday 2005 January 20 noon Eastern Standard Time. TimeZone est = TimeZone.getTimeZone ( "America/New_York" ); GregorianCalendar inauguration = new GregorianCalendar ( est ); inauguration.set(2005 , Calendar.JANUARY, 20, 12, 0, 0 ); // mask for: Thursday 2005/01/20 12:00:00 PM EST : Eastern Standard Time // See below for link to description of all possible mask characters // and their meanings. SimpleDateFormat sdf = new SimpleDateFormat ( "EEEE yyyy/MM/dd hh:mm:ss aa zz : zzzzzz" ); // set timezone sdf.setCalendar( inauguration ); // set timestamp String dateString = sdf.format( inauguration.getTime() ); System.out.println( dateString ); } }
Notes:
import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; // Hours until the next presidential inauguration public class Inauguration4 { static final int MILLIS_PER_HOUR = 60 * 60 * 1000; static public void main ( String [] args ) { // inauguration day is Thursday 2005 January 20 noon Eastern Standard Time. TimeZone est = TimeZone.getTimeZone ( "America/New_York" ); GregorianCalendar inauguration = new GregorianCalendar ( est ); inauguration.set( 2005 , Calendar.JANUARY, 20, 12, 0, 0 ); // now is current time, using default timezone GregorianCalendar now = new GregorianCalendar(); // milliseconds since 1970 Jan 1 long epochInauguration = inauguration.getTime().getTime(); long epochNow = now.getTime().getTime(); double hours = (double) ( epochInauguration - epochNow ) /MILLIS_PER_HOUR; System.out.println( hours + " hours until the inauguration." ); } }
Notes:
//taking a timestamp apart using default timezone. GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis( timestamp ); int year = cal.get( Calendar.YEAR ); int month = cal.get( Calendar.MONTH ) + 1; int day = cal.get( Calendar.DAY_OF_MONTH ); int hour24 = cal.get( Calendar.HOUR_OF_DAY ); int hour12 = cal.get( Calendar.HOUR ); int amIs0OrPmIs1 = cal.get( Calendar.AM_PM ); int minute = cal.get( Calendar.MINUTE ); int second = cal.get( Calendar.SECOND );
// buiding a timestamp using default timezone. GregorianCalendar stamp = new GregorianCalendar(); stamp.clear(); stamp.set( year, month-1, day, hour, minute, second ); longtimestamp = stamp.getTimeInMillis();
gc = new GregorianCalendar(); gc.setLenient( false ); /* Bug only manifests if lenient set false */ gc.set( 2001, 1, 1, 1, 0, 0 ); nYear = gc.get ( Calendar.YEAR ); /* throws exception */
The bug disappears at 7AM on 2001/01/01 for MST.
Dealing with pure dates is much simpler using the BigDate class. You can download it from http://mindprod.com/products.html#BIGDATE
Here is a list of available timezones:
Add column 1 in hours to UTC to get local standard time.
Add column 2 in hours to UTC to get local daylight saving time.
Use UTC when you want no timezone at all.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 26248. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/calendar.html | J:\mindprod\jgloss\calendar.html | |||