public class XDate
extends java.util.Date
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
after, before, clone, compareTo, equals, from, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setTime, setYear, toGMTString, toInstant, toLocaleString, toString, UTC
public XDate()
public XDate(long millis)
millis
- the millisecondspublic XDate(java.util.Date date)
date
- the datepublic XDate convertToTimeZone(java.util.TimeZone tz)
// 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")));
tz
- the time zone to convert the date toformatInTimeZone(java.lang.String, java.util.TimeZone)
public java.lang.String formatInTimeZone(java.lang.String dateFormat, java.util.TimeZone tz)
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);
dateFormat
- the date format string to use, in a form that's
compatible with java.text.SimpleDateFormattz
- the desired time zone