org.clapper.util.misc
Class XDate

java.lang.Object
  extended by java.util.Date
      extended by org.clapper.util.misc.XDate
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<java.util.Date>

public class XDate
extends java.util.Date

Version of java.util.Date that provides some extra utility functions.

Version:
$Revision: 6735 $
Author:
Copyright © 2006 Brian M. Clapper
See Also:
Serialized Form

Constructor Summary
XDate()
          Create a new XDate so that it represents the time the object was constructed, measure to the nearest millisecond.
XDate(java.util.Date date)
          Create a new XDate object and initialize it to represent the time contained in the specified, existing Date object (which may, itself, be an XDate).
XDate(long millis)
          Create a new XDate object and initialize it to represent the specified number of milliseconds since the epoch.
 
Method Summary
 XDate convertToTimeZone(java.util.TimeZone tz)
          Convert this date from its time zone to another.
 java.lang.String formatInTimeZone(java.lang.String dateFormat, java.util.TimeZone tz)
          Convenience method to produce a printable date in a specified time zone, using a SimpleDateFormat.
 
Methods inherited from class java.util.Date
after, before, clone, compareTo, equals, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setTime, setYear, toGMTString, toLocaleString, toString, UTC
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XDate

public XDate()
Create a new XDate so that it represents the time the object was constructed, measure to the nearest millisecond.


XDate

public XDate(long millis)
Create a new XDate object and initialize it to represent the specified number of milliseconds since the epoch.

Parameters:
millis - the milliseconds

XDate

public XDate(java.util.Date date)
Create a new XDate object and initialize it to represent the time contained in the specified, existing Date object (which may, itself, be an XDate).

Parameters:
date - the date
Method Detail

convertToTimeZone

public XDate convertToTimeZone(java.util.TimeZone tz)
Convert this date from its time zone to another. Sample use:
 // Convert time in default time zone to UTC

 XDate now = new XDate();
 TimeZone tzUTC = TimeZone.getTimeZone ("UTC");
 Date utc = now.convertToTimeZone (tzUTC);
 DateFormat fmt = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss z");
 fmt.setTimeZone (tzUTC);
 System.out.println (fmt.format (utc));
 

Or, more simply:

 XDate now = new XDate();
 System.out.println (now.formatInTimeZone ("yyyy/MM/dd HH:mm:ss z",
                                           TimeZone.getTimeZone ("UTC")));
 

Parameters:
tz - the time zone to convert the date to
Returns:
a new XDate object, appropriately converted. This result can safely be stored in a java.util.Date reference.
See Also:
formatInTimeZone(java.lang.String, java.util.TimeZone)

formatInTimeZone

public java.lang.String formatInTimeZone(java.lang.String dateFormat,
                                         java.util.TimeZone tz)
Convenience method to produce a printable date in a specified time zone, using a SimpleDateFormat. Calling this method is roughly equivalent to:
 XDate now = new XDate();
 TimeZone tzUTC = TimeZone.getTimeZone ("UTC");
 Date utc = now.convertToTimeZone (tzUTC);
 DateFormat fmt = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss z");
 fmt.setTimeZone (tzUTC);
 String formattedDate = fmt.format (utc);
 

Parameters:
dateFormat - the date format string to use, in a form that's compatible with java.text.SimpleDateFormat
tz - the desired time zone
Returns:
the formatted date string


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.