All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.joda.time.format.PeriodFormat Maven / Gradle / Ivy

There is a newer version: 1.5.7
Show newest version
/*
 *  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;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Factory that creates instances of PeriodFormatter.
 * 

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

* PeriodFormat is thread-safe and immutable, and the formatters it returns * are as well. * * @author Brian S O'Neill * @since 1.0 * @see ISOPeriodFormat * @see 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 FORMATTERS = new ConcurrentHashMap(); /** * Constructor. * * @since 1.1 (previously private) */ protected PeriodFormat() { super(); } //----------------------------------------------------------------------- /** * Gets the default formatter that outputs words in English. *

* This calls {@link #wordBased(Locale)} using a locale of {@code ENGLISH}. * * @return the formatter, not null */ public static PeriodFormatter getDefault() { return wordBased(Locale.ENGLISH); } /** * Returns a word based formatter for the JDK default locale. *

* This calls {@link #wordBased(Locale)} using the {@link Locale#getDefault() default locale}. * * @return 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 - * {@code org.joda.time.format.messages}. * This can be added to via the normal classpath resource bundle mechanisms. *

* Available languages are English, Danish, Dutch, French, German, Japanese, Portuguese, and Spanish. * * @return the formatter, not null * @since 2.0 */ public static PeriodFormatter wordBased(Locale locale) { PeriodFormatter pf = FORMATTERS.get(locale); if (pf == null) { ResourceBundle b = ResourceBundle.getBundle(BUNDLE_NAME, 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(); FORMATTERS.putIfAbsent(locale, pf); } return pf; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy