|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.smartgwt.client.util.DateUtil
public class DateUtil
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 |
|---|
public DateUtil()
| Method Detail |
|---|
public static void setDefaultDisplayTimezone(String offset)
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.
offset - offset from UTC. This should be a string in the format +/-HH:MM for example "-08:00"public static void setAdjustForDST(boolean adjustForDST)
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.
whether - time and datetimes should account for daylight savings time in this applicationpublic static void setDefaultDateSeparator(String separator)
separator - the date separatorpublic static String getDefaultDateSeparator()
public static void setNormalDateDisplayFormat(DateDisplayFormat format)
format - the DateDisplayFormatpublic static void setNormalDateDisplayFormatter(DateDisplayFormatter formatter)
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;
}
});
formatter - the DateDisplayFormatterpublic static void setShortDateDisplayFormat(DateDisplayFormat format)
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.
format - the DateDisplayFormatpublic static void setShortDateDisplayFormatter(DateDisplayFormatter formatter)
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;
}
});
formatter - the DateDisplayFormatterpublic static void setShortDatetimeDisplayFormat(DateDisplayFormat format)
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.
format - the DateDisplayFormatpublic static void setShortDatetimeDisplayFormatter(DateDisplayFormatter formatter)
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.
formatter - the DateDisplayFormatterpublic static Date parseInput(String dateString)
This calls the default parsing function used by SmartGWT components.
If setDateInputFormat(String) or setDateParser(DateParser)
has been specified it will be respected.
dateString - string to parse into a Date
public static String formatAsShortDate(Date date)
setShortDateDisplayFormat(DateDisplayFormat) or
setShortDateDisplayFormatter(DateDisplayFormatter).
This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.
date -
public static String formatAsShortDatetime(Date date)
setShortDatetimeDisplayFormat(DateDisplayFormat) or
setShortDatetimeDisplayFormatter(DateDisplayFormatter).
This calls the standard date formatting function used by SmartGWT components to display short-formatted date-times.
date -
public static String format(Date date)
setNormalDateDisplayFormat(DateDisplayFormat) or
setNormalDateDisplayFormatter(DateDisplayFormatter).
This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.
date -
public static void setDateInputFormatter(DateInputFormatter formatter)
DateUtil.setDateInputFormatter(new DateInputFormatter() {
public Date format(String dateString) {
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("MMM d, yyyy");
Date date = dateFormatter.parse(dateString);
return date;
}
});
formatter - the DateInputFormatterpublic static void setDateParser(DateParser parser)
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.
parser - public static void setDateInputFormat(String inputFormat)
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:
#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".setDateParser().
inputFormat -
public static String mapRelativeDateShortcut(String relativeDateShortcut,
RelativeDateRangePosition position)
"$today" to a RelativeDateString such as "+0D"
relativeDateShortcut - shortcut string to convertposition - 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"
public static String mapRelativeDateShortcut(String relativeDateShortcut)
public static Date getAbsoluteDate(RelativeDate relativeDate)
public static Date getAbsoluteDate(RelativeDate relativeDate,
Date baseDate,
RelativeDateRangePosition position)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||