Class DateUtil

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

public class DateUtil extends Object
Static singleton class containing APIs for interacting with Dates.
  • Field Details

    • TOSTRING

      public static final DateDisplayFormatter TOSTRING
      Default native browser 'toString()' implementation. May vary by browser.
      Example: Fri Nov 04 2005 11:03:00 GMT-0800 (Pacific Standard Time)
    • TOLOCALESTRING

      public static final DateDisplayFormatter TOLOCALESTRING
      Default native browser 'toLocaleString()' implementation. May vary by browser. Example: Friday, November 04, 2005 11:03:00 AM
    • TONORMALDATE

      public static final DateDisplayFormatter TONORMALDATE
      Normal date format for the locale
    • TONORMALDATETIME

      public static final DateDisplayFormatter TONORMALDATETIME
      Normal datetime format for the locale
    • TOUSSHORTDATE

      public static final DateDisplayFormatter TOUSSHORTDATE
      Short date in format MM/DD/YYYY.
      Example: 11/4/2005
    • TOUSSHORTDATETIME

      public static final DateDisplayFormatter TOUSSHORTDATETIME
      Short date with time in format MM/DD/YYYY HH:MM
      Example: 11/4/2005 11:03
    • TOEUROPEANSHORTDATE

      public static final DateDisplayFormatter TOEUROPEANSHORTDATE
      Short date in format DD/MM/YYYY.
      Example: 4/11/2005
    • TOEUROPEANSHORTDATETIME

      public static final DateDisplayFormatter TOEUROPEANSHORTDATETIME
      Short date with time in format DD/MM/YYYY HH:MM
      Example: 4/11/2005 11:03
    • TOJAPANSHORTDATE

      public static final DateDisplayFormatter TOJAPANSHORTDATE
      Short date in format YYYY/MM/DD.
      Example: 2005/11/4
    • TOJAPANSHORTDATETIME

      public static final DateDisplayFormatter TOJAPANSHORTDATETIME
      Short date with time in format YYYY/MM/DD HH:MM
      Example: 2005/11/4 11:03
    • TOSERIALIZEABLEDATE

      public static final DateDisplayFormatter TOSERIALIZEABLEDATE
      Date in the format YYYY-MM-DD HH:MM:SS
      Example: 2005-11-04 11:09:15
    • TODATESTAMP

      public static final DateDisplayFormatter TODATESTAMP
      Date in the format <YYYYMMDD>T<HHMMSS>Z Example: 20051104T111001Z
    • TOTIME

      public static final DateDisplayFormatter TOTIME
      String will display with seconds and am/pm indicator: [H]H:MM:SS am|pm.
      Example: 3:25:15 pm
    • TO24HOURTIME

      public static final DateDisplayFormatter TO24HOURTIME
      String will display with seconds in 24 hour time: [H]H:MM:SS.
      Example: 15:25:15
    • TOPADDEDTIME

      public static final DateDisplayFormatter 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

      public static final DateDisplayFormatter 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

      public static final DateDisplayFormatter TOSHORTTIME
      String will have no seconds and be in 12 hour format: [H]H:MM am|pm.
      Example: 3:25 pm
    • TOSHORT24HOURTIME

      public static final DateDisplayFormatter TOSHORT24HOURTIME
      String will have no seconds and be in 24 hour format: [H]H:MM.
      Example:15:25
    • TOSHORTPADDEDTIME

      public static final DateDisplayFormatter 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

      public static final DateDisplayFormatter 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

      public 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")

      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 value
      time - a Date instance representing logical time value
      Returns:
      a Date instance representing a datetime value combining the logical date and time passed
    • compareDates

      public 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. 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 compare
      date2 - second date to compare
      Returns:
      0 if equal, -1 if first date > second date, 1 if second date > first date
    • compareLogicalDates

      public 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.
      Parameters:
      date1 - first date to compare
      date2 - 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

      public static Date create()
      Create a new Date object - synonym for new Date(arguments)
      Returns:
      Date object
    • getAbsoluteDate

      public static Date getAbsoluteDate(RelativeDate relativeDate)
      Converts a RelativeDate, RelativeDateShortcut, or RelativeDateString to a concrete Date.
      Parameters:
      relativeDate - the relative date to convert
      Returns:
      resulting absolute date value
    • getAbsoluteDate

      public static Date getAbsoluteDate(RelativeDate relativeDate, Date baseDate)
      Converts a RelativeDate, RelativeDateShortcut, or RelativeDateString to a concrete Date.
      Parameters:
      relativeDate - the relative date to convert
      baseDate - base value for conversion. Defaults to the current date/time.
      Returns:
      resulting absolute date value
    • getDayNames

      public static String[] getDayNames()
      Return an array of the full names of each day, suitable for use in a selection list, etc. Day names are picked up from dayNames, 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 calling getShortDayNames().

      Returns:
      array of day names
    • getDefaultDateSeparator

      public static String getDefaultDateSeparator()
      gets the default date separator string
      Returns:
      the default date separator
    • getDisplayDay

      public static int getDisplayDay(Date datetime)
      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

      public static int getDisplayHours(Date datetime)
      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

      public static int getDisplayMinutes(Date datetime)
      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

      public static int getDisplayMonth(Date datetime)
      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

      public static int getDisplayYear(Date datetime)
      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

      public static Date getEndOf(Date date, String period)
      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 from
      period - 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

      public static Date getEndOf(Date date, String period, Boolean logicalDate)
      See Also:
    • getEndOf

      public static Date getEndOf(Date date, String period, Boolean logicalDate, Integer firstDayOfWeek)
      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 from
      period - the period to return the end of, one of mn/h/d/w/m/y
      logicalDate - process and return a logicalDate with no time element
      firstDayOfWeek - 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

      public static FiscalCalendar getFiscalCalendar()
      Returns the global FiscalCalendar object representing the start month and date of the fiscal year in the current locale.
      Returns:
      the FiscalCalendar object
    • getFiscalStartDate

      public static Date getFiscalStartDate(Date date)
      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

      public static Date getFiscalStartDate(Date date, FiscalCalendar fiscalCalendar)
      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
      fiscalCalendar - 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

      public static int getFiscalWeek(Date date)
      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

      public static int getFiscalWeek(Date date, FiscalCalendar fiscalCalendar)
      Returns a date's week-number, according to the fiscal calendar
      Parameters:
      date - the date to get the fiscal year for
      fiscalCalendar - the object representing the starts of fiscal years
      Returns:
      the fiscal week for the passed date
    • getFiscalYear

      public static FiscalYear getFiscalYear(Date date)
      Returns the FiscalYear 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

      public static FiscalYear getFiscalYear(Date date, FiscalCalendar fiscalCalendar)
      Returns the FiscalYear object for the fiscal year in which the passed date exists.
      Parameters:
      date - the date to get the fiscal year for
      fiscalCalendar - the object representing the start of the fiscal period
      Returns:
      the FiscalYear object for the passed date
    • getInputFormat

      public static String getInputFormat()
      Retrieves the default format for strings being parsed into dates via DateUtil.parseInput()
      Returns:
      the current inputFormat for dates
      See Also:
    • getShortDayNames

      public static String[] getShortDayNames(int length)
      Return an array of the short names of each day, suitable for use in a selection list, etc. Day names are picked up from a shortDayNames 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

      public static Date getStartOf(Date date, String period)
      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 from
      period - 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

      public static Date getStartOf(Date date, String period, Boolean logicalDate)
      See Also:
    • 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 from
      period - the period to return the start of, one of mn/h/d/w/m/y
      logicalDate - process and return a logicalDate with no time element
      firstDayOfWeek - 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

      public static Integer[] 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. Override weekendDays to accommodate different workweeks such as Saudi Arabia (Saturday -> Wednesday) or Israel (Sunday -> Thursday).
      Returns:
      array of weekend days
    • parseInput

      public static Date parseInput(String dateString)
      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

      public static Date parseInput(String dateString, String format)
      See Also:
    • parseInput

      public static Date parseInput(String dateString, String format, Integer centuryThreshold)
      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 string
      format - Format of the date string being passed. If not passed, the default date input format as set up via setInputFormat() will be used. See DateInputFormat
      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 19xx
      suppressConversion - 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

      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 - 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

      public 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.
      Parameters:
      fiscalCalendar - the object representing the start month and date of the fiscal year in the current locale
    • setInputFormat

      public static void setInputFormat(String format)
      Sets up the default system-wide input format for strings being parsed into dates via DateUtil.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() or getShortMonthNames(), 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 via formatAsShortDate() and formatAsShorDatetime().
      • 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. See DateInputFormat
      See Also:
    • setShortDisplayFormat

      public static void setShortDisplayFormat(String format)
      Set the default short format for dates. After calling this method, subsequent calls to formatAsShortDate 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 a FormatString, a DateDisplayFormat 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. See FormatString
    • setShowChooserFiscalYearPickers

      public static void setShowChooserFiscalYearPickers(boolean showChooserFiscalYearPickers)
      Sets the global attribute that dictates whether the choosers shelled from DateItems 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 the choosers shelled from DateItems show a UI for working with Weeks.
      Parameters:
      showChooserWeekPickers - whether to show Fiscal Week pickers in DateChoosers by default
    • setWeekendDays

      public static void setWeekendDays(Integer[] weekendDays)
      Sets the days that are considered weekend 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 a logicalDate representing the current day in the String.
    • getDayName

      public static String getDayName(Date date)
      Return the full day of week name for this date (Monday, Tuesday, etc). To modify the value returned by this method, use setDayNames(java.lang.String[]).
      Parameters:
      date -
      Returns:
      Day name
    • getShortDayName

      public String getShortDayName(Date date)
      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, use setShortDayNames(java.lang.String[]).
      Parameters:
      date -
      Returns:
      Abbreviated day name
    • getMonthName

      public String getMonthName(Date date)
      Return the full name of the month for this date (January, February, etc) To modify the value returned by this method, use com.smartgwt.client.util.DateUtil#setMonthNames().
      Parameters:
      date -
      Returns:
      Month name
    • getShortMonthName

      public String getShortMonthName(Date date)
      Return the abbreviated name of the month for this date (Jan, Feb, etc) To modify the value returned by this method, use com.smartgwt.client.util.DateUtil#setShortMonthNames().
      Parameters:
      date -
      Returns:
      Abbreviated month name (3 character string)
    • getShortYear

      public String getShortYear(Date date)
      Return a 2 digit year for this date.
      Parameters:
      date -
      Returns:
      year number, padded to 2 characters
    • toDateStamp

      public String toDateStamp(Date date)
      Return this date in the format (UTC timezone): YYYYMMDDTHHMMSS[Z]
      Parameters:
      date -
      Returns:
      formatted date string
    • 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
    • setNormalDatetimeDisplayFormatter

      public static void setNormalDatetimeDisplayFormatter(DateDisplayFormatter formatter)
      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

      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. SmartGWT includes several built-in DateDisplayFormatters for common formats - see DateDisplayFormatter 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 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. See com.smartgwt.client.data.DataSOurceField#format for details. Sample code :
       DateUtil.setNormalDateDisplayFormatter(new DateFormatStringFormatter("yyyy.MM.dd HH:mm:ss"));
       
      Parameters:
      formatter - the DateDisplayFormatter
    • 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)).

      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. SmartGWT includes several built-in DateDisplayFormatters for common formats - see DateDisplayFormatter 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

      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)).

      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. SmartGWT includes several built-in DateDisplayFormatters for common formats - see DateDisplayFormatter for details.

      Parameters:
      formatter - the DateDisplayFormatter
    • adjustDate

      public static Date adjustDate(Date baseDate, String relativeDateShortcut)
      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 - the RelativeDateShortcut or RelativeDateString string to convert
      Returns:
      resulting absolute date value
    • createDatetime

      public static Date createDatetime(Date baseDate)
      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

      public static Date createDatetime(Date baseDate, Integer month, Integer date)
      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 month
      date - 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 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.
      month - Integer month-number (0-11) - defaults to the month from the parameter baseDate
      date - Integer day of the month - defaults to the date from the parameter baseDate
      hour - Integer hours (0-23) - defaults to the hours form the parameter baseDate
      minute - Integer minutes (0-59) - defaults to the minutes form the parameter baseDate
      second - Integer seconds (0-59) - defaults to the seconds form the parameter baseDate
      millisecond - Integer milliseconds (0999) - defaults to the milliseconds form the parameter baseDate
      Returns:
      new Date instance in the current locale time
    • createDatetime

      public static Date createDatetime(Integer year, Integer month, Integer date)
      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 year
      month - 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 year
      month - Integer month-number (0-11)
      date - Integer day of the month
      hour - Integer hours (0-23) - defaults to zero
      minute - Integer minutes (0-59) - defaults to zero
      second - Integer seconds (0-59) - defaults to zero
      millisecond - Integer milliseconds (0-999) - defaults to zero
      Returns:
      resulting absolute date value
    • createLogicalDate

      public static LogicalDate 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

      public 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}. 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

      public static LogicalDate createLogicalDate(Date baseDate, Integer month, Integer date)
    • createLogicalDate

      public 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}. 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

      public static LogicalTime 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

      public 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}. 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

      public 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}. 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 instance
      minute - Integer minutes (0-59)
      second - Integer seconds (0-59)
      Returns:
      new LogicalTime representing the time in question
    • createLogicalTime

      public 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}. 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

      public 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").

      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

      public 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").

      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

      public static String formatAsShortDate(Date date)
      Format a date as a string according to the format specified by 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 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 setNormalDateDisplayFormatter(DateDisplayFormatter).

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

      Parameters:
      date -
      Returns:
    • formatAsNormalDate

      public static String formatAsNormalDate(Date date)
      Format a date as a string according to the format specified by setNormalDateDisplayFormatter(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

      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 the setShortDateDisplayFormatter(DateDisplayFormatter) and 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 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:

      Parameters:
      inputFormat -
    • clearDateParser

      public static void clearDateParser()
      If a custom system wide date parser has been specified via setDateParser(DateParser), clear this and revert to the standard date input format specified via setDateInputFormat(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 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(String relativeDateShortcut, Date baseDate, RelativeDateRangePosition rangePosition)
      Parameters:
      relativeDateShortcut - the RelativeDateShortcut or RelativeDateString string to convert
      baseDate - base value for conversion. Defaults to the current date/time.
      rangePosition - date-range position. Only has an effect if the date passed in is a RelativeDateShortcut where the range position is not implicit, such as "$yesterday"
      Returns:
      resulting absolute date value
      See Also:
    • setNormalTimeDisplayFormatter

      public static void setNormalTimeDisplayFormatter(DateDisplayFormatter formatter)
      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 is DateUtil.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. See com.smartgwt.client.data.DataSourceField#format for details. Sample code :

       DateUtil.setNormalTimeDisplayFormatter(new DateFormatStringFormatter("HH-mm-ss"));
       
      Parameters:
      formatter - the DateDisplayFormatter
    • setShortTimeDisplayFormatter

      public static void setShortTimeDisplayFormatter(DateDisplayFormatter formatter)
      Set up a system wide default short time formatting function. After calling this method, subsequent calls to isc.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 is DateUtil.TOSHORTTIME.

      SmartGWT includes several built-in DateDisplayFormatters for common formats - see DateDisplayFormatter for details.

      Parameters:
      formatter - the DateDisplayFormatter
    • format

      public static String format(Date date, String format)
      Format the parameter date using the parameter format string. This is a convenience method; it simply creates a DateFormatStringFormatter and calls its format() method
      Parameters:
      date - the Date to format
      format - the format string to use
      Returns:
      the parameter date formatted according to the parameter format string
    • setDayNames

      public static void setDayNames(String[] dayNames)
      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 the DateItem class).

      Note : This is an advanced setting

      Parameters:
      dayNames - a length 7 array of day names
    • setShortDayNames

      public static void setShortDayNames(String[] shortDayNames)
      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 the DateItem class).

      Note : This is an advanced setting

      Parameters:
      shortDayNames - a length 7 array of abbreviated day names
    • setMonthNames

      public static void setMonthNames(String[] monthNames)
      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 the DateItem class).

      Note : This is an advanced setting

      Parameters:
      monthNames - a length 12 array of month names
    • setShortMonthNames

      public static void setShortMonthNames(String[] shortMonthNames)
      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 the DateItem class).

      Note : This is an advanced setting

      Parameters:
      shortMonthNames - a length 12 array of abbreviated month names
    • getFiscalStartDate

      public static Date getFiscalStartDate(Integer year, FiscalCalendar fiscalCalendar)
      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.