01: /*
02: * Copyright 2001-2005 Stephen Colebourne
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.joda.time.format;
17:
18: /**
19: * Factory that creates instances of PeriodFormatter.
20: * <p>
21: * Period formatting is performed by the {@link PeriodFormatter} class.
22: * Three classes provide factory methods to create formatters, and this is one.
23: * The others are {@link ISOPeriodFormat} and {@link PeriodFormatterBuilder}.
24: * <p>
25: * PeriodFormat is thread-safe and immutable, and the formatters it returns
26: * are as well.
27: *
28: * @author Brian S O'Neill
29: * @since 1.0
30: * @see ISOPeriodFormat
31: * @see PeriodFormatterBuilder
32: */
33: public class PeriodFormat {
34:
35: /** An english words based formatter. */
36: private static PeriodFormatter cEnglishWords;
37:
38: /**
39: * Constructor.
40: *
41: * @since 1.1 (previously private)
42: */
43: protected PeriodFormat() {
44: super ();
45: }
46:
47: //-----------------------------------------------------------------------
48: /**
49: * Gets the default PeriodFormatter.
50: * <p>
51: * This currently returns a word based formatter using English only.
52: * Hopefully future release will support localized period formatting.
53: *
54: * @return the formatter
55: */
56: public static PeriodFormatter getDefault() {
57: if (cEnglishWords == null) {
58: String[] variants = { " ", ",", ",and ", ", and " };
59: cEnglishWords = new PeriodFormatterBuilder().appendYears()
60: .appendSuffix(" year", " years").appendSeparator(
61: ", ", " and ", variants).appendMonths()
62: .appendSuffix(" month", " months").appendSeparator(
63: ", ", " and ", variants).appendWeeks()
64: .appendSuffix(" week", " weeks").appendSeparator(
65: ", ", " and ", variants).appendDays()
66: .appendSuffix(" day", " days").appendSeparator(
67: ", ", " and ", variants).appendHours()
68: .appendSuffix(" hour", " hours").appendSeparator(
69: ", ", " and ", variants).appendMinutes()
70: .appendSuffix(" minute", " minutes")
71: .appendSeparator(", ", " and ", variants)
72: .appendSeconds()
73: .appendSuffix(" second", " seconds")
74: .appendSeparator(", ", " and ", variants)
75: .appendMillis().appendSuffix(" millisecond",
76: " milliseconds").toFormatter();
77: }
78: return cEnglishWords;
79: }
80:
81: }
|