com.smartgwt.client.util
Class DateUtil

java.lang.Object
  extended by com.smartgwt.client.util.DateUtil

public class DateUtil
extends Object

Date related utility methods.


Constructor Summary
DateUtil()
           
 
Method Summary
static String format(Date date)
          Format a date as a string according to the format specified by setNormalDateDisplayFormat(DateDisplayFormat) or setNormalDateDisplayFormatter(DateDisplayFormatter).
static String formatAsShortDate(Date date)
          Format a date as a string according to the format specified by setShortDateDisplayFormat(DateDisplayFormat) or setShortDateDisplayFormatter(DateDisplayFormatter).
static String formatAsShortDatetime(Date date)
          Format a date as a string according to the format specified by setShortDatetimeDisplayFormat(DateDisplayFormat) or setShortDatetimeDisplayFormatter(DateDisplayFormatter).
static Date getAbsoluteDate(RelativeDate relativeDate)
          Converts a RelativeDate to a concrete Date.
static Date getAbsoluteDate(RelativeDate relativeDate, Date baseDate, RelativeDateRangePosition position)
          Converts a RelativeDate to a concrete Date.
static String getDefaultDateSeparator()
          Returns the default date separator.
static String mapRelativeDateShortcut(String relativeDateShortcut)
           
static String mapRelativeDateShortcut(String relativeDateShortcut, RelativeDateRangePosition position)
          Converts a RelativeDate shortcut string such as "$today" to a RelativeDateString such as "+0D"
static Date parseInput(String dateString)
          Parse a formatted date string into a Date object.
static void setAdjustForDST(boolean adjustForDST)
          Determines whether, when formatting values of type datetime and time, the effect of Daylight Saving Time should be considered when computing offsets from UTC.
static void setDateInputFormat(String inputFormat)
          Sets up the default system-wide input format for strings being parsed into dates via SmartGWT utilities and components (see parseInput(String)).
static void setDateInputFormatter(DateInputFormatter formatter)
          Deprecated. in favor of setDateParser()
static void setDateParser(DateParser parser)
          Sets up a custom parsing function to use by default when converting dates or datetimes from formatted string values to Dates.
static void setDefaultDateSeparator(String separator)
          Sets a new default separator that will be used when formatting dates.
static void setDefaultDisplayTimezone(String offset)
          Globally sets the offset from UTC to use when formatting values of type datetime and time with standard display formatters.
static void setNormalDateDisplayFormat(DateDisplayFormat format)
          Set the default format for date objects to the DateDisplayFormat passed in.
static void setNormalDateDisplayFormatter(DateDisplayFormatter formatter)
          Set the default formatter for date objects to the custom DateDisplayFormatter passed in.
static void setShortDateDisplayFormat(DateDisplayFormat format)
          Set the system wide default short date format.
static void setShortDateDisplayFormatter(DateDisplayFormatter formatter)
          Set up a system wide default short date formatting function.
static void setShortDatetimeDisplayFormat(DateDisplayFormat format)
          Set the system wide default short datetime format.
static void setShortDatetimeDisplayFormatter(DateDisplayFormatter formatter)
          Set up a system wide default short datetime formatting function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtil

public DateUtil()
Method Detail

setDefaultDisplayTimezone

public static void setDefaultDisplayTimezone(String offset)
Globally sets the offset from UTC to use when formatting values of type datetime and time with standard display formatters. This property effects how dates are displayed and also the assumed timezone for user-input.

If this method is never called, the default display timezone for times and datetimes will be derived from the native browser local timezone.

Note that by default daylight savings time adjustments (based on browser locale) may also be applied when formatting datetimes. setAdjustForDST(boolean) may be used to disable this adjustment.

Parameters:
offset - offset from UTC. This should be a string in the format +/-HH:MM for example "-08:00"

setAdjustForDST

public static void setAdjustForDST(boolean adjustForDST)
Determines whether, when formatting values of type datetime and time, the effect of Daylight Saving Time should be considered when computing offsets from UTC. By default, this flag is set during framework initialization if SmartGWT detects that it is running in a locale that is observing DST this year. If you do not want DST adjustments to be applied, set this flag to false.

Note that setting this flag to true will have no effect unless you are in a locale that is observing Daylight Saving Time this year; this is because we rely on the browser for offset information, and browsers are only capable of returning local date and time information for the computer's current locale.

Parameters:
whether - time and datetimes should account for daylight savings time in this application

setDefaultDateSeparator

public static void setDefaultDateSeparator(String separator)
Sets a new default separator that will be used when formatting dates. By default, this is a forward slash character: "/"

Parameters:
separator - the date separator

getDefaultDateSeparator

public static String getDefaultDateSeparator()
Returns the default date separator.

Returns:
the default date separator

setNormalDateDisplayFormat

public static void setNormalDateDisplayFormat(DateDisplayFormat format)
Set the default format for date objects to the DateDisplayFormat passed in. After calling this method, subsequent calls to Date.toNormalDate will return a string formatted according to this format specification.
Note: this will be the standard long date format used by SmartGWT components. Initial default normalDisplayFormat is "toLocaleString"

Parameters:
format - the DateDisplayFormat

setNormalDateDisplayFormatter

public static void setNormalDateDisplayFormatter(DateDisplayFormatter formatter)
Set the default formatter for date objects to the custom DateDisplayFormatter passed in. After calling this method, subsequent calls to Date.toNormalDate will return a string formatted according to this formatter specification.

When writing custom date formatting and parsing logic, developers may find the DateTimeFormat class helpful. Sample code :

 DateUtil.setNormalDateDisplayFormatter(new DateDisplayFormatter() {
     public String format(Date date) {
         if(date == null) return null;
         final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("yyyy.MM.dd HH:mm:ss");
         String format = dateFormatter.format(date);
         return format;
     }
 });
 

Parameters:
formatter - the DateDisplayFormatter

setShortDateDisplayFormat

public static void setShortDateDisplayFormat(DateDisplayFormat format)
Set the system wide default short date format. The specified format will be used by default by SmartGwt components when formatting date values to short date format (and by formatAsShortDate(Date)).

This method, together with setDefaultDateSeparator(String) provide support for most standard "short date" formats. However if a custom format which doesn't match any of the specified DateDisplayFormat types is required, a custom formatting function may be provided via setShortDateDisplayFormatter(DateDisplayFormatter).

Note that the default short date format is initially set to DateDisplayFormat.TOUSSHORTDATE. This property is commonly modified for localization of applications. See http://en.wikipedia.org/wiki/Date_format_by_country for a useful overview of standard date formats per country.

Parameters:
format - the DateDisplayFormat

setShortDateDisplayFormatter

public static void setShortDateDisplayFormatter(DateDisplayFormatter formatter)
Set up a system wide default short date formatting function. The formatter passed in will be used by default by SmartGwt components when formatting date values to short date format (and by formatAsShortDate(Date)).

Note that setShortDateDisplayFormat(DateDisplayFormat) and setDefaultDateSeparator(String) already provide support for most standard "short date" formats without the need for a custom formatter.

If a custom short date formatter is applied, bear in mind that it will be applied by default when editing date values, so the system will need to be able to parse an edited date string in this format back to a live date object. Developers calling this method will therefore also commonly want to apply custom parsing logic via setDateInputFormat(String) or setDateParser(DateParser).

When writing custom date formatting and parsing logic, developers may find the DateTimeFormat class helpful. Sample code :

 DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
     public String format(Date date) {
         if(date == null) return null;
         final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MMM d, yyyy");
         String format = dateFormatter.format(date);
         return format;
     }
 });
 

Parameters:
formatter - the DateDisplayFormatter

setShortDatetimeDisplayFormat

public static void setShortDatetimeDisplayFormat(DateDisplayFormat format)
Set the system wide default short datetime format. The specified format will be used by default by SmartGwt components when formatting datetime field values to short datetime format (and by formatAsShortDatetime(Date)).

This method, together with setDefaultDateSeparator(String) provide support for most standard "short date" formats. However if a custom format which doesn't match any of the specified DateDisplayFormat types is required, a custom formatting function may be provided via setShortDatetimeDisplayFormatter(DateDisplayFormatter).

Note that the default short datetime format is initially set to DateDisplayFormat.TOUSSHORTDATETIME. This property is commonly modified for localization of applications. See http://en.wikipedia.org/wiki/Date_format_by_country for a useful overview of standard date formats per country.

Parameters:
format - the DateDisplayFormat

setShortDatetimeDisplayFormatter

public static void setShortDatetimeDisplayFormatter(DateDisplayFormatter formatter)
Set up a system wide default short datetime formatting function. The formatter passed in will be used by default by SmartGwt components when formatting date values to short datetime format (and by formatAsShortDatetime(Date)).

Note that setShortDatetimeDisplayFormat(DateDisplayFormat) and setDefaultDateSeparator(String) already provide support for most standard "short date" formats without the need for a custom formatter.

If a custom short datetime formatter is applied, bear in mind that it will be applied by default when editing date values, so the system will need to be able to parse an edited date string in this format back to a live date object. Developers calling this method will therefore also commonly want to apply custom parsing logic via setDateInputFormat(String) or setDateParser(DateParser).

When writing custom date formatting and parsing logic, developers may find the DateTimeFormat class helpful.

Parameters:
formatter - the DateDisplayFormatter

parseInput

public static Date parseInput(String dateString)
Parse a formatted date string into a Date object.

This calls the default parsing function used by SmartGWT components.

If setDateInputFormat(String) or setDateParser(DateParser) has been specified it will be respected.

Parameters:
dateString - string to parse into a Date
Returns:
date value from the string (or null if the string passed in could not be converted to a date).

formatAsShortDate

public static String formatAsShortDate(Date date)
Format a date as a string according to the format specified by setShortDateDisplayFormat(DateDisplayFormat) or setShortDateDisplayFormatter(DateDisplayFormatter).

This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.

Parameters:
date -
Returns:

formatAsShortDatetime

public static String formatAsShortDatetime(Date date)
Format a date as a string according to the format specified by setShortDatetimeDisplayFormat(DateDisplayFormat) or setShortDatetimeDisplayFormatter(DateDisplayFormatter).

This calls the standard date formatting function used by SmartGWT components to display short-formatted date-times.

Parameters:
date -
Returns:

format

public static String format(Date date)
Format a date as a string according to the format specified by setNormalDateDisplayFormat(DateDisplayFormat) or setNormalDateDisplayFormatter(DateDisplayFormatter).

This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.

Parameters:
date -
Returns:

setDateInputFormatter

public static void setDateInputFormatter(DateInputFormatter formatter)
Deprecated. in favor of setDateParser()

Sets up the default format for strings being parsed into dates via DateUtil.parseInput
Sample code :
 DateUtil.setDateInputFormatter(new DateInputFormatter() {
    public Date format(String dateString) {
       final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MMM d, yyyy");
       Date date = dateFormatter.parse(dateString);
       return date;
    }
 });
 

Parameters:
formatter - the DateInputFormatter

setDateParser

public static void setDateParser(DateParser parser)
Sets up a custom parsing function to use by default when converting dates or datetimes from formatted string values to Dates. This custom parser will be used by SmartGwt components parsing editable date or datetime type values back to live dates by default. The string passed in will be formatted according to the standard "short date" or "short datetime" format (which may be customized via setShortDateDisplayFormat(DateDisplayFormat) or setShortDateDisplayFormatter(DateDisplayFormatter) and setShortDatetimeDisplayFormat(DateDisplayFormat) or setShortDatetimeDisplayFormatter(DateDisplayFormatter) methods.

Note that the default date parsing logic already handles all standard short date formats, including those formatted with custom separators. In most cases rather than applying an entirely custom date parser method, desired behavior can be achieved via changing the standard #setDateInputFormat(String),input format.

When writing custom date formatting and parsing logic, developers may find the DateTimeFormat class helpful.

Sample code :

 DateUtil.setDateParser(new DateParser() {
    public Date parse(String dateString) {
       final DateTimeFormat format = DateTimeFormat.getFormat("MMM d, yyyy");
       Date date = format.parse(dateString);
       return date;
    }
 });
 

Individual components may also override date formatting and parsing functions directly.

Parameters:
parser -

setDateInputFormat

public static void setDateInputFormat(String inputFormat)
Sets up the default system-wide input format for strings being parsed into dates via SmartGWT utilities and components (see parseInput(String)). This input format is respected when parsing formatted strings to "date" or "datetime" type values.

This method takes a 3 character string like "MDY" indicating the order of the Month, Day and Year components of date strings.

As an example - an input format of "MDY" would parse "01/02/1999" to Jan 2nd 1999
This standard parsing logic will also handle date-time strings such as "01/02/1999 08:45", or "01/02/1999 16:21:05".

Notes:

  • If this method is never called, the system attempts to automatically determine the standard input format will be automatically determined based on the specified #setShortDateDisplayFormat(DateDisplayFormat),short date display format. For example if the short display format has been set to "toEuropeanShortDate" the input format will default to "DMY".
  • The default date parsing functionality built into SmartGWT will handle dates presented with any separator string, and can 1 or 2 digit day and month values and 2 or 4 digit year values. This means that in many cases custom date display formats can be parsed back to Date values without the need for explicit custom formatter functions. However if more sophisticated parsing logic is required an entirely custom parser function may be applied via setDateParser().
  • Date parsing and formatting logic may be overridden at the component level by setting properties directly on the component or field in question.

Parameters:
inputFormat -

mapRelativeDateShortcut

public static String mapRelativeDateShortcut(String relativeDateShortcut,
                                             RelativeDateRangePosition position)
Converts a RelativeDate shortcut string such as "$today" to a RelativeDateString such as "+0D"

Parameters:
relativeDateShortcut - shortcut string to convert
position - Are we interested in the start or end of the // specified relative date? This applies to shortcuts which do not specify a specific // moment (such as $today) - it does not apply to shortcuts which // already specify a specific moment such as $startOfToday. If unspecified // rangePosition is always assumed to be "start"
Returns:
converted relative date string

mapRelativeDateShortcut

public static String mapRelativeDateShortcut(String relativeDateShortcut)

getAbsoluteDate

public static Date getAbsoluteDate(RelativeDate relativeDate)
Converts a RelativeDate to a concrete Date.

Returns:

getAbsoluteDate

public static Date getAbsoluteDate(RelativeDate relativeDate,
                                   Date baseDate,
                                   RelativeDateRangePosition position)
Converts a RelativeDate to a concrete Date.

Returns: