Class PeriodFormat


  • public class PeriodFormat
    extends java.lang.Object
    Factory that creates instances of PeriodFormatter.

    Period formatting is performed by the PeriodFormatter class. Three classes provide factory methods to create formatters, and this is one. The others are ISOPeriodFormat and PeriodFormatterBuilder.

    PeriodFormat is thread-safe and immutable, and the formatters it returns are as well.

    Since:
    1.0
    See Also:
    ISOPeriodFormat, PeriodFormatterBuilder
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  PeriodFormat.DynamicWordBased
      Printer/parser that reacts to the locale and changes the word-based pattern if necessary.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String BUNDLE_NAME
      The resource bundle name.
      private static java.util.concurrent.ConcurrentMap<java.util.Locale,​PeriodFormatter> FORMATTERS
      The created formatters.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected PeriodFormat()
      Constructor.
    • Field Detail

      • BUNDLE_NAME

        private static final java.lang.String BUNDLE_NAME
        The resource bundle name.
        See Also:
        Constant Field Values
      • FORMATTERS

        private static final java.util.concurrent.ConcurrentMap<java.util.Locale,​PeriodFormatter> FORMATTERS
        The created formatters.
    • Constructor Detail

      • PeriodFormat

        protected PeriodFormat()
        Constructor.
        Since:
        1.1 (previously private)
    • Method Detail

      • getDefault

        public static PeriodFormatter getDefault()
        Gets the default formatter that outputs words in English.

        This calls wordBased(Locale) using a locale of ENGLISH.

        Returns:
        the formatter, not null
      • wordBased

        public static PeriodFormatter wordBased()
        Returns a word based formatter for the JDK default locale.

        This calls wordBased(Locale) using the default locale.

        Returns:
        the formatter, not null
        Since:
        2.0
      • wordBased

        public static PeriodFormatter wordBased​(java.util.Locale locale)
        Returns a word based formatter for the specified locale.

        The words are configured in a resource bundle text file - org.joda.time.format.messages. This can be added to via the normal classpath resource bundle mechanisms.

        You can add your own translation by creating messages_.properties file and adding it to the org.joda.time.format.messages path.

        Simple example (1 -> singular suffix, not 1 -> plural suffix):

         PeriodFormat.space=\ 
         PeriodFormat.comma=,
         PeriodFormat.commandand=,and 
         PeriodFormat.commaspaceand=, and 
         PeriodFormat.commaspace=, 
         PeriodFormat.spaceandspace=\ and 
         PeriodFormat.year=\ year
         PeriodFormat.years=\ years
         PeriodFormat.month=\ month
         PeriodFormat.months=\ months
         PeriodFormat.week=\ week
         PeriodFormat.weeks=\ weeks
         PeriodFormat.day=\ day
         PeriodFormat.days=\ days
         PeriodFormat.hour=\ hour
         PeriodFormat.hours=\ hours
         PeriodFormat.minute=\ minute
         PeriodFormat.minutes=\ minutes
         PeriodFormat.second=\ second
         PeriodFormat.seconds=\ seconds
         PeriodFormat.millisecond=\ millisecond
         PeriodFormat.milliseconds=\ milliseconds
         

        Some languages contain more than two suffixes. You can use regular expressions for them. Here's an example using regular expression for English:

         PeriodFormat.space=\ 
         PeriodFormat.comma=,
         PeriodFormat.commandand=,and 
         PeriodFormat.commaspaceand=, and 
         PeriodFormat.commaspace=, 
         PeriodFormat.spaceandspace=\ and 
         PeriodFormat.regex.separator=%
         PeriodFormat.years.regex=1$%.*
         PeriodFormat.years.list=\ year%\ years
         PeriodFormat.months.regex=1$%.*
         PeriodFormat.months.list=\ month%\ months
         PeriodFormat.weeks.regex=1$%.*
         PeriodFormat.weeks.list=\ week%\ weeks
         PeriodFormat.days.regex=1$%.*
         PeriodFormat.days.list=\ day%\ days
         PeriodFormat.hours.regex=1$%.*
         PeriodFormat.hours.list=\ hour%\ hours
         PeriodFormat.minutes.regex=1$%.*
         PeriodFormat.minutes.list=\ minute%\ minutes
         PeriodFormat.seconds.regex=1$%.*
         PeriodFormat.seconds.list=\ second%\ seconds
         PeriodFormat.milliseconds.regex=1$%.*
         PeriodFormat.milliseconds.list=\ millisecond%\ milliseconds
         

        You can mix both approaches. Here's example for Polish ( "1 year, 2 years, 5 years, 12 years, 15 years, 21 years, 22 years, 25 years" translates to "1 rok, 2 lata, 5 lat, 12 lat, 15 lat, 21 lat, 22 lata, 25 lat"). Notice that PeriodFormat.day and PeriodFormat.days is used for day suffixes as there is no need for regular expressions:

         PeriodFormat.space=\ 
         PeriodFormat.comma=,
         PeriodFormat.commandand=,i 
         PeriodFormat.commaspaceand=, i 
         PeriodFormat.commaspace=, 
         PeriodFormat.spaceandspace=\ i 
         PeriodFormat.regex.separator=%
         PeriodFormat.years.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.years.list=\ rok%\ lata%\ lat
         PeriodFormat.months.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.months.list=\ miesiąc%\ miesiące%\ miesięcy
         PeriodFormat.weeks.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.weeks.list=\ tydzień%\ tygodnie%\ tygodni
         PeriodFormat.day=\ dzień
         PeriodFormat.days=\ dni
         PeriodFormat.hours.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.hours.list=\ godzina%\ godziny%\ godzin
         PeriodFormat.minutes.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.minutes.list=\ minuta%\ minuty%\ minut
         PeriodFormat.seconds.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.seconds.list=\ sekunda%\ sekundy%\ sekund
         PeriodFormat.milliseconds.regex=^1$%[0-9]*(?<!1)[2-4]$%[0-9]*
         PeriodFormat.milliseconds.list=\ milisekunda%\ milisekundy%\ milisekund
         

        Each PeriodFormat.<duration_field_type>.regex property stands for an array of regular expressions and is followed by a property PeriodFormat.<duration_field_type>.list holding an array of suffixes. PeriodFormat.regex.separator is used for splitting. See PeriodFormatterBuilder.appendSuffix(String[], String[]) for details.

        Available languages are English, Danish, Dutch, French, German, Japanese, Polish, Portuguese and Spanish.

        Returns:
        the formatter, not null
        Since:
        2.0, regex since 2.5
      • buildWordBased

        private static PeriodFormatter buildWordBased​(java.util.Locale locale)
      • buildRegExFormatter

        private static PeriodFormatter buildRegExFormatter​(java.util.ResourceBundle b,
                                                           java.util.Locale locale)
      • buildNonRegExFormatter

        private static PeriodFormatter buildNonRegExFormatter​(java.util.ResourceBundle b,
                                                              java.util.Locale locale)
      • retrieveVariants

        private static java.lang.String[] retrieveVariants​(java.util.ResourceBundle b)
      • containsKey

        private static boolean containsKey​(java.util.ResourceBundle bundle,
                                           java.lang.String key)