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 for the ISO8601 standard.
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 PeriodFormat} and {@link PeriodFormatterBuilder}.
024     * <p>
025     * ISOPeriodFormat is thread-safe and immutable, and the formatters it
026     * returns are as well.
027     *
028     * @author Brian S O'Neill
029     * @since 1.0
030     * @see PeriodFormat
031     * @see PeriodFormatterBuilder
032     */
033    public class ISOPeriodFormat {
034    
035        /** Cache of standard format. */
036        private static PeriodFormatter cStandard;
037        /** Cache of alternate months format. */
038        private static PeriodFormatter cAlternate;
039        /** Cache of alternate extended months format. */
040        private static PeriodFormatter cAlternateExtended;
041        /** Cache of alternate weeks format. */
042        private static PeriodFormatter cAlternateWithWeeks;
043        /** Cache of alternate extended weeks format. */
044        private static PeriodFormatter cAlternateExtendedWihWeeks;
045    
046        /**
047         * Constructor.
048         *
049         * @since 1.1 (previously private)
050         */
051        protected ISOPeriodFormat() {
052            super();
053        }
054    
055        //-----------------------------------------------------------------------
056        /**
057         * The standard ISO format - PyYmMwWdDThHmMsS.
058         * Milliseconds are not output.
059         * Note that the ISO8601 standard actually indicates weeks should not
060         * be shown if any other field is present and vice versa.
061         * 
062         * @return the formatter
063         */
064        public static PeriodFormatter standard() {
065            if (cStandard == null) {
066                cStandard = new PeriodFormatterBuilder()
067                    .appendLiteral("P")
068                    .appendYears()
069                    .appendSuffix("Y")
070                    .appendMonths()
071                    .appendSuffix("M")
072                    .appendWeeks()
073                    .appendSuffix("W")
074                    .appendDays()
075                    .appendSuffix("D")
076                    .appendSeparatorIfFieldsAfter("T")
077                    .appendHours()
078                    .appendSuffix("H")
079                    .appendMinutes()
080                    .appendSuffix("M")
081                    .appendSecondsWithOptionalMillis()
082                    .appendSuffix("S")
083                    .toFormatter();
084            }
085            return cStandard;
086        }
087    
088        /**
089         * The alternate ISO format, PyyyymmddThhmmss, which excludes weeks.
090         * <p>
091         * Even if weeks are present in the period, they are not output.
092         * Fractional seconds (milliseconds) will appear if required.
093         * 
094         * @return the formatter
095         */
096        public static PeriodFormatter alternate() {
097            if (cAlternate == null) {
098                cAlternate = new PeriodFormatterBuilder()
099                    .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    }