com.hunnysoft.jmime
Class DateTime

java.lang.Object
  extended bycom.hunnysoft.jmime.Node
      extended bycom.hunnysoft.jmime.FieldBody
          extended bycom.hunnysoft.jmime.DateTime
All Implemented Interfaces:
java.lang.Cloneable

public final class DateTime
extends FieldBody

Class that represents an RFC 2822 date-time.

DateTime represents an Internet date-time string as described in RFC 2822.

The parse operation for DateTime parses the string representation to extract the year, month, day, hour, minute, second, and time zone. DateTime provides methods to set or get the individual elements of the date-time.

The use of DateTime is best explained by considering the following use cases.

DateTime parses Internet date-time strings. When a MIME message is received, perhaps from a remote source, you may call the inherited method Node.setString(com.hunnysoft.jmime.ByteString), or the DateTime(ByteString) constructor, to set the string representation of the DateTime object. Then call the parse() method to parse the string representation. Finally, call the accessor methods year(), month(), and so on, to access the parsed elements of the date-time string.

DateTime generates Internet date-time strings. Use the default constructor to create a new DateTime object with a null date-time. Call one of the methods setValuesLocal(int, int, int, int, int, int) or setValuesLiteral(int, int, int, int, int, int, int) to set the year, month, day, hour, minute, second, and zone with the desired values. Alternatively, you can set the values for the date-time from a system time, using the fromSystemTime(long, boolean) method. Then call the assemble() method to create the string representation. Finally, call the inherited method Node.string() to get the string representation.

DateTime converts to and from a scalar date and time. Call the fromSystemTime(long, boolean) method or the asSystemTime() method to convert from or to a "system time," defined as the number of milliseconds elapsed since 1 Jan 1970 00:00:00 UTC. The system time is used by the standard Java library; for example, it is returned by the method System.currentTimeMillis(). By converting to a scalar representation of the date and time, Hunny JMIME makes it easy for you to compare two different dates.

Note: It is possible that DateTime will encounter an error while parsing a date-time string. When this happens, throwing an exception is not a wise way to proceed, since in most cases higher level operations would also have to terminate before the library user's code could deal with the exception. What actually happens, is that DateTime "recovers" from the error by setting the date and time to 1 Jan 1970 00:00:00 UTC. To easily check whether a parse operation completed successfully, you can call asSystemTime() and examine the return value. A value of zero indicates that the parsing operation failed; any other value indicates it succeeded.


Field Summary
 
Fields inherited from class com.hunnysoft.jmime.FieldBody
mText
 
Fields inherited from class com.hunnysoft.jmime.Node
mIsModified, mParent, mString
 
Constructor Summary
DateTime()
          Default constructor.
DateTime(ByteString bstr)
          Constructor that takes an initial byte string argument.
DateTime(DateTime other)
          Copy constructor.
 
Method Summary
 void assemble()
          Assembles the string representation.
 long asSystemTime()
          Gets the date and time as a system time.
 java.lang.Object clone()
          Creates a copy of this object.
 int day()
          Gets the day of the month.
 int dayOfTheWeek()
          Gets the day of the week.
 void fromSystemTime(long time, boolean convertToLocal)
          Sets the date and time from a system time.
 int hour()
          Gets the hour.
 int minute()
          Gets the minute.
 int month()
          Gets the month.
 void parse()
          Parses the string representation.
 int second()
          Gets the second.
 void setValuesLiteral(int year, int month, int day, int hour, int minute, int second, int zoneOffset)
          Sets the date-time value as specified.
 void setValuesLocal(int year, int month, int day, int hour, int minute, int second)
          Sets the date-time value as specified, assuming local time.
 int year()
          Gets the year.
 int zone()
          Gets the zone.
 
Methods inherited from class com.hunnysoft.jmime.FieldBody
fold, isFoldingEnabled, maybeFold, setFoldingEnabled, setText, text, unfold
 
Methods inherited from class com.hunnysoft.jmime.Node
_setString, isModified, parent, setModified, setParent, setString, string, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DateTime

public DateTime()
Default constructor.

This constructor sets the DateTime object's string representation to the empty string and sets its parent node to null. The date-time value is set to the null value, defined as the date 1 Jan 1970 00:00:00 UTC.


DateTime

public DateTime(DateTime other)
Copy constructor.

This constructor performs a deep copy of its argument. The parent node of the new DateTime object is set to null.

Parameters:
other - DateTime instance to copy

DateTime

public DateTime(ByteString bstr)
Constructor that takes an initial byte string argument.

This constructor sets the DateTime object's string representation to bstr and sets its parent node to null. The date-time value is set to the null value, defined as the date 1 Jan 1970 00:00:00 UTC.

Normally, you call the parse() method immediately after this constructor to parse the date and time information from the string representation.

Parameters:
bstr - initial value for the string representation
Method Detail

clone

public java.lang.Object clone()
Creates a copy of this object.

This method, inherited from Object, creates a new DateTime object that has the same value as this DateTime object. The parent node of the new DateTime object is set to null.

Overrides:
clone in class FieldBody
Returns:
a copy of this object

parse

public void parse()
Parses the string representation.

This method, inherited from Node, executes the parse operation for DateTime objects. The parse operation creates or updates the broken-down representation from the string representation. For DateTime objects, the parse method parses the string representation to get the values of the year, month, day, hour, minute, second, and time zone.

You should call this method after you set or modify the string representation, and before you retrieve the year, month, day, hour, minute second, and zone.

This method clears the is-modified flag.

Overrides:
parse in class FieldBody

assemble

public void assemble()
Assembles the string representation.

This method, inherited from Node, executes the assemble operation for DateTime objects. The assemble operation creates or updates the string representation from the broken-down representation. For DateTime objects, the assemble operation builds the string representation from the year, month, day, hour, minute, second, and zone.

You should call this method after you set or modify the year, month, day, hour, minute, second, or zone, and before you retrieve the string representation.

This method clears the is-modified flag.

Overrides:
assemble in class FieldBody

asSystemTime

public long asSystemTime()
Gets the date and time as a system time.

The system time is defined as the number of milliseconds elapsed since 1 Jan 1970 00:00:00 UTC. This is the same date and time representation that is returned by the System.currentTimeMillis() method in the Java library.

Returns:
scalar time value, indicating the number of milliseconds elapsed since 1 Jan 1970 00:00:00 UTC

fromSystemTime

public void fromSystemTime(long time,
                           boolean convertToLocal)
Sets the date and time from a system time.

The system time is defined as the number of milliseconds elapsed since 1 Jan 1970 00:00:00 UTC. This is the same date and time representation that is returned by the System.currentTimeMillis() method in the Java library.

If convertToLocal is true, then the DateTime object creates the internal representation in local time; otherwise, it creates the internal representation in UTC time.

Parameters:
time - a scalar time value, indicating the number of milliseconds elapsed since 1 Jan 1970 00:00:00 UTC
convertToLocal - if true, the internal representation will be in local time; if false, the internal representation will be in UTC

setValuesLocal

public void setValuesLocal(int year,
                           int month,
                           int day,
                           int hour,
                           int minute,
                           int second)
Sets the date-time value as specified, assuming local time.

Use this method to create the internal date-time representation in local time. Time zone information is obtained from the local system, based on the values specified.

Note that when the local system is set up to recognize daylight time, there is an ambiguity during the overlap time period that occurs when the local time clock is set back. During this overlap period, it is impossible to determine whether the time zone offset should be the standard zone offset or the daylight time zone offset. This ambiguity is probably not a problem for most applications. If it is a problem, then the work around is to use setValuesLiteral(int, int, int, int, int, int, int) to specify the precise time zone offset.

Parameters:
year - four digit year (e.g. 2001)
month - month (1 <= month <= 12)
day - day of month (1 <= day <= 31)
hour - hour (0 <= hour <= 23)
minute - minute (0 <= minute <= 59)
second - second (0 <= second <= 60)
See Also:
setValuesLiteral(int, int, int, int, int, int, int)

setValuesLiteral

public void setValuesLiteral(int year,
                             int month,
                             int day,
                             int hour,
                             int minute,
                             int second,
                             int zoneOffset)
Sets the date-time value as specified.

Use this method to create the internal date-time representation in any time zone. The values are used "as is" without any additional interpretation.

zoneOffset is the offset in minutes from Coordinated Universal Time (UTC). For example, the correct value for US Eastern Standard Time (e.g. New York City) is -300, since 12:00 UTC equals 7:00 EST, a difference of 300 minutes.

Parameters:
year - four digit year (e.g. 2001)
month - month (1 <= month <= 12)
day - day of month (1 <= day <= 31)
hour - hour (0 <= hour <= 23)
minute - minute (0 <= minute <= 59)
second - second (0 <= second <= 60)
zoneOffset - difference in minutes between local time and UTC (-720 <= zoneOffset <= 720), e.g. zoneOffset=-300 for US Eastern Standard Time (New York City)
See Also:
setValuesLocal(int, int, int, int, int, int)

year

public int year()
Gets the year.

Returns:
year

month

public int month()
Gets the month.

The month is in the range 1 <= month <= 12, with 1 = January.

Returns:
month

day

public int day()
Gets the day of the month.

The day is in the range 1 <= day <= 31.

Returns:
day of the month

hour

public int hour()
Gets the hour.

The hour is in the range 0 <= hour <= 23.

Returns:
hour

minute

public int minute()
Gets the minute.

The minute is in the range 0 <= minute <= 59.

Returns:
minute

second

public int second()
Gets the second.

In accordance with RFC 2822, the second is in the range 0 <= second <= 60.

Returns:
second

zone

public int zone()
Gets the zone.

The zone is in the range -720 <= zone <= 720, and represents the difference in minutes between local time and UTC. For example, for US Eastern Standard Time (New York City), the zone offset is -300.

Returns:
zone offset

dayOfTheWeek

public int dayOfTheWeek()
Gets the day of the week.

The returned value is in the range (0 <= dayOfWeek <= 6), with 0 = Sunday. This value is always computed from the other values. The day of the week is a read-only value that cannot be set.

Returns:
day of the week