Class DateUtil
-
Field Summary
Modifier and TypeFieldDescriptionstatic final DateDisplayFormatter
String will display with seconds in 24 hour time:[H]H:MM:SS
.static final DateDisplayFormatter
Date in the format <YYYYMMDD>T<HHMMSS>Z Example:20051104T111001Z
static final DateDisplayFormatter
Short date in format DD/MM/YYYY.
Example:4/11/2005
static final DateDisplayFormatter
Short date with time in format DD/MM/YYYY HH:MM
Example:4/11/2005 11:03
static final DateDisplayFormatter
Short date in format YYYY/MM/DD.
Example:2005/11/4
static final DateDisplayFormatter
Short date with time in format YYYY/MM/DD HH:MM
Example:2005/11/4 11:03
static final DateDisplayFormatter
Default native browser 'toLocaleString()' implementation.static final DateDisplayFormatter
Normal date format for the localestatic final DateDisplayFormatter
Normal datetime format for the localestatic final DateDisplayFormatter
String will display with seconds, with a 2 digit hour in 24 hour format:HH:MM:SS
.static final DateDisplayFormatter
String will display with seconds, with a 2 digit hour and am/pm indicator:HH:MM:SS am|pm
.static final DateDisplayFormatter
Date in the format YYYY-MM-DD HH:MM:SS
Example:2005-11-04 11:09:15
static final DateDisplayFormatter
String will have no seconds and be in 24 hour format:[H]H:MM
.
Example:15:25
static final DateDisplayFormatter
String will have no seconds and will display with a 2 digit hour in 24 hour clock format:HH:MM
.
Examples:15:25
,03:16
static final DateDisplayFormatter
String will have no seconds and will display a 2 digit hour, in 12 hour clock format:HH:MM am|pm
.
Example:03:25 pm
static final DateDisplayFormatter
String will have no seconds and be in 12 hour format:[H]H:MM am|pm
.
Example:3:25 pm
static final DateDisplayFormatter
Default native browser 'toString()' implementation.static final DateDisplayFormatter
String will display with seconds and am/pm indicator:[H]H:MM:SS am|pm
.static final DateDisplayFormatter
Short date in format MM/DD/YYYY.
Example:11/4/2005
static final DateDisplayFormatter
Short date with time in format MM/DD/YYYY HH:MM
Example:11/4/2005 11:03
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Date
adjustDate
(Date baseDate, String relativeDateShortcut) Create a new Date instance representing the baseDate adjusted by the parameter relativeDateShortcut.static void
Use the GWT LocaleInfo class to auto-detect the various formats for dates and times.static void
If a custom system wide date parser has been specified viasetDateParser(DateParser)
, clear this and revert to the standard date input format specified viasetDateInputFormat(String)
.static Date
combineLogicalDateAndTime
(Date date, Date time) Combine a logical date (a value appropriate for a DataSourceField of type "date") with a logical time (a value appropriate for a DataSourceField of type "time") into a datetime value (a value appropriate for a DataSourceField of type "datetime")static int
compareDates
(Date date1, Date date2) Compare two dates; returns 0 if equal, -1 if the first date is greater (later), or 1 if the second date is greater.static int
compareLogicalDates
(Date date1, Date date2) Compare two dates, normalizing out the time elements so that only the date elements are considered; returns 0 if equal, -1 if the first date is greater (later), or 1 if the second date is greater.static Date
create()
Create a newDate
object - synonym fornew Date(arguments)
static Date
createDatetime
(Integer year, Integer month, Integer date) Create a new Date instance in the current locale time.static Date
createDatetime
(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second, Integer millisecond) Create a new Date instance in the current locale time.static Date
createDatetime
(Date baseDate) Create a new Date instance in the current locale time.static Date
createDatetime
(Date baseDate, Integer month, Integer date) Create a new Date instance in the current locale time.static Date
createDatetime
(Date baseDate, Integer month, Integer date, Integer hour, Integer minute, Integer second, Integer millisecond) Create a new Date instance in the current locale timestatic LogicalDate
Create a new Date representing a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}.static LogicalDate
createLogicalDate
(int year, int month, int date) Create a new Date to represent a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}.static LogicalDate
createLogicalDate
(Date baseDate) Create a new Date to represent a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}.static LogicalDate
createLogicalDate
(Date baseDate, Integer month, Integer date) static LogicalTime
Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}.static LogicalTime
createLogicalTime
(int hour, int minute, int second, int millisecond) Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}.static LogicalTime
createLogicalTime
(Date baseDate) Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}.static LogicalTime
createLogicalTime
(Date baseDate, Integer minutes, Integer seconds) Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}.static String
Format a date as a string according to the format specified bysetNormalDateDisplayFormatter(DateDisplayFormatter)
.static String
Format the parameter date using the parameter format string.static String
formatAsNormalDate
(Date date) Format a date as a string according to the format specified bysetNormalDateDisplayFormatter(DateDisplayFormatter)
.static String
formatAsShortDate
(Date date) Format a date as a string according to the format specified bysetShortDateDisplayFormatter(DateDisplayFormatter)
.static String
formatAsShortDatetime
(Date date) Format a date as a string according to the format specified bysetShortDatetimeDisplayFormatter(DateDisplayFormatter)
.static Date
getAbsoluteDate
(RelativeDate relativeDate) static Date
getAbsoluteDate
(RelativeDate relativeDate, Date baseDate) static Date
getAbsoluteDate
(String relativeDateShortcut, Date baseDate, RelativeDateRangePosition rangePosition) static String
getDayName
(Date date) Return the full day of week name for this date (Monday, Tuesday, etc).static String[]
Return an array of the full names of each day, suitable for use in a selection list, etc.static String
gets the default date separator stringstatic int
getDisplayDay
(Date datetime) Returns the day of month from the passed datetime, as it will be displayed to the user.static int
getDisplayHours
(Date datetime) Returns the hours value from the passed datetime, as it will be displayed to the user.static int
getDisplayMinutes
(Date datetime) Returns the minutes value from the passed datetime, as it will be displayed to the user.static int
getDisplayMonth
(Date datetime) Returns the month number from the passed datetime, as it will be displayed to the user.static int
getDisplayYear
(Date datetime) Returns the full year from the passed datetime, as it will be displayed to the user.static Date
Returns the end of some period, like day, week or month, relative to a passed Date instance.static Date
static Date
Returns the end of some period, like day, week or month, relative to a passed Date instance.static int
Returns the global attribute that dictates which day should be treated as the first day of the week in calendars and date calculations.static FiscalCalendar
Returns the globalFiscalCalendar object
representing the start month and date of the fiscal year in the current locale.static Date
getFiscalStartDate
(Integer year, FiscalCalendar fiscalCalendar) Get the start date of the fiscal period for the passed year.static Date
getFiscalStartDate
(Date date) Returns the start date of the fiscal year for the passed date.static Date
getFiscalStartDate
(Date date, FiscalCalendar fiscalCalendar) Returns the start date of the fiscal year for the passed date.static int
getFiscalWeek
(Date date) Returns a date's week-number, according to the fiscal calendarstatic int
getFiscalWeek
(Date date, FiscalCalendar fiscalCalendar) Returns a date's week-number, according to the fiscal calendarstatic FiscalYear
getFiscalYear
(Date date) Returns theFiscalYear
object for the fiscal year in which the passed date exists.static FiscalYear
getFiscalYear
(Date date, FiscalCalendar fiscalCalendar) Returns theFiscalYear
object for the fiscal year in which the passed date exists.static String
Retrieves the default format for strings being parsed into dates viaDateUtil.parseInput()
static LogicalDate
getLogicalDateOnly
(Date date) Get a logical date - a value appropriate for a DataSourceField of type "date" - from a datetime value (a value from a DataSourceField of type "datetime").static LogicalTime
getLogicalTimeOnly
(Date date) Get a logical time - a value appropriate for a DataSourceField of type "time" - from a datetime value (a value from a DataSourceField of type "datetime").getMonthName
(Date date) Return the full name of the month for this date (January, February, etc) To modify the value returned by this method, usecom.smartgwt.client.util.DateUtil#setMonthNames()
.getShortDayName
(Date date) Return the abbreviated (up to 3 chars) day of week name for this date (Mon, Tue, etc).static String[]
getShortDayNames
(int length) Return an array of the short names of each day, suitable for use in a selection list, etc.getShortMonthName
(Date date) Return the abbreviated name of the month for this date (Jan, Feb, etc) To modify the value returned by this method, usecom.smartgwt.client.util.DateUtil#setShortMonthNames()
.getShortYear
(Date date) Return a 2 digit year for this date.static Date
getStartOf
(Date date, String period) Returns the start of some period, like day, week or month, relative to a passed Date instance.static Date
getStartOf
(Date date, String period, Boolean logicalDate) static Date
getStartOf
(Date date, String period, Boolean logicalDate, Integer firstDayOfWeek) Returns the start of some period, like day, week or month, relative to a passed Date instance.static Integer[]
Return an array of days that are considered "weekend" days.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 date passed in as a string, returning the appropriate date object.static Date
parseInput
(String dateString, String format) static Date
parseInput
(String dateString, String format, Integer centuryThreshold) static Date
parseInput
(String dateString, String format, Integer centuryThreshold, Boolean suppressConversion) Parse a date passed in as a string, returning the appropriate 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 (seeparseInput(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
setDayNames
(String[] dayNames) Set the array of names of days.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
setFirstDayOfWeek
(int firstDayOfWeek) Sets the global attribute that dictates which day should be treated as the first day of the week in calendars and date calculations.static void
setFiscalCalendar
(FiscalCalendar fiscalCalendar) Sets the global fiscal calendar, which is used for all calls to getFiscalYear() / getFiscalWeek() if those methods aren't passed a fiscalCalander.static void
setInputFormat
(String format) Sets up the default system-wide input format for strings being parsed into dates viaDateUtil.parseInput()
.static void
setMonthNames
(String[] monthNames) Set the array of names of months.static void
Set the default formatter for date objects to the custom DateDisplayFormatter passed in.static void
Set the default datetime format for date objects to the DateDisplayFormat passed in.static void
Set up a system wide default normal time formatting function.static void
setShortDateDisplayFormatter
(DateDisplayFormatter formatter) Set up a system wide default short date formatting function.static void
Set up a system wide default short datetime formatting function.static void
setShortDayNames
(String[] shortDayNames) Set the array of shortened names of days.static void
setShortDisplayFormat
(String format) Set the default short format for dates.static void
setShortMonthNames
(String[] shortMonthNames) Set the array of names of months.static void
setShortTimeDisplayFormatter
(DateDisplayFormatter formatter) Set up a system wide default short time formatting function.static void
setShowChooserFiscalYearPickers
(boolean showChooserFiscalYearPickers) static void
setShowChooserWeekPickers
(boolean showChooserWeekPickers) static void
setWeekendDays
(Integer[] weekendDays) Sets the days that are consideredweekend days
.toDateStamp
(Date date) Return this date in the format (UTC timezone):YYYYMMDDTHHMMSS[Z]
static void
today()
Return alogicalDate
representing the current day in the String.
-
Field Details
-
TOSTRING
Default native browser 'toString()' implementation. May vary by browser.
Example:Fri Nov 04 2005 11:03:00 GMT-0800 (Pacific Standard Time)
-
TOLOCALESTRING
Default native browser 'toLocaleString()' implementation. May vary by browser. Example:Friday, November 04, 2005 11:03:00 AM
-
TONORMALDATE
Normal date format for the locale -
TONORMALDATETIME
Normal datetime format for the locale -
TOUSSHORTDATE
Short date in format MM/DD/YYYY.
Example:11/4/2005
-
TOUSSHORTDATETIME
Short date with time in format MM/DD/YYYY HH:MM
Example:11/4/2005 11:03
-
TOEUROPEANSHORTDATE
Short date in format DD/MM/YYYY.
Example:4/11/2005
-
TOEUROPEANSHORTDATETIME
Short date with time in format DD/MM/YYYY HH:MM
Example:4/11/2005 11:03
-
TOJAPANSHORTDATE
Short date in format YYYY/MM/DD.
Example:2005/11/4
-
TOJAPANSHORTDATETIME
Short date with time in format YYYY/MM/DD HH:MM
Example:2005/11/4 11:03
-
TOSERIALIZEABLEDATE
Date in the format YYYY-MM-DD HH:MM:SS
Example:2005-11-04 11:09:15
-
TODATESTAMP
Date in the format <YYYYMMDD>T<HHMMSS>Z Example:20051104T111001Z
-
TOTIME
String will display with seconds and am/pm indicator:[H]H:MM:SS am|pm
.
Example:3:25:15 pm
-
TO24HOURTIME
String will display with seconds in 24 hour time:[H]H:MM:SS
.
Example:15:25:15
-
TOPADDEDTIME
String will display with seconds, with a 2 digit hour and am/pm indicator:HH:MM:SS am|pm
.
Example:03:25:15 pm
-
TOPADDED24HOURTIME
String will display with seconds, with a 2 digit hour in 24 hour format:HH:MM:SS
.
Examples:15:25:15
,03:16:45
-
TOSHORTTIME
String will have no seconds and be in 12 hour format:[H]H:MM am|pm
.
Example:3:25 pm
-
TOSHORT24HOURTIME
String will have no seconds and be in 24 hour format:[H]H:MM
.
Example:15:25
-
TOSHORTPADDEDTIME
String will have no seconds and will display a 2 digit hour, in 12 hour clock format:HH:MM am|pm
.
Example:03:25 pm
-
TOSHORTPADDED24HOURTIME
String will have no seconds and will display with a 2 digit hour in 24 hour clock format:HH:MM
.
Examples:15:25
,03:16
-
-
Constructor Details
-
DateUtil
public DateUtil()
-
-
Method Details
-
combineLogicalDateAndTime
Combine a logical date (a value appropriate for a DataSourceField of type "date") with a logical time (a value appropriate for a DataSourceField of type "time") into a datetime value (a value appropriate for a DataSourceField of type "datetime")This method correctly takes into account the current String, specifically, the returned datetime value will show the same date and time as the passed date and time objects when rendered by a Smart GWT component that has been configured with a field of type "datetime".
For further background on date, time and datetime types, storage and transmission, see
this overview
.- Parameters:
date
- a Date instance representing logical date valuetime
- a Date instance representing logical time value- Returns:
- a Date instance representing a datetime value combining the logical date and time passed
-
compareDates
Compare two dates; returns 0 if equal, -1 if the first date is greater (later), or 1 if the second date is greater. If either value is not a Date object, it is treated as the epoch (midnight on Jan 1 1970) for comparison purposes.- Parameters:
date1
- first date to comparedate2
- second date to compare- Returns:
- 0 if equal, -1 if first date > second date, 1 if second date > first date
-
compareLogicalDates
Compare two dates, normalizing out the time elements so that only the date elements are considered; returns 0 if equal, -1 if the first date is greater (later), or 1 if the second date is greater.- Parameters:
date1
- first date to comparedate2
- second date to compare- Returns:
- 0 if equal, -1 if first date > second date, 1 if second date > first date. Returns false if either argument is not a date
-
create
Create a newDate
object - synonym fornew Date(arguments)
- Returns:
- Date object
-
getAbsoluteDate
- Parameters:
relativeDate
- the relative date to convert- Returns:
- resulting absolute date value
-
getAbsoluteDate
- Parameters:
relativeDate
- the relative date to convertbaseDate
- base value for conversion. Defaults to the current date/time.- Returns:
- resulting absolute date value
-
getDayNames
Return an array of the full names of each day, suitable for use in a selection list, etc. Day names are picked up fromdayNames
, which defaults to an array of English-language strings and these are updated to localized values by loading a locale.If
DateUtil.dayNames
is purposely cleared, this method will fall back to deriving day-names from the native browser date string. Note, if we have to use this native backup behavior, the day names may vary by browser as well as locale - for example, they may be in an abbreviated form, similar to the result of callinggetShortDayNames()
.- Returns:
- array of day names
-
getDefaultDateSeparator
gets the default date separator string- Returns:
- the default date separator
-
getDisplayDay
Returns the day of month from the passed datetime, as it will be displayed to the user. This might not be the same value as that returned by getDate() if a String has been applied. Only necessary for datetimes - for logical dates and times, this method returns the same value as getDate().- Parameters:
datetime
- datetime instance to work with- Returns:
- the day of month from the passed datetime
-
getDisplayHours
Returns the hours value from the passed datetime, as it will be displayed to the user. This might not be the same value as that returned by getHours() if a String has been applied. Only necessary for datetimes - for logical dates and times, this method returns the same value as getHours().- Parameters:
datetime
- datetime instance to work with- Returns:
- the hours value from the passed datetime
-
getDisplayMinutes
Returns the minutes value from the passed datetime, as it will be displayed to the user. This might not be the same value as that returned by getMinutes() if a String has been applied. Only necessary for datetimes - for logical dates and times, this method returns the same value as getMinutes().- Parameters:
datetime
- datetime instance to work with- Returns:
- the minutes value from the passed datetime
-
getDisplayMonth
Returns the month number from the passed datetime, as it will be displayed to the user. This might not be the same value as that returned by getMonth() if a String has been applied. Only necessary for datetimes - for logical dates and times, this method returns the same value as getMonth().- Parameters:
datetime
- datetime instance to work with- Returns:
- the month number from the passed datetime
-
getDisplayYear
Returns the full year from the passed datetime, as it will be displayed to the user. This might not be the same value as that returned by getFullYear() if a String has been applied. Only necessary for datetimes - for logical dates and times, this method returns the same value as getFullYear().- Parameters:
datetime
- datetime instance to work with- Returns:
- the 4-digit display year from the passed datetime
-
getEndOf
Returns the end of some period, like day, week or month, relative to a passed Date instance.- Parameters:
date
- the base date to find the period end fromperiod
- the period to return the end of, one of mn/h/d/w/m/y- Returns:
- a Date instance representing the end of the period relative to the passed date
-
getEndOf
-
getEndOf
Returns the end of some period, like day, week or month, relative to a passed Date instance.- Parameters:
date
- the base date to find the period end fromperiod
- the period to return the end of, one of mn/h/d/w/m/ylogicalDate
- process and return a logicalDate with no time elementfirstDayOfWeek
- which day should be considered the firstDayOfWeek - overrides the default provided by the locale- Returns:
- a Date instance representing the end of the period relative to the passed date
-
getFirstDayOfWeek
public static int getFirstDayOfWeek()Returns the global attribute that dictates which day should be treated as the first day of the week in calendars and date calculations. The parameter is expected to be an integer value between 0 (Sunday) and 6 (Saturday).The default value is picked up from the current locale.
- Returns:
- the number of the day being used as the first day of the week
-
getFiscalCalendar
Returns the globalFiscalCalendar object
representing the start month and date of the fiscal year in the current locale.- Returns:
- the FiscalCalendar object
-
getFiscalStartDate
Returns the start date of the fiscal year for the passed date.- Parameters:
date
- the date, or the year-number, to get the fiscal year for- Returns:
- the start of the fiscal year for the passed date and fiscalCalendar
-
getFiscalStartDate
Returns the start date of the fiscal year for the passed date.- Parameters:
date
- the date, or the year-number, to get the fiscal year forfiscalCalendar
- the object representing the starts of one or more fiscal years- Returns:
- the start of the fiscal year for the passed date and fiscalCalendar
-
getFiscalWeek
Returns a date's week-number, according to the fiscal calendar- Parameters:
date
- the date to get the fiscal year for- Returns:
- the fiscal week for the passed date
-
getFiscalWeek
Returns a date's week-number, according to the fiscal calendar- Parameters:
date
- the date to get the fiscal year forfiscalCalendar
- the object representing the starts of fiscal years- Returns:
- the fiscal week for the passed date
-
getFiscalYear
Returns theFiscalYear
object for the fiscal year in which the passed date exists.- Parameters:
date
- the date to get the fiscal year for- Returns:
- the
FiscalYear
object for the passed date
-
getFiscalYear
Returns theFiscalYear
object for the fiscal year in which the passed date exists.- Parameters:
date
- the date to get the fiscal year forfiscalCalendar
- the object representing the start of the fiscal period- Returns:
- the
FiscalYear
object for the passed date
-
getInputFormat
Retrieves the default format for strings being parsed into dates viaDateUtil.parseInput()
- Returns:
- the current inputFormat for dates
- See Also:
-
getShortDayNames
Return an array of the short names of each day, suitable for use in a selection list, etc. Day names are picked up from ashortDayNames
list specified in each locale.- Parameters:
length
- maximum length of each day string - default is no maximum (full strings)- Returns:
- array of short day names
-
getStartOf
Returns the start of some period, like day, week or month, relative to a passed Date instance.- Parameters:
date
- the base date to find the period start fromperiod
- the period to return the start of, one of mn/h/d/w/m/y- Returns:
- a Date instance representing the start of the period relative to the passed date
-
getStartOf
-
getStartOf
public static Date getStartOf(Date date, String period, Boolean logicalDate, Integer firstDayOfWeek) Returns the start of some period, like day, week or month, relative to a passed Date instance.- Parameters:
date
- the base date to find the period start fromperiod
- the period to return the start of, one of mn/h/d/w/m/ylogicalDate
- process and return a logicalDate with no time elementfirstDayOfWeek
- which day should be considered the firstDayOfWeek - overrides the default provided by the locale- Returns:
- a Date instance representing the start of the period relative to the passed date
-
getWeekendDays
Return an array of days that are considered "weekend" days. Values will be the integers returned by the JavaScript built-in Date.getDay(), eg, 0 is Sunday and 6 is Saturday. OverrideweekendDays
to accommodate different workweeks such as Saudi Arabia (Saturday -> Wednesday) or Israel (Sunday -> Thursday).- Returns:
- array of weekend days
-
parseInput
Parse a date passed in as a string, returning the appropriate date object.- Parameters:
dateString
- date value as a string- Returns:
- date value, or null if the string could not be parsed to a valid date.
-
parseInput
- See Also:
-
parseInput
- See Also:
-
parseInput
public static Date parseInput(String dateString, String format, Integer centuryThreshold, Boolean suppressConversion) Parse a date passed in as a string, returning the appropriate date object.- Parameters:
dateString
- date value as a stringformat
- Format of the date string being passed. If not passed, the default date input format as set up via setInputFormat() will be used. SeeDateInputFormat
centuryThreshold
- For date formats that support a 2 digit year, if parsed year is 2 digits and less than this number, assume year to be 20xx rather than 19xxsuppressConversion
- If the string passed in was not a valid date, in some cases we can convert to a valid date (for example incrementing the year if the month is greater than 12). This optional parameter will suppress such conversions - anything that doesn't parse directly to a valid date will simply return null.- Returns:
- date value, or null if the string could not be parsed to a valid date.
-
setDefaultDateSeparator
Sets a new default separator that will be used when formatting dates. By default, this is a forward slash character: "/"- Parameters:
separator
- separator to use in dates
-
setFirstDayOfWeek
public static void setFirstDayOfWeek(int firstDayOfWeek) Sets the global attribute that dictates which day should be treated as the first day of the week in calendars and date calculations. The parameter is expected to be an integer value between 0 (Sunday) and 6 (Saturday).The default value is picked up from the current locale.
- Parameters:
firstDayOfWeek
- the number of the day to use as the first day of the week
-
setFiscalCalendar
Sets the global fiscal calendar, which is used for all calls to getFiscalYear() / getFiscalWeek() if those methods aren't passed a fiscalCalander.- Parameters:
fiscalCalendar
- the object representing the start month and date of the fiscal year in the current locale
-
setInputFormat
Sets up the default system-wide input format for strings being parsed into dates viaDateUtil.parseInput()
. This will effect how Smart GWT components showing editable date or datetime fields parse user-entered values into live Date objects.The input format can be specified as a
DateInputFormat
- 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 the
inputFormat is not explicitly set,the system automatically determines the standard input format will be based on the
specified
DateUtil.shortDisplayFormat
wherever possible. 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 Smart GWT will handle dates presented
with any separator string, and can handle 1 or 2 digit day and month values, months formatted as
getMonthNames()
orgetShortMonthNames()
, 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 a custom parser function. However if more sophisticated parsing logic is required, a function may be passed into this method. In this case the parser function should be able to handle parsing date and datetime values formatted viaformatAsShortDate()
andformatAsShorDatetime()
. - Date parsing and formatting logic may be overridden at the component level by setting properties directly on the component or field in question.
- Parameters:
format
- Default format for strings to be parsed into Dates. SeeDateInputFormat
- See Also:
- If the
inputFormat is not explicitly set,the system automatically determines the standard input format will be based on the
specified
-
setShortDisplayFormat
Set the default short format for dates. After calling this method, subsequent calls toformatAsShortDate
will return a string formatted according to this format specification. Note that this will be the standard short date format used by Smart GWT components.The
format
parameter may be aFormatString
, aDateDisplayFormat
string, or a function. If passed a function, this function will be executed in the scope of the Date and should return the formatted string.
Initial default shortDateFormat is
"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
- new formatter. SeeFormatString
-
setShowChooserFiscalYearPickers
public static void setShowChooserFiscalYearPickers(boolean showChooserFiscalYearPickers) Sets the global attribute that dictates whether thechoosers
shelled fromDateItems
show a UI for working with Fiscal Years.- Parameters:
showChooserFiscalYearPickers
- whether to show Fiscal Year pickers in DateChoosers by default
-
setShowChooserWeekPickers
public static void setShowChooserWeekPickers(boolean showChooserWeekPickers) Sets the global attribute that dictates whether thechoosers
shelled fromDateItems
show a UI for working with Weeks.- Parameters:
showChooserWeekPickers
- whether to show Fiscal Week pickers in DateChoosers by default
-
setWeekendDays
Sets the days that are consideredweekend days
. The parameter should be array of the integers returned by the JavaScript built-in Date.getDay(), eg, 0 is Sunday and 6 is Saturday. Override to accommodate different workweeks such as Saudi Arabia (Saturday -> Wednesday) or Israel (Sunday -> Thursday).- Parameters:
weekendDays
- the array of day-numbers to assign as weekend days
-
today
public static void today()Return alogicalDate
representing the current day in the String. -
getDayName
Return the full day of week name for this date (Monday, Tuesday, etc). To modify the value returned by this method, usesetDayNames(java.lang.String[])
.- Parameters:
date
-- Returns:
- Day name
-
getShortDayName
Return the abbreviated (up to 3 chars) day of week name for this date (Mon, Tue, etc). To modify the value returned by this method, usesetShortDayNames(java.lang.String[])
.- Parameters:
date
-- Returns:
- Abbreviated day name
-
getMonthName
Return the full name of the month for this date (January, February, etc) To modify the value returned by this method, usecom.smartgwt.client.util.DateUtil#setMonthNames()
.- Parameters:
date
-- Returns:
- Month name
-
getShortMonthName
Return the abbreviated name of the month for this date (Jan, Feb, etc) To modify the value returned by this method, usecom.smartgwt.client.util.DateUtil#setShortMonthNames()
.- Parameters:
date
-- Returns:
- Abbreviated month name (3 character string)
-
getShortYear
Return a 2 digit year for this date.- Parameters:
date
-- Returns:
- year number, padded to 2 characters
-
toDateStamp
Return this date in the format (UTC timezone):YYYYMMDDTHHMMSS[Z]
- Parameters:
date
-- Returns:
- formatted date string
-
setDefaultDisplayTimezone
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
-
setNormalDatetimeDisplayFormatter
Set the default datetime 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 datetime format used by SmartGWT components. Initial default normalDatetimeDisplayFormat is "toLocaleString"- Parameters:
format
- the DateDisplayFormatter
-
setNormalDateDisplayFormatter
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. SmartGWT includes several built-in DateDisplayFormatters for common formats - seeDateDisplayFormatter
for details. 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; } });
As of version 4.1, SmartGWT has built-in string-based formatting of date and time values via theDateFormatStringFormatter
class. The main advantage of using the built-in formatting feature is that it is harmonized with the formatting we export to Excel and other targets, leading to an exported document that more closely resembles the original application. Seecom.smartgwt.client.data.DataSOurceField#format
for details. Sample code :DateUtil.setNormalDateDisplayFormatter(new DateFormatStringFormatter("yyyy.MM.dd HH:mm:ss"));
- Parameters:
formatter
- the DateDisplayFormatter
-
setShortDateDisplayFormatter
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 byformatAsShortDate(Date)
).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)
orsetDateParser(DateParser)
.When writing custom date formatting and parsing logic, developers may find the
DateTimeFormat
class helpful. SmartGWT includes several built-in DateDisplayFormatters for common formats - seeDateDisplayFormatter
for details. 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
-
setShortDatetimeDisplayFormatter
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 byformatAsShortDatetime(Date)
).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)
orsetDateParser(DateParser)
.When writing custom date formatting and parsing logic, developers may find the
DateTimeFormat
class helpful. SmartGWT includes several built-in DateDisplayFormatters for common formats - seeDateDisplayFormatter
for details.- Parameters:
formatter
- the DateDisplayFormatter
-
adjustDate
Create a new Date instance representing the baseDate adjusted by the parameter relativeDateShortcut.- Parameters:
baseDate
- base Date value to adjust. Defaults to the current date/time.relativeDateShortcut
- theRelativeDateShortcut
orRelativeDateString
string to convert- Returns:
- resulting absolute date value
-
createDatetime
Create a new Date instance in the current locale time.See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
- any Date instance. Defaults to the current date/time.- Returns:
- new Date instance in the current locale time
-
createDatetime
Create a new Date instance in the current locale time. Time elements default to those from the parameter baseDate.See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
- any Date instance. Defaults to the current date/time.month
- Integer month-number (0-11) - defaults to baseDate monthdate
- Integer day of the month - defaults to baseDate date- Returns:
- new Date instance in the current locale time
-
createDatetime
public static Date createDatetime(Date baseDate, Integer month, Integer date, Integer hour, Integer minute, Integer second, Integer millisecond) Create a new Date instance in the current locale timeSee the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
- any Date instance. Defaults to the current date/time.month
- Integer month-number (0-11) - defaults to the month from the parameter baseDatedate
- Integer day of the month - defaults to the date from the parameter baseDatehour
- Integer hours (0-23) - defaults to the hours form the parameter baseDateminute
- Integer minutes (0-59) - defaults to the minutes form the parameter baseDatesecond
- Integer seconds (0-59) - defaults to the seconds form the parameter baseDatemillisecond
- Integer milliseconds (0999) - defaults to the milliseconds form the parameter baseDate- Returns:
- new Date instance in the current locale time
-
createDatetime
Create a new Date instance in the current locale time. Time elements default to zero.See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
year
- Integer full yearmonth
- Integer month-number (0-11)date
- Integer day of the month- Returns:
- resulting absolute date value
-
createDatetime
public static Date createDatetime(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second, Integer millisecond) Create a new Date instance in the current locale time.See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
year
- Integer full yearmonth
- Integer month-number (0-11)date
- Integer day of the monthhour
- Integer hours (0-23) - defaults to zerominute
- Integer minutes (0-59) - defaults to zerosecond
- Integer seconds (0-59) - defaults to zeromillisecond
- Integer milliseconds (0-999) - defaults to zero- Returns:
- resulting absolute date value
-
createLogicalDate
Create a new Date representing a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}. The generated Date value will have year, month and date set to today (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Returns:
- LogicalDate representing a logical date.
-
createLogicalDate
Create a new Date to represent a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}. The parameter baseDate defaults to null if unset, and the generated Date value will have year, month and date set from that baseDate (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
-- Returns:
- LogicalDate representing a logical date.
-
createLogicalDate
-
createLogicalDate
Create a new Date to represent a logical date value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,date type field}. The generated Date value will have year, month and date set to the specified values (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
year
-month
-date
-- Returns:
- LogicalDate representing a logical date.
-
createLogicalTime
Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}. The generated Date value will have year, month and date set to the epoch date (Jan 1 1970), and time elements set to the current time (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Returns:
- new LogicalTime representing the time in question
-
createLogicalTime
Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}. The generated Date value will have year, month and date set to the epoch date (Jan 1 1970), and time elements set to those from the parameter baseDate (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
- any Date instance- Returns:
- new LogicalTime representing the time in question
-
createLogicalTime
Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}. The generated Date value will have year, month and date set to the epoch date (Jan 1 1970), where the hour comes from the parameter baseDate and the minute and second values come from their respective parameters, defaulting to the values from the parameter baseDate (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
baseDate
- any Date instanceminute
- Integer minutes (0-59)second
- Integer seconds (0-59)- Returns:
- new LogicalTime representing the time in question
-
createLogicalTime
Create a new Date object to represent a logical time value (rather than a specific datetime value), typically for display in a +link{DataSourceField.type,time type field}. The generated Date value will have year, month and date set to the epoch date (Jan 1 1970), and time elements set to the supplied hour, minute and second (in browser native local time).See the
docs
for a discussion of the difference between datetime field values and logical date field values, logical time field values.- Parameters:
hour
- (0-23)minute
- (0-59)second
- (0-59)millisecond
- (0-999)- Returns:
- new LogicalTime representing the time in question
-
getLogicalDateOnly
Get a logical date - a value appropriate for a DataSourceField of type "date" - from a datetime value (a value from a DataSourceField of type "datetime").This method correctly takes into account the current String, specifically, the returned Date will reflect the day, month and year that appears when the datetime is rendered by a Smart GWT component rather than the date values that would be returned by Date.getDay() et al (which can differ, since getDay() uses the browser's local timezone).
For further background on date, time and datetime types, storage and transmission, see
this overview
.- Parameters:
date
- a Date instance representing a datetime value- Returns:
- a Date instance representing just the date portion of the datetime value, as a logical date
-
getLogicalTimeOnly
Get a logical time - a value appropriate for a DataSourceField of type "time" - from a datetime value (a value from a DataSourceField of type "datetime").This method correctly takes into account the current String, specifically, the returned Date will reflect the hour, minute and second that appears when the datetime is rendered by a Smart GWT component rather than the time values that would be returned by Date.getHours() et al (which can differ, since getHours() uses the browser's local timezone).
For further background on date, time and datetime types, storage and transmission, see
this overview
.- Parameters:
date
- a Date instance representing a datetime value- Returns:
- a Date instance representing just the time portion of the datetime value, as a logical time
-
formatAsShortDate
Format a date as a string according to the format specified bysetShortDateDisplayFormatter(DateDisplayFormatter)
.This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.
- Parameters:
date
-- Returns:
-
formatAsShortDatetime
Format a date as a string according to the format specified bysetShortDatetimeDisplayFormatter(DateDisplayFormatter)
.This calls the standard date formatting function used by SmartGWT components to display short-formatted date-times.
- Parameters:
date
-- Returns:
-
format
Format a date as a string according to the format specified bysetNormalDateDisplayFormatter(DateDisplayFormatter)
.This calls the standard date formatting function used by SmartGWT components to display short-formatted dates.
- Parameters:
date
-- Returns:
-
formatAsNormalDate
Format a date as a string according to the format specified bysetNormalDateDisplayFormatter(DateDisplayFormatter)
.This calls the standard date formatting function used by SmartGWT components to display normal-formatted dates.
- Parameters:
date
-- Returns:
- Returns a String containing the formatted date
-
setDateInputFormatter
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
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 thesetShortDateDisplayFormatter(DateDisplayFormatter)
andsetShortDatetimeDisplayFormatter(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
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
Sets up the default system-wide input format for strings being parsed into dates via SmartGWT utilities and components (seeparseInput(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:
- Parameters:
inputFormat
-
-
clearDateParser
public static void clearDateParser()If a custom system wide date parser has been specified viasetDateParser(DateParser)
, clear this and revert to the standard date input format specified viasetDateInputFormat(String)
. -
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 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"- Returns:
- converted relative date string
-
mapRelativeDateShortcut
-
getAbsoluteDate
public static Date getAbsoluteDate(String relativeDateShortcut, Date baseDate, RelativeDateRangePosition rangePosition) - Parameters:
relativeDateShortcut
- theRelativeDateShortcut
orRelativeDateString
string to convertbaseDate
- base value for conversion. Defaults to the current date/time.rangePosition
- date-range position. Only has an effect if the date passed in is aRelativeDateShortcut
where the range position is not implicit, such as "$yesterday"- Returns:
- resulting absolute date value
- See Also:
-
setNormalTimeDisplayFormatter
Set up a system wide default normal time formatting function. After calling this method, times displayed in SmartGWT components will use the specified format. The initial default normal time display formatter isDateUtil.TOTIME
.SmartGWT includes several built-in DateDisplayFormatters for common formats - see
DateDisplayFormatter
for details.As of version 4.1, SmartGWT has built-in string-based formatting of date and time values via the
DateFormatStringFormatter
class. The main advantage of using the built-in formatting feature is that it is harmonized with the formatting we export to Excel and other targets, leading to an exported document that more closely resembles the original application. Seecom.smartgwt.client.data.DataSourceField#format
for details. Sample code :DateUtil.setNormalTimeDisplayFormatter(new DateFormatStringFormatter("HH-mm-ss"));
- Parameters:
formatter
- the DateDisplayFormatter
-
setShortTimeDisplayFormatter
Set up a system wide default short time formatting function. After calling this method, subsequent calls toisc.Time.toShortTime()
will return a string formatted according to this formatter specification. Note: this will be the standard time format used by SmartGWT components. The initial default normal time display formatter isDateUtil.TOSHORTTIME
.SmartGWT includes several built-in DateDisplayFormatters for common formats - see
DateDisplayFormatter
for details.- Parameters:
formatter
- the DateDisplayFormatter
-
format
Format the parameter date using the parameter format string. This is a convenience method; it simply creates aDateFormatStringFormatter
and calls itsformat()
method- Parameters:
date
- the Date to formatformat
- the format string to use- Returns:
- the parameter date formatted according to the parameter format string
-
setDayNames
Set the array of names of days. For example:new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
The appropriate day name will then be returned from#getDayName()
, and may be used whenever SmartGWT components display day-names (for example in theDateItem class
).Note : This is an advanced setting
- Parameters:
dayNames
- a length 7 array of day names
-
setShortDayNames
Set the array of shortened names of days. For example:new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
The appropriate day name will then be returned from#getShortDayName()
, and may be used whenever SmartGWT components display shortened day-names (for example in theDateItem class
).Note : This is an advanced setting
- Parameters:
shortDayNames
- a length 7 array of abbreviated day names
-
setMonthNames
Set the array of names of months. For example:new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }
The appropriate month name will then be returned from#getMonthName()
, and may be used whenever SmartGWT components display month-names (for example in theDateItem class
).Note : This is an advanced setting
- Parameters:
monthNames
- a length 12 array of month names
-
setShortMonthNames
Set the array of names of months. For example:new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
The appropriate month name will then be returned from#getShortMonthName()
, and may be used whenever SmartGWT components display month-names (for example in theDateItem class
).Note : This is an advanced setting
- Parameters:
shortMonthNames
- a length 12 array of abbreviated month names
-
getFiscalStartDate
Get the start date of the fiscal period for the passed year.- Returns:
-
autoDetectFormats
public static void autoDetectFormats()Use the GWT LocaleInfo class to auto-detect the various formats for dates and times.
-