1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.format;
17
18 import java.util.Locale;
19 import java.util.ResourceBundle;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class PeriodFormat {
39
40
41
42
43 private static final String BUNDLE_NAME = "org.joda.time.format.messages";
44
45
46
47 private static final ConcurrentMap<Locale, PeriodFormatter> FORMATTERS = new ConcurrentHashMap<Locale, PeriodFormatter>();
48
49
50
51
52
53
54 protected PeriodFormat() {
55 super();
56 }
57
58
59
60
61
62
63
64
65
66 public static PeriodFormatter getDefault() {
67 return wordBased(Locale.ENGLISH);
68 }
69
70
71
72
73
74
75
76
77
78 public static PeriodFormatter wordBased() {
79 return wordBased(Locale.getDefault());
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public static PeriodFormatter wordBased(Locale locale) {
95 PeriodFormatter pf = FORMATTERS.get(locale);
96 if (pf == null) {
97 ResourceBundle b = ResourceBundle.getBundle(BUNDLE_NAME, locale);
98 String[] variants = {
99 b.getString("PeriodFormat.space"), b.getString("PeriodFormat.comma"),
100 b.getString("PeriodFormat.commandand"), b.getString("PeriodFormat.commaspaceand")};
101 pf = new PeriodFormatterBuilder()
102 .appendYears()
103 .appendSuffix(b.getString("PeriodFormat.year"), b.getString("PeriodFormat.years"))
104 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
105 .appendMonths()
106 .appendSuffix(b.getString("PeriodFormat.month"), b.getString("PeriodFormat.months"))
107 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
108 .appendWeeks()
109 .appendSuffix(b.getString("PeriodFormat.week"), b.getString("PeriodFormat.weeks"))
110 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
111 .appendDays()
112 .appendSuffix(b.getString("PeriodFormat.day"), b.getString("PeriodFormat.days"))
113 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
114 .appendHours()
115 .appendSuffix(b.getString("PeriodFormat.hour"), b.getString("PeriodFormat.hours"))
116 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
117 .appendMinutes()
118 .appendSuffix(b.getString("PeriodFormat.minute"), b.getString("PeriodFormat.minutes"))
119 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
120 .appendSeconds()
121 .appendSuffix(b.getString("PeriodFormat.second"), b.getString("PeriodFormat.seconds"))
122 .appendSeparator(b.getString("PeriodFormat.commaspace"), b.getString("PeriodFormat.spaceandspace"), variants)
123 .appendMillis()
124 .appendSuffix(b.getString("PeriodFormat.millisecond"), b.getString("PeriodFormat.milliseconds"))
125 .toFormatter();
126 FORMATTERS.putIfAbsent(locale, pf);
127 }
128 return pf;
129 }
130
131 }