com.day.cq.commons.date
Class DateUtil

java.lang.Object
  extended by com.day.cq.commons.date.DateUtil

public class DateUtil
extends Object

DateUtil is a utility class that provides easy access to commonly used dates and for parsing/reading ISO8601 date strings.


Constructor Summary
DateUtil()
          Initializes a DateUtil with the first day of week being the Monday.
DateUtil(int firstDayOfWeek)
          Initializes a DateUtil with a custom first day of the week.
 
Method Summary
static String getISO8601Date(Calendar calendar)
          Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
static String getISO8601Date(Date date)
          Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
static String getISO8601Date(Date date, TimeZone timeZone)
          Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".
static String getISO8601DateAndTimeNoMillis(Calendar calendar)
          Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
static String getISO8601DateAndTimeNoMillis(Date date)
          Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
static String getISO8601DateAndTimeNoMillis(Date date, TimeZone timeZone)
          Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".
static String getISO8601DateNoTime(Calendar calendar)
          Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD"
static String getISO8601DateNoTime(Date date)
          Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".
static String getISO8601DateNoTime(Date date, TimeZone timeZone)
          Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD".
 Calendar getLastYearStart()
           
 Calendar getMonthStart()
           
 Calendar getNow()
           
 Calendar getThreeMonthsAgo()
           
 Calendar getToday()
           
 Calendar getWeekStart()
           
 Calendar getYearStart()
           
static Calendar parseISO8601(String iso8601Date)
          Parse the given string in ISO 8601 format and build a Calendar object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtil

public DateUtil()
Initializes a DateUtil with the first day of week being the Monday.


DateUtil

public DateUtil(int firstDayOfWeek)
Initializes a DateUtil with a custom first day of the week.

Parameters:
firstDayOfWeek - the first day of the week.
See Also:
Calendar.DAY_OF_WEEK
Method Detail

getLastYearStart

public final Calendar getLastYearStart()
Returns:
calendar that represents the beginning of the last year.

getYearStart

public final Calendar getYearStart()
Returns:
calendar that represents the beginning of this year.

getThreeMonthsAgo

public final Calendar getThreeMonthsAgo()
Returns:
calendar that represents the date three months ago.

getMonthStart

public final Calendar getMonthStart()
Returns:
calendar that represents the beginning of this month.

getWeekStart

public final Calendar getWeekStart()
Returns:
calendar that represents the start of this week. Depends on #setFirstDayOfWeek(int)!

getToday

public final Calendar getToday()
Returns:
calendar that represents the current date.

getNow

public final Calendar getNow()
Returns:
calendar that represents the current time.

parseISO8601

public static Calendar parseISO8601(String iso8601Date)
                             throws InvalidDateException
Parse the given string in ISO 8601 format and build a Calendar object.

Parameters:
iso8601Date - the date in ISO 8601 format
Returns:
a Calendar instance
Throws:
InvalidDateException - if the date string is not valid

getISO8601Date

public static String getISO8601Date(Calendar calendar)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ".

Parameters:
calendar - a Calendar instance
Returns:
a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"

getISO8601Date

public static String getISO8601Date(Date date)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).

Parameters:
date - a Date instance representing a UTC time
Returns:
a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"

getISO8601Date

public static String getISO8601Date(Date date,
                                    TimeZone timeZone)
Generate a full ISO 8601 date: "YYYY-MM-DDTHH:mm:ss.SSSZ". It uses the given timezone for interpreting the date value.

Parameters:
date - a Date instance
timeZone - the timeZone of the date
Returns:
a string representing the date in the full ISO 8601 format: "YYYY-MM-DDTHH:mm:ss.SSSZ"

getISO8601DateAndTimeNoMillis

public static String getISO8601DateAndTimeNoMillis(Calendar calendar)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ".

Parameters:
calendar - a Calendar instance
Returns:
a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"

getISO8601DateAndTimeNoMillis

public static String getISO8601DateAndTimeNoMillis(Date date)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).

Parameters:
date - a Date instance representing a UTC time
Returns:
a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"

getISO8601DateAndTimeNoMillis

public static String getISO8601DateAndTimeNoMillis(Date date,
                                                   TimeZone timeZone)
Generate a ISO 8601 date with date and time, but without the milliseconds part: "YYYY-MM-DDTHH:mm:ssZ". It uses the given timezone for interpreting the date value.

Parameters:
date - a Date instance
timeZone - the timeZone of the date
Returns:
a string representing date and time except the milliseconds in the ISO 8601 format: "YYYY-MM-DDTHH:mm:ssZ"

getISO8601DateNoTime

public static String getISO8601DateNoTime(Calendar calendar)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD"

Parameters:
calendar - a Calendar instance
Returns:
a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"

getISO8601DateNoTime

public static String getISO8601DateNoTime(Date date)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD". It assumes the timezone of the date to be GMT (or UTC, "Z" at the end of ISO8601).

Parameters:
date - a Date instance representing a UTC time.
Returns:
a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"

getISO8601DateNoTime

public static String getISO8601DateNoTime(Date date,
                                          TimeZone timeZone)
Generate a ISO 8601 date in the pure date form: "YYYY-MM-DD". It uses the given timezone for interpreting the date value.

Parameters:
date - a Date instance representing a date
timeZone - the timeZone of the date
Returns:
a string representing only the date in the ISO 8601 format: "YYYY-MM-DD"


Copyright © 2009 Day Management AG. All Rights Reserved.