Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.format;
 
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.

Author(s):
Brian S O'Neill
Since:
1.0
See also:
ISOPeriodFormat
PeriodFormatterBuilder
 
 public class PeriodFormat {

    
The resource bundle name.
 
     private static final String BUNDLE_NAME = "org.joda.time.format.messages";
    
The created formatters.
 
     private static final ConcurrentMap<LocalePeriodFormatterFORMATTERS = new ConcurrentHashMap<LocalePeriodFormatter>();

    
Constructor.

Since:
1.1 (previously private)
 
     protected PeriodFormat() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Gets the default formatter that outputs words in English.

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

Returns:
the formatter, not null
 
     public static PeriodFormatter getDefault() {
         return wordBased(.);
     }

    
Returns a word based formatter for the JDK default locale.

This calls wordBased(java.util.Locale) using the default locale.

Returns:
the formatter, not null
Since:
2.0
 
     public static PeriodFormatter wordBased() {
         return wordBased(Locale.getDefault());
     }

    
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.

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

Returns:
the formatter, not null
Since:
2.0
 
     public static PeriodFormatter wordBased(Locale locale) {
         PeriodFormatter pf = .get(locale);
         if (pf == null) {
             ResourceBundle b = ResourceBundle.getBundle(locale);
             String[] variants = {
                     b.getString("PeriodFormat.space"), b.getString("PeriodFormat.comma"),
                    b.getString("PeriodFormat.commandand"), b.getString("PeriodFormat.commaspaceand")};
            pf = new PeriodFormatterBuilder()
                .appendYears()
                .appendSuffix(b.getString("PeriodFormat.year"), b.getString("PeriodFormat.years"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendMonths()
                .appendSuffix(b.getString("PeriodFormat.month"), b.getString("PeriodFormat.months"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendWeeks()
                .appendSuffix(b.getString("PeriodFormat.week"), b.getString("PeriodFormat.weeks"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendDays()
                .appendSuffix(b.getString("PeriodFormat.day"), b.getString("PeriodFormat.days"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendHours()
                .appendSuffix(b.getString("PeriodFormat.hour"), b.getString("PeriodFormat.hours"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendMinutes()
                .appendSuffix(b.getString("PeriodFormat.minute"), b.getString("PeriodFormat.minutes"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendSeconds()
                .appendSuffix(b.getString("PeriodFormat.second"), b.getString("PeriodFormat.seconds"))
                .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
                .appendMillis()
                .appendSuffix(b.getString("PeriodFormat.millisecond"), b.getString("PeriodFormat.milliseconds"))
                .toFormatter();
            .putIfAbsent(localepf);
        }
        return pf;
    }
New to GrepCode? Check out our FAQ X