Package com.vaadin.client
Class DateTimeService
- java.lang.Object
-
- com.vaadin.client.DateTimeService
-
public class DateTimeService extends Object
This class provides date/time parsing services to all components on the client side.- Author:
- Vaadin Ltd.
-
-
Constructor Summary
Constructors Constructor Description DateTimeService()Creates a new date time service with the application default locale.DateTimeService(String locale)Creates a new date time service with a given locale.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringasTwoDigits(int i)Utility method to format positive int as zero-padded two-digits number.StringformatDate(Date date, String formatStr)Check if format contains the month name.StringformatDate(Date date, String formatStr, com.google.gwt.i18n.client.TimeZone timeZone)Check if format contains the month name.String[]getAmPmStrings()StringgetClockDelimeter()StringgetDay(int day)intgetFirstDayOfWeek()Returns the first day of the week, according to the used Locale.static intgetISOWeekNumber(Date date)Returns the ISO-8601 week number of the given date.StringgetLocale()static intgetMilliseconds(Date date)StringgetMonth(int month)static intgetNumberOfDaysInMonth(Date date)StringgetShortDay(int day)Returns the localized short name of the specified day.StringgetShortMonth(int month)intgetStartWeekDay(Date month)Returns the first day of week of the specifiedmonth.static booleanisInRange(Date date, Date rangeStart, Date rangeEnd, DateResolution resolution)static booleanisLeapYear(Date date)static booleanisSameDay(Date d1, Date d2)booleanisTwelveHourClock()Returns whether the locale has twelve hour, or twenty four hour clock.DateparseDate(String dateString, String formatString, boolean lenient)Parses the given date string using the given format string and the locale set in this DateTimeService instance.voidsetLocale(String locale)static voidsetMilliseconds(Date date, int ms)
-
-
-
Constructor Detail
-
DateTimeService
public DateTimeService()
Creates a new date time service with the application default locale.
-
DateTimeService
public DateTimeService(String locale) throws LocaleNotLoadedException
Creates a new date time service with a given locale.- Parameters:
locale- e.g.fi,en, etc.- Throws:
LocaleNotLoadedException
-
-
Method Detail
-
asTwoDigits
public static String asTwoDigits(int i)
Utility method to format positive int as zero-padded two-digits number.- Parameters:
i- the value- Returns:
- "00".."99"
- Since:
- 8.4
-
setLocale
public void setLocale(String locale) throws LocaleNotLoadedException
- Throws:
LocaleNotLoadedException
-
getLocale
public String getLocale()
-
getMonth
public String getMonth(int month)
-
getShortMonth
public String getShortMonth(int month)
-
getDay
public String getDay(int day)
-
getShortDay
public String getShortDay(int day)
Returns the localized short name of the specified day.- Parameters:
day- the day,0isSUNDAY- Returns:
- the localized short name
-
getFirstDayOfWeek
public int getFirstDayOfWeek()
Returns the first day of the week, according to the used Locale.- Returns:
- the localized first day of the week,
0isSUNDAY
-
isTwelveHourClock
public boolean isTwelveHourClock()
Returns whether the locale has twelve hour, or twenty four hour clock.- Returns:
trueif the locale has twelve hour clock,falsefor twenty four clock
-
getClockDelimeter
public String getClockDelimeter()
-
getAmPmStrings
public String[] getAmPmStrings()
-
getStartWeekDay
public int getStartWeekDay(Date month)
Returns the first day of week of the specifiedmonth.- Parameters:
month- the month, notnull- Returns:
- the first day of week,
-
setMilliseconds
public static void setMilliseconds(Date date, int ms)
-
getMilliseconds
public static int getMilliseconds(Date date)
-
getNumberOfDaysInMonth
public static int getNumberOfDaysInMonth(Date date)
-
isLeapYear
public static boolean isLeapYear(Date date)
-
isInRange
public static boolean isInRange(Date date, Date rangeStart, Date rangeEnd, DateResolution resolution)
-
getISOWeekNumber
public static int getISOWeekNumber(Date date)
Returns the ISO-8601 week number of the given date.- Parameters:
date- The date for which the week number should be resolved- Returns:
- The ISO-8601 week number for date
-
formatDate
public String formatDate(Date date, String formatStr)
Check if format contains the month name. If it does we manually convert it to the month name since DateTimeFormat.format always uses the current locale and will replace the month name wrong if current locale is different from the locale set for the DateField. MMMM is converted into long month name, MMM is converted into short month name. '' are added around the name to avoid that DateTimeFormat parses the month name as a pattern.- Parameters:
date- The date to convertformatStr- The format string that might contain MMM or MMMM- Returns:
-
formatDate
public String formatDate(Date date, String formatStr, com.google.gwt.i18n.client.TimeZone timeZone)
Check if format contains the month name. If it does we manually convert it to the month name since DateTimeFormat.format always uses the current locale and will replace the month name wrong if current locale is different from the locale set for the DateField. MMMM is converted into long month name, MMM is converted into short month name. '' are added around the name to avoid that DateTimeFormat parses the month name as a pattern. z is converted into the time zone name, using the specifiedtimeZoneJSON- Parameters:
date- The date to convertformatStr- The format string that might containMMMorMMMMtimeZone- TheTimeZoneused to replacez, can benull- Returns:
- the formatted date string
- Since:
- 8.2
-
parseDate
public Date parseDate(String dateString, String formatString, boolean lenient) throws IllegalArgumentException
Parses the given date string using the given format string and the locale set in this DateTimeService instance.- Parameters:
dateString- Date string e.g. "1 February 2010"formatString- Format string e.g. "d MMMM yyyy"lenient- true to use lenient parsing, false to use strict parsing- Returns:
- A Date object representing the dateString. Never returns null.
- Throws:
IllegalArgumentException- if the parsing fails
-
-