Class DateFormats


  • public class DateFormats
    extends java.lang.Object
    DateFormat relevant utilities.
    Author:
    tomyeh
    • Constructor Summary

      Constructors 
      Constructor Description
      DateFormats()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String format​(java.util.Date d, boolean dateOnly)
      Formats a Date object based on the current Locale and the default date format (DateFormat.DEFAULT.
      static java.lang.String getDateFormat​(int style, java.util.Locale locale, java.lang.String defaultFormat)
      Returns the current date format; never null.
      static java.lang.String getDateTimeFormat​(int dateStyle, int timeStyle, java.util.Locale locale, java.lang.String defaultFormat)
      Returns the current date/time format; never null.
      static DateFormatInfo getLocalFormatInfo()
      Returns the info of date/time format for the current thread, or null if not set.
      static java.lang.String getTimeFormat​(int style, java.util.Locale locale, java.lang.String defaultFormat)
      Returns the current time format; never null.
      static java.util.Date parse​(java.lang.String s, boolean dateOnly)
      Parses a string, that is formatted by format(java.util.Date, boolean), to a date.
      static DateFormatInfo setLocalFormatInfo​(DateFormatInfo info)
      Sets the info of date/time format for the current thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DateFormats

        public DateFormats()
    • Method Detail

      • 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 java.lang.String getDateFormat​(int style,
                                                     java.util.Locale locale,
                                                     java.lang.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 java.lang.String getTimeFormat​(int style,
                                                           java.util.Locale locale,
                                                           java.lang.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 java.lang.String getDateTimeFormat​(int dateStyle,
                                                               int timeStyle,
                                                               java.util.Locale locale,
                                                               java.lang.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 java.lang.String format​(java.util.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 java.util.Date parse​(java.lang.String s,
                                                 boolean dateOnly)
                                          throws java.text.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:
        java.text.ParseException
        Since:
        5.0.5