1 | /* |
2 | * Copyright 2001-2005 Stephen Colebourne |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
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() |
60 | .appendYears() |
61 | .appendSuffix(" year", " years") |
62 | .appendSeparator(", ", " and ", variants) |
63 | .appendMonths() |
64 | .appendSuffix(" month", " months") |
65 | .appendSeparator(", ", " and ", variants) |
66 | .appendWeeks() |
67 | .appendSuffix(" week", " weeks") |
68 | .appendSeparator(", ", " and ", variants) |
69 | .appendDays() |
70 | .appendSuffix(" day", " days") |
71 | .appendSeparator(", ", " and ", variants) |
72 | .appendHours() |
73 | .appendSuffix(" hour", " hours") |
74 | .appendSeparator(", ", " and ", variants) |
75 | .appendMinutes() |
76 | .appendSuffix(" minute", " minutes") |
77 | .appendSeparator(", ", " and ", variants) |
78 | .appendSeconds() |
79 | .appendSuffix(" second", " seconds") |
80 | .appendSeparator(", ", " and ", variants) |
81 | .appendMillis() |
82 | .appendSuffix(" millisecond", " milliseconds") |
83 | .toFormatter(); |
84 | } |
85 | return cEnglishWords; |
86 | } |
87 | |
88 | } |