org.deegree.commons.utils.time
Class DateUtils

java.lang.Object
  extended by org.deegree.commons.utils.time.DateUtils

public final class DateUtils
extends Object

Utilities for working with dates.

Many of the methods that convert dates to and from strings utilize the ISO 8601:2004 standard string format yyyy-MM-ddTHH:mm:ss.SSSZ, where

 Symbol   Meaning                 Presentation        Example
 ------   -------                 ------------        -------
 y        year                    (Number)            1996
 M        month in year           (Number)            07
 d        day in month            (Number)            10
 h        hour in am/pm (1˜12)    (Number)            12
 H        hour in day (0˜23)      (Number)            0
 m        minute in hour          (Number)            30
 s        second in minute        (Number)            55
 S        millisecond             (Number)            978
 Z        time zone               (Number)            -0600
 

This class is written to be thread safe. As SimpleDateFormat is not threadsafe, no shared instances are used.

Version:
r304 http://anonsvn.jboss.org/repos/dna/trunk/dna-common/src/main/java/org/jboss/dna/common/util/, $Revision: 30279 $, $Date: 2011-03-30 14:23:50 +0200 (Wed, 30 Mar 2011) $
Author:
Randall Hauch, Oliver Tonnhofer, last edited by: $Author: mschneider $

Method Summary
static String formatISO8601Date(Calendar date)
          Obtain an ISO 8601:2004 string representation of the date given the supplied milliseconds since the epoch.
static String formatISO8601Date(Date date)
          Obtain an ISO 8601:2004 string representation of the supplied date.
static String formatISO8601DateWOMS(Date date)
           
static String formatISO8601DateWOTime(Date date)
           
static String formatISO8601Duration(Duration duration)
          Obtain an ISO 8601:2004 string representation of the duration given.
static String formatISO8601Time(Date date)
          Obtain an ISO 8601:2004 string representation of the supplied date.
static String formatLocaleDate(Date date)
          Return a string representation of the supplied date with the current default locale.
static String formatLocaleDate(Date date, Locale locale)
          Return a string representation of the supplied date with the supplied locale.
static Date parseISO8601Date(String dateString)
          Parse the date contained in the supplied string and return a UTC Calendar object.
static Duration parseISO8601Duration(String duration)
          Parses ISO8601 duration strings like P1Y2MT5H, PT5M
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseISO8601Date

public static Date parseISO8601Date(String dateString)
                             throws ParseException
Parse the date contained in the supplied string and return a UTC Calendar object. The date must follow one of the standard ISO 8601 formats, of the form datepartTtimepart, where datepart is one of the following forms:

YYYYMMDD
The 4-digit year, the 2-digit month (00-12), and the 2-digit day of the month (00-31). The month and day are optional, but the month is required if the day is given.
YYYY-MM-DD
The 4-digit year, the 2-digit month (00-12), and the 2-digit day of the month (00-31). The month and day are optional, but the month is required if the day is given.
YYYY-Www-D
The 4-digit year followed by 'W', the 2-digit week number (00-53), and the day of the week (1-7). The day of week number is optional.
YYYYWwwD
The 4-digit year followed by 'W', the 2-digit week number (00-53), and the day of the week (1-7). The day of week number is optional.
YYYY-DDD
The 4-digit year followed by the 3-digit day of the year (000-365)
YYYYDDD
The 4-digit year followed by the 3-digit day of the year (000-365)

The timepart consists of one of the following forms that contain the 2-digit hour (00-24), the 2-digit minutes (00-59), the 2-digit seconds (00-59), and the 1-to-3 digit milliseconds. The minutes, seconds and milliseconds are optional, but any component is required if it is followed by another component (e.g., minutes are required if the seconds are given).

hh:mm:ss.SSS
hhmmssSSS

followed by one of the following time zone definitions:

Z
The uppercase or lowercase 'Z' to denote UTC time
±hh:mm
The 2-digit hour and the 2-digit minute offset from UTC
±hhmm
The 2-digit hour and the 2-digit minute offset from UTC
±hh
The 2-digit hour offset from UTC
hh:mm
The 2-digit hour and the 2-digit minute offset from UTC
hhmm
The 2-digit hour and the 2-digit minute offset from UTC
hh
The 2-digit hour offset from UTC

Parameters:
dateString - the string containing the date to be parsed
Returns:
the parsed date as a Calendar object. The return value is always in UTC time zone. Conversion occurs when necessary.
Throws:
ParseException - if there is a problem parsing the string

parseISO8601Duration

public static Duration parseISO8601Duration(String duration)
                                     throws ParseException
Parses ISO8601 duration strings like P1Y2MT5H, PT5M

Parameters:
duration -
Returns:
a new duration
Throws:
ParseException

formatISO8601Date

public static String formatISO8601Date(Date date)
Obtain an ISO 8601:2004 string representation of the supplied date.

Parameters:
date - the date
Returns:
the string in the standard format

formatISO8601DateWOMS

public static String formatISO8601DateWOMS(Date date)
Parameters:
date -
Returns:
the date string WithOutMilliSeconds

formatISO8601DateWOTime

public static String formatISO8601DateWOTime(Date date)
Parameters:
date -
Returns:
the date string without time

formatISO8601Time

public static String formatISO8601Time(Date date)
Obtain an ISO 8601:2004 string representation of the supplied date.

Parameters:
date - the date
Returns:
the string representation (only time)

formatISO8601Date

public static String formatISO8601Date(Calendar date)
Obtain an ISO 8601:2004 string representation of the date given the supplied milliseconds since the epoch.

Parameters:
date - the date in calendar form
Returns:
the string in the standard format

formatISO8601Duration

public static String formatISO8601Duration(Duration duration)
Obtain an ISO 8601:2004 string representation of the duration given.

Parameters:
duration -
Returns:
the duration string (eg. P1Y3M, PT6H30M, ...)

formatLocaleDate

public static String formatLocaleDate(Date date)
Return a string representation of the supplied date with the current default locale.

Parameters:
date -
Returns:
the string in locale format

formatLocaleDate

public static String formatLocaleDate(Date date,
                                      Locale locale)
Return a string representation of the supplied date with the supplied locale.

Parameters:
date -
locale -
Returns:
the string in locale format


Copyright © 2011. All Rights Reserved.