001 /*
002 * Copyright 2001-2005 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time.format;
017
018 /**
019 * Factory that creates instances of PeriodFormatter.
020 * <p>
021 * Period formatting is performed by the {@link PeriodFormatter} class.
022 * Three classes provide factory methods to create formatters, and this is one.
023 * The others are {@link ISOPeriodFormat} and {@link PeriodFormatterBuilder}.
024 * <p>
025 * PeriodFormat is thread-safe and immutable, and the formatters it returns
026 * are as well.
027 *
028 * @author Brian S O'Neill
029 * @since 1.0
030 * @see ISOPeriodFormat
031 * @see PeriodFormatterBuilder
032 */
033 public class PeriodFormat {
034
035 /** An english words based formatter. */
036 private static PeriodFormatter cEnglishWords;
037
038 /**
039 * Constructor.
040 *
041 * @since 1.1 (previously private)
042 */
043 protected PeriodFormat() {
044 super();
045 }
046
047 //-----------------------------------------------------------------------
048 /**
049 * Gets the default PeriodFormatter.
050 * <p>
051 * This currently returns a word based formatter using English only.
052 * Hopefully future release will support localized period formatting.
053 *
054 * @return the formatter
055 */
056 public static PeriodFormatter getDefault() {
057 if (cEnglishWords == null) {
058 String[] variants = {" ", ",", ",and ", ", and "};
059 cEnglishWords = new PeriodFormatterBuilder()
060 .appendYears()
061 .appendSuffix(" year", " years")
062 .appendSeparator(", ", " and ", variants)
063 .appendMonths()
064 .appendSuffix(" month", " months")
065 .appendSeparator(", ", " and ", variants)
066 .appendWeeks()
067 .appendSuffix(" week", " weeks")
068 .appendSeparator(", ", " and ", variants)
069 .appendDays()
070 .appendSuffix(" day", " days")
071 .appendSeparator(", ", " and ", variants)
072 .appendHours()
073 .appendSuffix(" hour", " hours")
074 .appendSeparator(", ", " and ", variants)
075 .appendMinutes()
076 .appendSuffix(" minute", " minutes")
077 .appendSeparator(", ", " and ", variants)
078 .appendSeconds()
079 .appendSuffix(" second", " seconds")
080 .appendSeparator(", ", " and ", variants)
081 .appendMillis()
082 .appendSuffix(" millisecond", " milliseconds")
083 .toFormatter();
084 }
085 return cEnglishWords;
086 }
087
088 }