Rakesh API

org.rakeshv.utils
Class Calendar

java.lang.Object
  extended by java.util.Calendar
      extended by java.util.GregorianCalendar
          extended by org.rakeshv.utils.Calendar
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.util.Calendar>

public class Calendar
extends java.util.GregorianCalendar

A wrapper around java.util.GregorianCalendar. Setting the TimeZone of this Calendar will automatically adjust the underlying Date by offsetting from the existing TimeZone.

Java always represents the time in UTC. This can sometimes be incovenient especially when dealing with RDBMS systems, where fetching a date/time/timestamp value will return a value with the millisecond value that represents the value in current TimeZone as opposed to UTC. This class can be used to conveniently correct the internal time represented.

The following code snippet shows a way of using this class:

 
    import java.util.Calendar;
    import java.util.TimeZone;

        Calendar local = org.rakeshv.utils.Calendar.getInstance();
        System.out.println( "local " + local.getTime() ); 
 
        Calendar utc = org.rakeshv.utils.Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
        System.out.println( "utc " + utc.getTime() );

        Calendar pst = org.rakeshv.utils.Calendar.getInstance( TimeZone.getTimeZone( "America/Los_Angeles" ) );
        System.out.println( "pst " + pst.getTime() );
        pst.setTimeZone( TimeZone.getDefault() );
        System.out.println( "pst in local " + pst.getTime() );
 

Copyright 2004-2006 Rakesh Vidyadharan

Version:
$Id: Calendar.java,v 1.6 2006/03/11 04:15:14 rakesh Exp $
Author:
Rakesh Vidyadharan 2004 August 18
See Also:
Serialized Form

Field Summary
static int ROUND_AUTO
          A contant used to indicate that the MINUTE of this calendar is to be rounded automatically using normal rounding rules to the nearest multiple of 5.
static int ROUND_DOWN
          A contant used to indicate that the MINUTE of this calendar is to be rounded down to the nearest multiple of 5.
static int ROUND_UP
          A contant used to indicate that the MINUTE of this calendar is to be rounded up to the nearest multiple of 5.
 
Fields inherited from class java.util.GregorianCalendar
AD, BC
 
Fields inherited from class java.util.Calendar
AM, AM_PM, APRIL, areFieldsSet, AUGUST, DATE, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_WEEK_IN_MONTH, DAY_OF_YEAR, DECEMBER, DST_OFFSET, ERA, FEBRUARY, FIELD_COUNT, fields, FRIDAY, HOUR, HOUR_OF_DAY, isSet, isTimeSet, JANUARY, JULY, JUNE, MARCH, MAY, MILLISECOND, MINUTE, MONDAY, MONTH, NOVEMBER, OCTOBER, PM, SATURDAY, SECOND, SEPTEMBER, SUNDAY, THURSDAY, time, TUESDAY, UNDECIMBER, WEDNESDAY, WEEK_OF_MONTH, WEEK_OF_YEAR, YEAR, ZONE_OFFSET
 
Constructor Summary
Calendar()
          Constructs a Calendar with the default time zone and locale.
Calendar(java.util.Locale locale)
          Constructs a Calendar based on the current time in the default time zone with the given locale.
Calendar(java.util.TimeZone timeZone)
          Constructs a Calendar based on the current time in the given time zone with the default locale.
Calendar(java.util.TimeZone timeZone, java.util.Locale locale)
          Constructs a calendar with the specified time zone and locale.
 
Method Summary
static java.util.Calendar getInstance()
          Gets a calendar using the default time zone and locale.
static java.util.Calendar getInstance(java.util.Locale locale)
          Gets a calendar using the default time zone and specified locale.
static java.util.Calendar getInstance(java.util.TimeZone timeZone)
          Gets a calendar using the specified time zone and default locale.
static java.util.Calendar getInstance(java.util.TimeZone timeZone, java.util.Locale locale)
          Gets a calendar with the specified time zone and locale.
 Calendar roundMinutes(int minInterval, int rounding)
          Round the MINUTE part of this calendar to the nearest 5 minute mark.
 void set(int field, int value)
          Sets the given calendar field to the given value.
 void setTimeZone(java.util.TimeZone timeZone)
          Sets the time zone with the given time zone value.
 
Methods inherited from class java.util.GregorianCalendar
add, clone, computeFields, computeTime, equals, getActualMaximum, getActualMinimum, getGreatestMinimum, getGregorianChange, getLeastMaximum, getMaximum, getMinimum, getTimeZone, hashCode, isLeapYear, roll, roll, setGregorianChange
 
Methods inherited from class java.util.Calendar
after, before, clear, clear, compareTo, complete, get, getAvailableLocales, getFirstDayOfWeek, getMinimalDaysInFirstWeek, getTime, getTimeInMillis, internalGet, isLenient, isSet, set, set, set, setFirstDayOfWeek, setLenient, setMinimalDaysInFirstWeek, setTime, setTimeInMillis, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ROUND_UP

public static final int ROUND_UP
A contant used to indicate that the MINUTE of this calendar is to be rounded up to the nearest multiple of 5. Used by the roundMinutes(int, int) method. 1

See Also:
Constant Field Values

ROUND_DOWN

public static final int ROUND_DOWN
A contant used to indicate that the MINUTE of this calendar is to be rounded down to the nearest multiple of 5. Used by the roundMinutes(int, int) method. 2

See Also:
Constant Field Values

ROUND_AUTO

public static final int ROUND_AUTO
A contant used to indicate that the MINUTE of this calendar is to be rounded automatically using normal rounding rules to the nearest multiple of 5. Used by the roundMinutes(int, int) method. 3

See Also:
Constant Field Values
Constructor Detail

Calendar

public Calendar()
Constructs a Calendar with the default time zone and locale. Just invokes the super class constructor.


Calendar

public Calendar(java.util.TimeZone timeZone)
Constructs a Calendar based on the current time in the given time zone with the default locale. The time for the calendar is set to the equivalent for the specified time zone based upon the system time in the system time zone.

Parameters:
timeZone - - The TimeZone to use.
See Also:
setTimeZone( TimeZone )

Calendar

public Calendar(java.util.Locale locale)
Constructs a Calendar based on the current time in the default time zone with the given locale.

Parameters:
locale - - The Locale to use.

Calendar

public Calendar(java.util.TimeZone timeZone,
                java.util.Locale locale)
Constructs a calendar with the specified time zone and locale. The time for the calendar is set to the equivalent for the specified time zone based upon the system time in the system time zone.

Parameters:
timeZone - - The TimeZone to use.
locale - - The Locale to use.
See Also:
setTimeZone( TimeZone )
Method Detail

getInstance

public static java.util.Calendar getInstance()
Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

Returns:
java.util.Calendar - A new instance of Calendar.
See Also:
Calendar()

getInstance

public static java.util.Calendar getInstance(java.util.TimeZone timeZone)
Gets a calendar using the specified time zone and default locale. The Calendar returned is based on the current time in the given time zone corresponding to the system time in the system time zone with the default locale.

Parameters:
timeZone - - The TimeZone to use.
Returns:
java.util.Calendar - A new instance of Calendar.
See Also:
Calendar( TimeZone )

getInstance

public static java.util.Calendar getInstance(java.util.Locale locale)
Gets a calendar using the default time zone and specified locale. The Calendar returned is based on the current time in the default time zone with the given locale.

Parameters:
locale - - The Locale to use.
Returns:
java.util.Calendar - A new instance of Calendar.
See Also:
Calendar( Locale )

getInstance

public static java.util.Calendar getInstance(java.util.TimeZone timeZone,
                                             java.util.Locale locale)
Gets a calendar with the specified time zone and locale. The Calendar returned is based on the current time in the given time zone corresponding to the system time in the sytem time zone with the given locale.

Parameters:
timeZone - - The TimeZone to use.
locale - - The Locale to use.
Returns:
java.util.Calendar - A new instance of Calendar.
See Also:
Calendar( TimeZone, Locale )

setTimeZone

public void setTimeZone(java.util.TimeZone timeZone)
Sets the time zone with the given time zone value. The time value is modified by the appropriate offset based upon the current time zone.

Overrides:
setTimeZone in class java.util.GregorianCalendar

set

public void set(int field,
                int value)
Sets the given calendar field to the given value. Over-ridden to force HOUR and HOUR_OF_DAY to apply proper correction when the timeZone is not the local timeZone.

Overrides:
set in class java.util.Calendar
Parameters:
field - The given calendar field.
value - The value to be set for the given calendar field.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the specified field is out of range (field < 0 || field >= FIELD_COUNT). In non-lenient mode.

roundMinutes

public Calendar roundMinutes(int minInterval,
                             int rounding)
Round the MINUTE part of this calendar to the nearest 5 minute mark. This is useful for creating drop-down menus in user interface components.

Parameters:
minInterval - The interval to use for rounding. The value must be at least 5 (it will be corrected to 5 otherwise), and should be a multiple of 5 (will be corrected to a multiple of 5 otherwise).
rounding - A constant used to indicate the direction in which the rounding is to be performed. Specify one of ROUND_UP, ROUND_DOWN, or ROUND_AUTO.
Returns:
Calendar Returns a reference to this object.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan