org.spiffyui.client
Class JSDateUtil

java.lang.Object
  extended by org.spiffyui.client.JSDateUtil

public final class JSDateUtil
extends java.lang.Object

A collection of utility methods for manipulating dates in the browser.

JSDateUtil provide flexible locale aware support for formatting dates and times in GWT. This class fills in many of the gaps missing from the java.text package from the JDK.

In addition to standard date formatting this library supports custom formatting following the Spiffy UI date formatters specification.


Method Summary
static java.util.Date dateAdd(java.util.Date date, int amt, java.lang.String unit)
          Add to a specified date
static java.lang.String dateAdd(java.lang.String epochDate, int amt, java.lang.String unit)
          Add to a specified epoch date
static java.lang.String format(java.util.Date date, java.lang.String format)
          Format the specified date according to the format string.
static java.lang.String format(java.lang.String epochDate, java.lang.String format)
          Format the specified date according to the format string.
static java.lang.String getDate(java.util.Date date)
          Convert java.util.Date to Date format in the current locale
static java.lang.String getDate(java.util.Date date, java.lang.String format)
          Convert UTC epoch format to a date string matching the specified format.
static java.lang.String getDate(java.lang.String epochDate)
          Convert UTC epoch format to short Date format in the current locale
static java.lang.String getDate(java.lang.String epochDate, java.lang.String format)
          Convert UTC epoch format to a date string matching the specified format.
static java.lang.String getEpoch(java.lang.String dateString)
          Convert Date to UTC Epoch format
static java.lang.String getFullDateTime(java.util.Date date)
          Convert java.util.Date to Full Date Time format in the current locale
static java.lang.String getFullDateTime(java.lang.String epochDate)
          Convert UTC epoch format to Date Time format in the current locale
static java.lang.String getFullDateTimeFormat()
          Gets the full date time format string in the current locale.
static int getHours(java.lang.String time)
          Gets the hours from the specified short time string
static java.lang.String getLocale()
           Gets the current locale of the application running in the browser.
static java.lang.String getLongDate(java.lang.String epochDate)
          Convert UTC epoch format to Date format in the current locale
static java.lang.String getLongDateFormat()
          Gets the long date format string in the current locale.
static int getMinutes(java.lang.String time)
          Gets the minutes from the specified short time string
static java.lang.String getMonthDay(java.lang.String epochDate, boolean abbrev)
          Convert UTC epoch format to Month Day format in the current locale
static java.lang.String getMonthDayFormat()
          Gets the month day format string in the current locale.
static int getOrdinalNumber(java.util.Date date)
          Get the Ordinal day (numeric day number) of the year, adjusted for leap year.
static int getOrdinalNumber(java.lang.String epochDate)
          Get the Ordinal day (numeric day number) of the year, adjusted for leap year.
static java.lang.String getShortDate(java.lang.String epochDate)
           Convert UTC epoch format to short Date format in the current locale
static java.lang.String getShortDateFormat()
          Gets the short date format string in the current locale.
static java.lang.String getShortDateTime(java.util.Date date)
          Converts a java.util.Date to a concatenation of the Short Date and Short Time format in the current locale
static java.lang.String getShortMonthDayTime(java.lang.String epochDate)
          Convert UTC epoch format to Month Day format in the current locale concatenated with the Short Time
static java.lang.String getShortTime(java.util.Date date)
          Convert java.util.Date to Short Time format in the current locale
static java.lang.String getShortTime(java.lang.String epochDate)
          Convert UTC epoch format to short time format in the current locale
static java.lang.String getShortTimeFormat()
          Gets the short time format string in the current locale.
static java.lang.String getShortTimeRounded(java.lang.String epochDate)
          Convert UTC epoch format to short time format in the current locale rounded up to the nearest 30 minutes.
static java.lang.String getToday()
          Convert UTC epoch format to Date format in the current locale
static int getUTCOffset()
          Get the UTC offset of the current browser.
static boolean is24Time()
          Determines if the browser's current locale uses 24 hour time
static java.lang.String nextDay()
          Adds the 1 day to today
static java.util.Date parseShortDate(java.lang.String dateString)
          Convert the specified String to a date object using the short format of the current locale
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDate

public static java.lang.String getDate(java.util.Date date)
Convert java.util.Date to Date format in the current locale

Parameters:
date - - the Date object to convert
Returns:
- Date format in String

getShortDate

public static java.lang.String getShortDate(java.lang.String epochDate)

Convert UTC epoch format to short Date format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date format in String

getDate

public static java.lang.String getDate(java.lang.String epochDate)
Convert UTC epoch format to short Date format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date format in String

getDate

public static java.lang.String getDate(java.util.Date date,
                                       java.lang.String format)
Convert UTC epoch format to a date string matching the specified format. See Format Specifiers for more details

Parameters:
date - - the date object to format
format - - the formatter for the date
Returns:
- Time format as a String

format

public static java.lang.String format(java.util.Date date,
                                      java.lang.String format)
Format the specified date according to the format string. See Format Specifiers

Parameters:
date - - the date object to format
format - - the formatter for the date
Returns:
- the formatted string

format

public static java.lang.String format(java.lang.String epochDate,
                                      java.lang.String format)
Format the specified date according to the format string. See Format Specifiers

Parameters:
epochDate - - the long date to format
format - - the formatter for the date
Returns:
- the formatted string

getDate

public static java.lang.String getDate(java.lang.String epochDate,
                                       java.lang.String format)
Convert UTC epoch format to a date string matching the specified format. See Format Specifiers for more details

Parameters:
epochDate - - the time in milliseconds since Jan , 1, 1970
format - - the formatter for the date
Returns:
- Time format as a String

getLongDate

public static java.lang.String getLongDate(java.lang.String epochDate)
Convert UTC epoch format to Date format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date format in String

getToday

public static java.lang.String getToday()
Convert UTC epoch format to Date format in the current locale

Returns:
- Date format in String

nextDay

public static java.lang.String nextDay()
Adds the 1 day to today

Returns:
- epochDate time in milliseconds String

getShortTime

public static java.lang.String getShortTime(java.util.Date date)
Convert java.util.Date to Short Time format in the current locale

Parameters:
date - - the Date object to convert
Returns:
- Time format in String

getShortTime

public static java.lang.String getShortTime(java.lang.String epochDate)
Convert UTC epoch format to short time format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Time format in String

getShortTimeRounded

public static java.lang.String getShortTimeRounded(java.lang.String epochDate)
Convert UTC epoch format to short time format in the current locale rounded up to the nearest 30 minutes.

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date format in String

getHours

public static int getHours(java.lang.String time)
Gets the hours from the specified short time string

Parameters:
time - - the time 11:30pm
Returns:
- the hours

getMinutes

public static int getMinutes(java.lang.String time)
Gets the minutes from the specified short time string

Parameters:
time - - the time 11:30pm
Returns:
- the hours

getEpoch

public static java.lang.String getEpoch(java.lang.String dateString)
Convert Date to UTC Epoch format

Parameters:
dateString - - the String to test
Returns:
- Time in epoch format

getShortDateFormat

public static java.lang.String getShortDateFormat()
Gets the short date format string in the current locale. In en-US this is M/d/yyyy in fr_FR it is dd/MM/yyyy

Returns:
- The date format

getLongDateFormat

public static java.lang.String getLongDateFormat()
Gets the long date format string in the current locale. In en-US this is dddd, MMMM dd, yyyy in fr_FR it is dddd d MMMM yyyy

Returns:
- The date format

getMonthDayFormat

public static java.lang.String getMonthDayFormat()
Gets the month day format string in the current locale. In en-US this is MMMM dd in fr_FR it is d MMMM

Returns:
- The date format

getShortTimeFormat

public static java.lang.String getShortTimeFormat()
Gets the short time format string in the current locale. In en-US this is h:mm tt in fr_FR it is HH:mm

Returns:
- The time format

getLocale

public static java.lang.String getLocale()

Gets the current locale of the application running in the browser.

This methods returns the locale being used by GWT if the GWTLocaleFilter is used in the application. This locale is the best match between the user's preferred locale and the locales supported in the application. The user's preferred locale may be different than the installed locale of the browser.

For example, if the application is localized into English (en), French (fr), and Japanese (ja) the the user will get the follow locales:

Example 1

The user's list of preferred locales is:

  1. ko_KR (South Korean)
  2. ja_JP (Japanese)
  3. en (English)

In this example the locale would be Japanese.

Example 2

The user's list of preferred locales is:

  1. en_GB (English - United Kingdom)
  2. fr (French)
  3. en_US (English - United States)

In this example the user will get French because that is the closest exact match. If the application was localized into English - United Kingdom (en_GB) then the user would get that locale.

Example 3

The user's list of preferred locales is:

  1. iw_IL (Hebrew Israel)
  2. is_IS (Icelandic Iceland)

In this example the user will get English because none of their preferred locales are supported and English is the default.

For more information on these processes see section 14.4 of the HTTP specification.

Returns:
The client Locale

parseShortDate

public static java.util.Date parseShortDate(java.lang.String dateString)
Convert the specified String to a date object using the short format of the current locale

Parameters:
dateString - - the String to convert
Returns:
- The Date object

is24Time

public static boolean is24Time()
Determines if the browser's current locale uses 24 hour time

Returns:
True if the locale uses 24 time and false otherwise

getFullDateTimeFormat

public static java.lang.String getFullDateTimeFormat()
Gets the full date time format string in the current locale. In en-US this is dddd, MMMM dd, yyyy h:mm:ss tt

Returns:
- The time format

getFullDateTime

public static java.lang.String getFullDateTime(java.util.Date date)
Convert java.util.Date to Full Date Time format in the current locale

Parameters:
date - - the Date object to convert
Returns:
- Date Time format in String

getFullDateTime

public static java.lang.String getFullDateTime(java.lang.String epochDate)
Convert UTC epoch format to Date Time format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date Time format in String

getShortDateTime

public static java.lang.String getShortDateTime(java.util.Date date)
Converts a java.util.Date to a concatenation of the Short Date and Short Time format in the current locale

Parameters:
date - - the Date object to convert
Returns:
- Short Date and Short Time format in String

dateAdd

public static java.util.Date dateAdd(java.util.Date date,
                                     int amt,
                                     java.lang.String unit)
Add to a specified date

Parameters:
date - - a Date to start
amt - - the amount to add
unit - - the unit to add. Can be WEEK, MONTH, YEAR, HOUR, MINUTE, SECOND or defaults to DAY
Returns:
the new date

dateAdd

public static java.lang.String dateAdd(java.lang.String epochDate,
                                       int amt,
                                       java.lang.String unit)
Add to a specified epoch date

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
amt - - the amount to add
unit - - the unit to add. Can be WEEK, MONTH, YEAR, HOUR, MINUTE, SECOND or defaults to DAY
Returns:
the new date as an epoch

getMonthDay

public static java.lang.String getMonthDay(java.lang.String epochDate,
                                           boolean abbrev)
Convert UTC epoch format to Month Day format in the current locale

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
abbrev - - if true, then abbreviate the month
Returns:
- Date format in String

getShortMonthDayTime

public static java.lang.String getShortMonthDayTime(java.lang.String epochDate)
Convert UTC epoch format to Month Day format in the current locale concatenated with the Short Time

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
- Date format in String

getOrdinalNumber

public static int getOrdinalNumber(java.util.Date date)
Get the Ordinal day (numeric day number) of the year, adjusted for leap year. Returns 1 through 365 (366 in leap years).

Parameters:
date - - a Date
Returns:
the day of year

getOrdinalNumber

public static int getOrdinalNumber(java.lang.String epochDate)
Get the Ordinal day (numeric day number) of the year, adjusted for leap year. Returns 1 through 365 (366 in leap years).

Parameters:
epochDate - - the time in milliseconds since Jan 1, 1970
Returns:
the day of year

getUTCOffset

public static int getUTCOffset()
Get the UTC offset of the current browser. This returns a number in hours like -5 or 8

Returns:
the UTC offset