Class DateFormats

java.lang.Object
org.zkoss.text.DateFormats

public class DateFormats extends Object
DateFormat relevant utilities.
Author:
tomyeh
  • Constructor Details

    • DateFormats

      public DateFormats()
  • Method Details

    • setLocalFormatInfo

      public static DateFormatInfo setLocalFormatInfo(DateFormatInfo info)
      Sets the info of date/time format for the current thread. It does not have effect on other threads.

      When Invoking this method under a thread, remember to clean up the setting upon completing each request.

      DateFormatInfo old = DateFormats.setLocalFormatFormatInfo(info);
      try { 
        ...
      } finally {
        DateFormats.setLocalFormatInfo(old);
      }
      Parameters:
      info - the format info. It could be null (and the JVM's default is used).
      Returns:
      the previous format info being set, or null if not set.
      Since:
      5.0.7
    • getLocalFormatInfo

      public static DateFormatInfo getLocalFormatInfo()
      Returns the info of date/time format for the current thread, or null if not set.
      Since:
      5.0.7
    • getDateFormat

      public static String getDateFormat(int style, Locale locale, String defaultFormat)
      Returns the current date format; never null.

      If a format info is set by setLocalFormatInfo(org.zkoss.text.DateFormatInfo), DateFormatInfo.getDateFormat(int, java.util.Locale) of the info will be called first. If no info is set or the info returns null, java.text.DateFormat.getDateInstance(style, locale) will be called instead. If no format is found and the value of defaultFormat is not null, defaultFormat will be returned. Otherwise, "M/d/yy" is returned.

      Parameters:
      style - the giving formatting style. For example, DateFormat.SHORT for "M/d/yy" in the US locale.
      locale - the locale. If null, Locales.getCurrent() is assumed.
      defaultFormat - the default format. It is used if no default format. If null, "M/d/yy" is assumed.
      Since:
      5.0.7
    • getTimeFormat

      public static final String getTimeFormat(int style, Locale locale, String defaultFormat)
      Returns the current time format; never null.

      If a format info is set by setLocalFormatInfo(org.zkoss.text.DateFormatInfo), DateFormatInfo.getTimeFormat(int, java.util.Locale) of the info will be called first. If no info is set or the info returns null, java.text.DateFormat.getTimeInstance(style, locale) will be called instead. If no format is found and the value of defaultFormat is not null, defaultFormat will be returned. Otherwise, "h:mm a" is returned.

      Parameters:
      style - the giving formatting style. For example, DateFormat.SHORT for "h:mm a" in the US locale.
      locale - the locale. If null, Locales.getCurrent() is assumed.
      defaultFormat - the default format. It is used if no default format. If null, "h:mm a" is assumed.
      Since:
      5.0.7
    • getDateTimeFormat

      public static final String getDateTimeFormat(int dateStyle, int timeStyle, Locale locale, String defaultFormat)
      Returns the current date/time format; never null.

      If a format info is set by setLocalFormatInfo(org.zkoss.text.DateFormatInfo), DateFormatInfo.getDateTimeFormat(int, int, java.util.Locale) of the info will be called first. If no info is set or the info returns null, java.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale) will be called instead. If no format is found and the value of defaultFormat is not null, defaultFormat will be returned. Otherwise, "M/d/yy h:mm a" is returned.

      Parameters:
      dateStyle - the giving formatting style. For example, DateFormat.SHORT for "M/d/yy" in the US locale.
      timeStyle - the giving formatting style. For example, DateFormat.SHORT for "h:mm a" in the US locale.
      locale - the locale. If null, Locales.getCurrent() is assumed.
      Since:
      5.0.7
    • format

      public static final String format(Date d, boolean dateOnly)
      Formats a Date object based on the current Locale and the default date format (DateFormat.DEFAULT.
      Parameters:
      dateOnly - indicates whether to show only date; false to show both date and time
    • parse

      public static final Date parse(String s, boolean dateOnly) throws ParseException
      Parses a string, that is formatted by format(java.util.Date, boolean), to a date. It assumes the current locale is Locales.getCurrent().
      Parameters:
      dateOnly - indicates whether to show only date; false to show both date and time
      Throws:
      ParseException
      Since:
      5.0.5