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().appendLiteral("P")
067: .appendYears().appendSuffix("Y").appendMonths()
068: .appendSuffix("M").appendWeeks().appendSuffix("W")
069: .appendDays().appendSuffix("D")
070: .appendSeparatorIfFieldsAfter("T").appendHours()
071: .appendSuffix("H").appendMinutes()
072: .appendSuffix("M")
073: .appendSecondsWithOptionalMillis()
074: .appendSuffix("S").toFormatter();
075: }
076: return cStandard;
077: }
078:
079: /**
080: * The alternate ISO format, PyyyymmddThhmmss, which excludes weeks.
081: * <p>
082: * Even if weeks are present in the period, they are not output.
083: * Fractional seconds (milliseconds) will appear if required.
084: *
085: * @return the formatter
086: */
087: public static PeriodFormatter alternate() {
088: if (cAlternate == null) {
089: cAlternate = new PeriodFormatterBuilder()
090: .appendLiteral("P").printZeroAlways()
091: .minimumPrintedDigits(4).appendYears()
092: .minimumPrintedDigits(2).appendMonths()
093: .appendDays().appendSeparatorIfFieldsAfter("T")
094: .appendHours().appendMinutes()
095: .appendSecondsWithOptionalMillis().toFormatter();
096: }
097: return cAlternate;
098: }
099:
100: /**
101: * The alternate ISO format, Pyyyy-mm-ddThh:mm:ss, which excludes weeks.
102: * <p>
103: * Even if weeks are present in the period, they are not output.
104: * Fractional seconds (milliseconds) will appear if required.
105: *
106: * @return the formatter
107: */
108: public static PeriodFormatter alternateExtended() {
109: if (cAlternateExtended == null) {
110: cAlternateExtended = new PeriodFormatterBuilder()
111: .appendLiteral("P").printZeroAlways()
112: .minimumPrintedDigits(4).appendYears()
113: .appendSeparator("-").minimumPrintedDigits(2)
114: .appendMonths().appendSeparator("-").appendDays()
115: .appendSeparatorIfFieldsAfter("T").appendHours()
116: .appendSeparator(":").appendMinutes()
117: .appendSeparator(":")
118: .appendSecondsWithOptionalMillis().toFormatter();
119: }
120: return cAlternateExtended;
121: }
122:
123: /**
124: * The alternate ISO format, PyyyyWwwddThhmmss, which excludes months.
125: * <p>
126: * Even if months are present in the period, they are not output.
127: * Fractional seconds (milliseconds) will appear if required.
128: *
129: * @return the formatter
130: */
131: public static PeriodFormatter alternateWithWeeks() {
132: if (cAlternateWithWeeks == null) {
133: cAlternateWithWeeks = new PeriodFormatterBuilder()
134: .appendLiteral("P").printZeroAlways()
135: .minimumPrintedDigits(4).appendYears()
136: .minimumPrintedDigits(2).appendPrefix("W")
137: .appendWeeks().appendDays()
138: .appendSeparatorIfFieldsAfter("T").appendHours()
139: .appendMinutes().appendSecondsWithOptionalMillis()
140: .toFormatter();
141: }
142: return cAlternateWithWeeks;
143: }
144:
145: /**
146: * The alternate ISO format, Pyyyy-Www-ddThh:mm:ss, which excludes months.
147: * <p>
148: * Even if months are present in the period, they are not output.
149: * Fractional seconds (milliseconds) will appear if required.
150: *
151: * @return the formatter
152: */
153: public static PeriodFormatter alternateExtendedWithWeeks() {
154: if (cAlternateExtendedWihWeeks == null) {
155: cAlternateExtendedWihWeeks = new PeriodFormatterBuilder()
156: .appendLiteral("P").printZeroAlways()
157: .minimumPrintedDigits(4).appendYears()
158: .appendSeparator("-").minimumPrintedDigits(2)
159: .appendPrefix("W").appendWeeks().appendSeparator(
160: "-").appendDays()
161: .appendSeparatorIfFieldsAfter("T").appendHours()
162: .appendSeparator(":").appendMinutes()
163: .appendSeparator(":")
164: .appendSecondsWithOptionalMillis().toFormatter();
165: }
166: return cAlternateExtendedWihWeeks;
167: }
168:
169: }
|