freemarker.template.utility
Class DateUtil

java.lang.Object
  extended by freemarker.template.utility.DateUtil

public class DateUtil
extends java.lang.Object


Nested Class Summary
static interface DateUtil.DateToISO8601CalendarFactory
          Used internally by DateUtil; don't use it's implementations for anything else.
static class DateUtil.TrivialDateToISO8601CalendarFactory
          Non-thread-safe factory that hard-references a calendar internally.
 
Field Summary
static int ACCURACY_HOURS
           
static int ACCURACY_MILLISECONDS
           
static int ACCURACY_MINUTES
           
static int ACCURACY_SECONDS
           
static java.util.TimeZone UTC
           
 
Method Summary
static java.lang.String dateToISO8601String(java.util.Date date, boolean datePart, boolean timePart, boolean offsetPart, int accuracy, java.util.TimeZone timeZone, DateUtil.DateToISO8601CalendarFactory calendarFactory)
          Format a date, time or date+time with one of the ISO 8601 extended formats.
static java.util.TimeZone getTimeZone(java.lang.String name)
          Returns the time zone object for the name (or ID).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACCURACY_HOURS

public static final int ACCURACY_HOURS
See Also:
Constant Field Values

ACCURACY_MINUTES

public static final int ACCURACY_MINUTES
See Also:
Constant Field Values

ACCURACY_SECONDS

public static final int ACCURACY_SECONDS
See Also:
Constant Field Values

ACCURACY_MILLISECONDS

public static final int ACCURACY_MILLISECONDS
See Also:
Constant Field Values

UTC

public static final java.util.TimeZone UTC
Method Detail

getTimeZone

public static java.util.TimeZone getTimeZone(java.lang.String name)
                                      throws UnrecognizedTimeZoneException
Returns the time zone object for the name (or ID). This differs from TimeZone.getTimeZone(String) in that the latest returns GMT if it doesn't recognize the name, while this throws an UnrecognizedTimeZoneException.

Throws:
UnrecognizedTimeZoneException

dateToISO8601String

public static java.lang.String dateToISO8601String(java.util.Date date,
                                                   boolean datePart,
                                                   boolean timePart,
                                                   boolean offsetPart,
                                                   int accuracy,
                                                   java.util.TimeZone timeZone,
                                                   DateUtil.DateToISO8601CalendarFactory calendarFactory)
Format a date, time or date+time with one of the ISO 8601 extended formats. Examples of possible outputs: "2005-11-27T15:30:00+02:00", "2005-11-27", "15:30:00Z". Note the ":00" in the time zone offset; this is not required by ISO 8601, but included for compatibility with the XML Schema date/time formats. This method is thread-safe.

Parameters:
date - the date to convert to ISO 8601 string
datePart - whether the date part (year, month, day) will be included or not
timePart - whether the time part (hours, minutes, seconds, milliseconds) will be included or not
offsetPart - whether the time zone offset part will be included or not. This will be shown as an offset to UTC (examples: "+01", "-02", "+04:30") or as "Z" for UTC (and for UT1 and for GMT+00, since the Java platform doesn't really care about the difference). Note that this can't be true when timePart is false, because ISO 8601 (2004) doesn't mention such patterns.
accuracy - tells which parts of the date/time to drop. The datePart and timePart parameters are stronger than this. Note that when ACCURACY_MILLISECONDS is specified, the milliseconds part will be displayed as fraction seconds (like "15:30.00.25") with the minimum number of digits needed to show the milliseconds without precision lose. Thus, if the milliseconds happen to be exactly 0, no fraction seconds will be shown at all.
timeZone - the time zone in which the date/time will be shown. (You may find UTC handy here.) Note that although date-only formats has no time zone offset part, the result still depends on the time zone, as days start and end at different points in time in different zones.
calendarFactory - the factory that will create the calendar used internally for calculations. The point of this parameter is that creating a new calendar is relatively expensive, so it's desirable to reuse calendars and only set their time and zone. (This was tested on Sun JDK 1.6 x86 Win, where it gave 2x-3x speedup.)