KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > format > ISOPeriodFormat


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 for the ISO8601 standard.
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 PeriodFormat} and {@link PeriodFormatterBuilder}.
24  * <p>
25  * ISOPeriodFormat is thread-safe and immutable, and the formatters it
26  * returns are as well.
27  *
28  * @author Brian S O'Neill
29  * @since 1.0
30  * @see PeriodFormat
31  * @see PeriodFormatterBuilder
32  */

33 public class ISOPeriodFormat {
34
35     /** Cache of standard format. */
36     private static PeriodFormatter cStandard;
37     /** Cache of alternate months format. */
38     private static PeriodFormatter cAlternate;
39     /** Cache of alternate extended months format. */
40     private static PeriodFormatter cAlternateExtended;
41     /** Cache of alternate weeks format. */
42     private static PeriodFormatter cAlternateWithWeeks;
43     /** Cache of alternate extended weeks format. */
44     private static PeriodFormatter cAlternateExtendedWihWeeks;
45
46     /**
47      * Constructor.
48      *
49      * @since 1.1 (previously private)
50      */

51     protected ISOPeriodFormat() {
52         super();
53     }
54
55     //-----------------------------------------------------------------------
56
/**
57      * The standard ISO format - PyYmMwWdDThHmMsS.
58      * Milliseconds are not output.
59      * Note that the ISO8601 standard actually indicates weeks should not
60      * be shown if any other field is present and vice versa.
61      *
62      * @return the formatter
63      */

64     public static PeriodFormatter standard() {
65         if (cStandard == null) {
66             cStandard = new PeriodFormatterBuilder()
67                 .appendLiteral("P")
68                 .appendYears()
69                 .appendSuffix("Y")
70                 .appendMonths()
71                 .appendSuffix("M")
72                 .appendWeeks()
73                 .appendSuffix("W")
74                 .appendDays()
75                 .appendSuffix("D")
76                 .appendSeparatorIfFieldsAfter("T")
77                 .appendHours()
78                 .appendSuffix("H")
79                 .appendMinutes()
80                 .appendSuffix("M")
81                 .appendSecondsWithOptionalMillis()
82                 .appendSuffix("S")
83                 .toFormatter();
84         }
85         return cStandard;
86     }
87
88     /**
89      * The alternate ISO format, PyyyymmddThhmmss, which excludes weeks.
90      * <p>
91      * Even if weeks are present in the period, they are not output.
92      * Fractional seconds (milliseconds) will appear if required.
93      *
94      * @return the formatter
95      */

96     public static PeriodFormatter alternate() {
97         if (cAlternate == null) {
98             cAlternate = new PeriodFormatterBuilder()
99                 .appendLiteral("P")
100                 .printZeroAlways()
101                 .minimumPrintedDigits(4)
102                 .appendYears()
103                 .minimumPrintedDigits(2)
104                 .appendMonths()
105                 .appendDays()
106                 .appendSeparatorIfFieldsAfter("T")
107                 .appendHours()
108                 .appendMinutes()
109                 .appendSecondsWithOptionalMillis()
110                 .toFormatter();
111         }
112         return cAlternate;
113     }
114
115     /**
116      * The alternate ISO format, Pyyyy-mm-ddThh:mm:ss, which excludes weeks.
117      * <p>
118      * Even if weeks are present in the period, they are not output.
119      * Fractional seconds (milliseconds) will appear if required.
120      *
121      * @return the formatter
122      */

123     public static PeriodFormatter alternateExtended() {
124         if (cAlternateExtended == null) {
125             cAlternateExtended = new PeriodFormatterBuilder()
126                 .appendLiteral("P")
127                 .printZeroAlways()
128                 .minimumPrintedDigits(4)
129                 .appendYears()
130                 .appendSeparator("-")
131                 .minimumPrintedDigits(2)
132                 .appendMonths()
133                 .appendSeparator("-")
134                 .appendDays()
135                 .appendSeparatorIfFieldsAfter("T")
136                 .appendHours()
137                 .appendSeparator(":")
138                 .appendMinutes()
139                 .appendSeparator(":")
140                 .appendSecondsWithOptionalMillis()
141                 .toFormatter();
142         }
143         return cAlternateExtended;
144     }
145
146     /**
147      * The alternate ISO format, PyyyyWwwddThhmmss, which excludes months.
148      * <p>
149      * Even if months are present in the period, they are not output.
150      * Fractional seconds (milliseconds) will appear if required.
151      *
152      * @return the formatter
153      */

154     public static PeriodFormatter alternateWithWeeks() {
155         if (cAlternateWithWeeks == null) {
156             cAlternateWithWeeks = new PeriodFormatterBuilder()
157                 .appendLiteral("P")
158                 .printZeroAlways()
159                 .minimumPrintedDigits(4)
160                 .appendYears()
161                 .minimumPrintedDigits(2)
162                 .appendPrefix("W")
163                 .appendWeeks()
164                 .appendDays()
165                 .appendSeparatorIfFieldsAfter("T")
166                 .appendHours()
167                 .appendMinutes()
168                 .appendSecondsWithOptionalMillis()
169                 .toFormatter();
170         }
171         return cAlternateWithWeeks;
172     }
173
174     /**
175      * The alternate ISO format, Pyyyy-Www-ddThh:mm:ss, which excludes months.
176      * <p>
177      * Even if months are present in the period, they are not output.
178      * Fractional seconds (milliseconds) will appear if required.
179      *
180      * @return the formatter
181      */

182     public static PeriodFormatter alternateExtendedWithWeeks() {
183         if (cAlternateExtendedWihWeeks == null) {
184             cAlternateExtendedWihWeeks = new PeriodFormatterBuilder()
185                 .appendLiteral("P")
186                 .printZeroAlways()
187                 .minimumPrintedDigits(4)
188                 .appendYears()
189                 .appendSeparator("-")
190                 .minimumPrintedDigits(2)
191                 .appendPrefix("W")
192                 .appendWeeks()
193                 .appendSeparator("-")
194                 .appendDays()
195                 .appendSeparatorIfFieldsAfter("T")
196                 .appendHours()
197                 .appendSeparator(":")
198                 .appendMinutes()
199                 .appendSeparator(":")
200                 .appendSecondsWithOptionalMillis()
201                 .toFormatter();
202         }
203         return cAlternateExtendedWihWeeks;
204     }
205
206 }
207
Popular Tags