Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2009 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.format;
 
 import java.io.Writer;
 import java.util.Map;
 
Factory that creates instances of DateTimeFormatter from patterns and styles.

Datetime formatting is performed by the DateTimeFormatter class. Three classes provide factory methods to create formatters, and this is one. The others are ISODateTimeFormat and DateTimeFormatterBuilder.

This class provides two types of factory:

  • Pattern provides a DateTimeFormatter based on a pattern string that is mostly compatible with the JDK date patterns.
  • Style provides a DateTimeFormatter based on a two character style, representing short, medium, long and full.

For example, to use a patterm:

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = DateTimeFormat.forPattern("MMMM, yyyy");
 String str = fmt.print(dt);
 
The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:
 Symbol  Meaning                      Presentation  Examples
 ------  -------                      ------------  -------
 G       era                          text          AD
 C       century of era (>=0)         number        20
 Y       year of era (>=0)            year          1996

 x       weekyear                     year          1996
 w       week of weekyear             number        27
 e       day of week                  number        2
 E       day of week                  text          Tuesday; Tue

 y       year                         year          1996
 D       day of year                  number        189
 M       month of year                month         July; Jul; 07
 d       day of month                 number        10

 a       halfday of day               text          PM
 K       hour of halfday (0~11)       number        0
 h       clockhour of halfday (1~12)  number        12

 H       hour of day (0~23)           number        0
 k       clockhour of day (1~24)      number        24
 m       minute of hour               number        30
 s       second of minute             number        55
 S       fraction of second           number        978

 z       time zone                    text          Pacific Standard Time; PST
 Z       time zone offset/id          zone          -0800; -08:00; America/Los_Angeles

 '       escape for text              delimiter
 ''      single quote                 literal       '
 
The count of pattern letters determine the format.

Text: If the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available.

Number: The minimum number of digits. Shorter numbers are zero-padded to this amount.

Year: Numeric presentation for year and weekyear fields are handled specially. For example, if the count of 'y' is 2, the year will be displayed as the zero-based year of the century, which is two digits.

Month: 3 or over, use text, otherwise use number.

Zone: 'Z' outputs offset without a colon, 'ZZ' outputs the offset with a colon, 'ZZZ' or more outputs the zone id.

Zone names: Time zone names ('z') cannot be parsed.

Any characters in the pattern that are not in the ranges of ['a'..'z'] and ['A'..'Z'] will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '?' will appear in the resulting time text even they are not embraced within single quotes.

DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

Author(s):
Brian S O'Neill
Maxim Zhao
Since:
1.0
See also:
ISODateTimeFormat
DateTimeFormatterBuilder
public class DateTimeFormat {

    
Style constant for FULL.
    static final int FULL = 0;  // DateFormat.FULL
    
Style constant for LONG.
    static final int LONG = 1;  // DateFormat.LONG
    
Style constant for MEDIUM.
    static final int MEDIUM = 2;  // DateFormat.MEDIUM
    
Style constant for SHORT.
    static final int SHORT = 3;  // DateFormat.SHORT
    
Style constant for NONE.
    static final int NONE = 4;

    
Type constant for DATE only.
    static final int DATE = 0;
    
Type constant for TIME only.
    static final int TIME = 1;
    
Type constant for DATETIME.
    static final int DATETIME = 2;

    
Maps patterns to formatters, patterns don't vary by locale.
    private static final Map<StringDateTimeFormattercPatternedCache = new HashMap<StringDateTimeFormatter>(7);
    
Maps patterns to formatters, patterns don't vary by locale.
    private static final DateTimeFormatter[] cStyleCache = new DateTimeFormatter[25];
    //-----------------------------------------------------------------------
    
Factory to create a formatter from a pattern string. The pattern string is described above in the class level javadoc. It is very similar to SimpleDateFormat patterns.

The format may contain locale specific output, and this will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale. For example:

 DateTimeFormat.forPattern(pattern).withLocale(Locale.FRANCE).print(dt);
 

Parameters:
pattern pattern specification
Returns:
the formatter
Throws:
java.lang.IllegalArgumentException if the pattern is invalid
    public static DateTimeFormatter forPattern(String pattern) {
        return createFormatterForPattern(pattern);
    }

    
Factory to create a format from a two character style pattern.

The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-'.

The returned formatter will dynamically adjust to the locale that the print/parse takes place in. Thus you just call DateTimeFormatter.withLocale(java.util.Locale) and the Short/Medium/Long/Full style for that locale will be output. For example:

 DateTimeFormat.forStyle(style).withLocale(Locale.FRANCE).print(dt);
 

Parameters:
style two characters from the set {"S", "M", "L", "F", "-"}
Returns:
the formatter
Throws:
java.lang.IllegalArgumentException if the style is invalid
    public static DateTimeFormatter forStyle(String style) {
        return createFormatterForStyle(style);
    }

    
Returns the pattern used by a particular style and locale.

The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-'.

Parameters:
style two characters from the set {"S", "M", "L", "F", "-"}
locale locale to use, null means default
Returns:
the formatter
Throws:
java.lang.IllegalArgumentException if the style is invalid
Since:
1.3
    public static String patternForStyle(String styleLocale locale) {
        DateTimeFormatter formatter = createFormatterForStyle(style);
        if (locale == null) {
            locale = Locale.getDefault();
        }
        // Not pretty, but it works.
        return ((StyleFormatterformatter.getPrinter()).getPattern(locale);
    }
    //-----------------------------------------------------------------------
    
Creates a format that outputs a short date format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter shortDate() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a short time format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter shortTime() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a short datetime format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter shortDateTime() {
        return createFormatterForStyleIndex();
    }
    //-----------------------------------------------------------------------
    
Creates a format that outputs a medium date format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter mediumDate() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a medium time format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter mediumTime() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a medium datetime format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter mediumDateTime() {
        return createFormatterForStyleIndex();
    }
    //-----------------------------------------------------------------------
    
Creates a format that outputs a long date format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter longDate() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a long time format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter longTime() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a long datetime format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter longDateTime() {
        return createFormatterForStyleIndex();
    }
    //-----------------------------------------------------------------------
    
Creates a format that outputs a full date format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter fullDate() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a full time format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter fullTime() {
        return createFormatterForStyleIndex();
    }

    
Creates a format that outputs a full datetime format.

The format will change as you change the locale of the formatter. Call DateTimeFormatter.withLocale(java.util.Locale) to switch the locale.

Returns:
the formatter
    public static DateTimeFormatter fullDateTime() {
        return createFormatterForStyleIndex();
    }
    //-----------------------------------------------------------------------
    
Parses the given pattern and appends the rules to the given DateTimeFormatterBuilder.

Parameters:
pattern pattern specification
Throws:
java.lang.IllegalArgumentException if the pattern is invalid
    static void appendPatternTo(DateTimeFormatterBuilder builderString pattern) {
        parsePatternTo(builderpattern);
    }
    //-----------------------------------------------------------------------
    
Constructor.

Since:
1.1 (previously private)
    protected DateTimeFormat() {
        super();
    }
    //-----------------------------------------------------------------------
    
Parses the given pattern and appends the rules to the given DateTimeFormatterBuilder.

Parameters:
pattern pattern specification
Throws:
java.lang.IllegalArgumentException if the pattern is invalid
See also:
forPattern(java.lang.String)
    private static void parsePatternTo(DateTimeFormatterBuilder builderString pattern) {
        int length = pattern.length();
        int[] indexRef = new int[1];
        for (int i=0; i<lengthi++) {
            indexRef[0] = i;
            String token = parseToken(patternindexRef);
            i = indexRef[0];
            int tokenLen = token.length();
            if (tokenLen == 0) {
                break;
            }
            char c = token.charAt(0);
            switch (c) {
            case 'G'// era designator (text)
                builder.appendEraText();
                break;
            case 'C'// century of era (number)
                builder.appendCenturyOfEra(tokenLentokenLen);
                break;
            case 'x'// weekyear (number)
            case 'y'// year (number)
            case 'Y'// year of era (number)
                if (tokenLen == 2) {
                    boolean lenientParse = true;
                    // Peek ahead to next token.
                    if (i + 1 < length) {
                        indexRef[0]++;
                        if (isNumericToken(parseToken(patternindexRef))) {
                            // If next token is a number, cannot support
                            // lenient parse, because it will consume digits
                            // that it should not.
                            lenientParse = false;
                        }
                        indexRef[0]--;
                    }
                    // Use pivots which are compatible with SimpleDateFormat.
                    switch (c) {
                    case 'x':
                        builder.appendTwoDigitWeekyear
                            (new DateTime().getWeekyear() - 30, lenientParse);
                        break;
                    case 'y':
                    case 'Y':
                    default:
                        builder.appendTwoDigitYear(new DateTime().getYear() - 30, lenientParse);
                        break;
                    }
                } else {
                    // Try to support long year values.
                    int maxDigits = 9;
                    // Peek ahead to next token.
                    if (i + 1 < length) {
                        indexRef[0]++;
                        if (isNumericToken(parseToken(patternindexRef))) {
                            // If next token is a number, cannot support long years.
                            maxDigits = tokenLen;
                        }
                        indexRef[0]--;
                    }
                    switch (c) {
                    case 'x':
                        builder.appendWeekyear(tokenLenmaxDigits);
                        break;
                    case 'y':
                        builder.appendYear(tokenLenmaxDigits);
                        break;
                    case 'Y':
                        builder.appendYearOfEra(tokenLenmaxDigits);
                        break;
                    }
                }
                break;
            case 'M'// month of year (text and number)
                if (tokenLen >= 3) {
                    if (tokenLen >= 4) {
                        builder.appendMonthOfYearText();
                    } else {
                        builder.appendMonthOfYearShortText();
                    }
                } else {
                    builder.appendMonthOfYear(tokenLen);
                }
                break;
            case 'd'// day of month (number)
                builder.appendDayOfMonth(tokenLen);
                break;
            case 'a'// am/pm marker (text)
                builder.appendHalfdayOfDayText();
                break;
            case 'h'// clockhour of halfday (number, 1..12)
                builder.appendClockhourOfHalfday(tokenLen);
                break;
            case 'H'// hour of day (number, 0..23)
                builder.appendHourOfDay(tokenLen);
                break;
            case 'k'// clockhour of day (1..24)
                builder.appendClockhourOfDay(tokenLen);
                break;
            case 'K'// hour of halfday (0..11)
                builder.appendHourOfHalfday(tokenLen);
                break;
            case 'm'// minute of hour (number)
                builder.appendMinuteOfHour(tokenLen);
                break;
            case 's'// second of minute (number)
                builder.appendSecondOfMinute(tokenLen);
                break;
            case 'S'// fraction of second (number)
                builder.appendFractionOfSecond(tokenLentokenLen);
                break;
            case 'e'// day of week (number)
                builder.appendDayOfWeek(tokenLen);
                break;
            case 'E'// dayOfWeek (text)
                if (tokenLen >= 4) {
                    builder.appendDayOfWeekText();
                } else {
                    builder.appendDayOfWeekShortText();
                }
                break;
            case 'D'// day of year (number)
                builder.appendDayOfYear(tokenLen);
                break;
            case 'w'// week of weekyear (number)
                builder.appendWeekOfWeekyear(tokenLen);
                break;
            case 'z'// time zone (text)
                if (tokenLen >= 4) {
                    builder.appendTimeZoneName();
                } else {
                    builder.appendTimeZoneShortName();
                }
                break;
            case 'Z'// time zone offset
                if (tokenLen == 1) {
                    builder.appendTimeZoneOffset(null"Z"false, 2, 2);
                } else if (tokenLen == 2) {
                    builder.appendTimeZoneOffset(null"Z"true, 2, 2);
                } else {
                    builder.appendTimeZoneId();
                }
                break;
            case '\''// literal text
                String sub = token.substring(1);
                if (sub.length() == 1) {
                    builder.appendLiteral(sub.charAt(0));
                } else {
                    // Create copy of sub since otherwise the temporary quoted
                    // string would still be referenced internally.
                    builder.appendLiteral(new String(sub));
                }
                break;
            default:
                throw new IllegalArgumentException
                    ("Illegal pattern component: " + token);
            }
        }
    }

    
Parses an individual token.

Parameters:
pattern the pattern string
indexRef a single element array, where the input is the start location and the output is the location after parsing the token
Returns:
the parsed token
    private static String parseToken(String patternint[] indexRef) {
        StringBuffer buf = new StringBuffer();
        int i = indexRef[0];
        int length = pattern.length();
        char c = pattern.charAt(i);
        if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
            // Scan a run of the same character, which indicates a time
            // pattern.
            buf.append(c);
            while (i + 1 < length) {
                char peek = pattern.charAt(i + 1);
                if (peek == c) {
                    buf.append(c);
                    i++;
                } else {
                    break;
                }
            }
        } else {
            // This will identify token as text.
            buf.append('\'');
            boolean inLiteral = false;
            for (; i < lengthi++) {
                c = pattern.charAt(i);
                
                if (c == '\'') {
                    if (i + 1 < length && pattern.charAt(i + 1) == '\'') {
                        // '' is treated as escaped '
                        i++;
                        buf.append(c);
                    } else {
                        inLiteral = !inLiteral;
                    }
                } else if (!inLiteral &&
                           (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
                    i--;
                    break;
                } else {
                    buf.append(c);
                }
            }
        }
        indexRef[0] = i;
        return buf.toString();
    }

    
Returns true if token should be parsed as a numeric field.

Parameters:
token the token to parse
Returns:
true if numeric field
    private static boolean isNumericToken(String token) {
        int tokenLen = token.length();
        if (tokenLen > 0) {
            char c = token.charAt(0);
            switch (c) {
            case 'c'// century (number)
            case 'C'// century of era (number)
            case 'x'// weekyear (number)
            case 'y'// year (number)
            case 'Y'// year of era (number)
            case 'd'// day of month (number)
            case 'h'// hour of day (number, 1..12)
            case 'H'// hour of day (number, 0..23)
            case 'm'// minute of hour (number)
            case 's'// second of minute (number)
            case 'S'// fraction of second (number)
            case 'e'// day of week (number)
            case 'D'// day of year (number)
            case 'F'// day of week in month (number)
            case 'w'// week of year (number)
            case 'W'// week of month (number)
            case 'k'// hour of day (1..24)
            case 'K'// hour of day (0..11)
                return true;
            case 'M'// month of year (text and number)
                if (tokenLen <= 2) {
                    return true;
                }
            }
        }
            
        return false;
    }
    //-----------------------------------------------------------------------
    
Select a format from a custom pattern.

Parameters:
pattern pattern specification
Throws:
java.lang.IllegalArgumentException if the pattern is invalid
See also:
appendPatternTo(org.joda.time.format.DateTimeFormatterBuilder,java.lang.String)
    private static DateTimeFormatter createFormatterForPattern(String pattern) {
        if (pattern == null || pattern.length() == 0) {
            throw new IllegalArgumentException("Invalid pattern specification");
        }
        DateTimeFormatter formatter = null;
        synchronized () {
            formatter = .get(pattern);
            if (formatter == null) {
                DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
                parsePatternTo(builderpattern);
                formatter = builder.toFormatter();
                .put(patternformatter);
            }
        }
        return formatter;
    }

    
Select a format from a two character style pattern. The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-'.

Parameters:
style two characters from the set {"S", "M", "L", "F", "-"}
Throws:
java.lang.IllegalArgumentException if the style is invalid
    private static DateTimeFormatter createFormatterForStyle(String style) {
        if (style == null || style.length() != 2) {
            throw new IllegalArgumentException("Invalid style specification: " + style);
        }
        int dateStyle = selectStyle(style.charAt(0));
        int timeStyle = selectStyle(style.charAt(1));
        if (dateStyle ==  && timeStyle == ) {
            throw new IllegalArgumentException("Style '--' is invalid");
        }
        return createFormatterForStyleIndex(dateStyletimeStyle);
    }

    
Gets the formatter for the specified style.

Parameters:
dateStyle the date style
timeStyle the time style
Returns:
the formatter
    private static DateTimeFormatter createFormatterForStyleIndex(int dateStyleint timeStyle) {
        int index = ((dateStyle << 2) + dateStyle) + timeStyle;
        DateTimeFormatter f = null;
        synchronized () {
            f = [index];
            if (f == null) {
                int type = ;
                if (dateStyle == ) {
                    type = ;
                } else if (timeStyle == ) {
                    type = ;
                }
                StyleFormatter llf = new StyleFormatter(
                        dateStyletimeStyletype);
                f = new DateTimeFormatter(llfllf);
                [index] = f;
            }
        }
        return f;
    }

    
Gets the JDK style code from the Joda code.

Parameters:
ch the Joda style code
Returns:
the JDK style code
    private static int selectStyle(char ch) {
        switch (ch) {
        case 'S':
            return ;
        case 'M':
            return ;
        case 'L':
            return ;
        case 'F':
            return ;
        case '-':
            return ;
        default:
            throw new IllegalArgumentException("Invalid style character: " + ch);
        }
    }
    //-----------------------------------------------------------------------
    static class StyleFormatter
            implements DateTimePrinterDateTimeParser {
        private static final Map<StringDateTimeFormattercCache = new HashMap<StringDateTimeFormatter>();  // manual sync
        
        private final int iDateStyle;
        private final int iTimeStyle;
        private final int iType;
        StyleFormatter(int dateStyleint timeStyleint type) {
            super();
             = dateStyle;
             = timeStyle;
             = type;
        }
        public int estimatePrintedLength() {
            return 40;  // guess
        }
        public void printTo(
                StringBuffer buflong instantChronology chrono,
                int displayOffsetDateTimeZone displayZoneLocale locale) {
            DateTimePrinter p = getFormatter(locale).getPrinter();
            p.printTo(bufinstantchronodisplayOffsetdisplayZonelocale);
        }
        public void printTo(
                Writer outlong instantChronology chrono,
                int displayOffsetDateTimeZone displayZoneLocale localethrows IOException {
            DateTimePrinter p = getFormatter(locale).getPrinter();
            p.printTo(outinstantchronodisplayOffsetdisplayZonelocale);
        }
        public void printTo(StringBuffer bufReadablePartial partialLocale locale) {
            DateTimePrinter p = getFormatter(locale).getPrinter();
            p.printTo(bufpartiallocale);
        }
        public void printTo(Writer outReadablePartial partialLocale localethrows IOException {
            DateTimePrinter p = getFormatter(locale).getPrinter();
            p.printTo(outpartiallocale);
        }
        public int estimateParsedLength() {
            return 40;  // guess
        }
        public int parseInto(DateTimeParserBucket bucketString textint position) {
            DateTimeParser p = getFormatter(bucket.getLocale()).getParser();
            return p.parseInto(buckettextposition);
        }
        private DateTimeFormatter getFormatter(Locale locale) {
            locale = (locale == null ? Locale.getDefault() : locale);
            String key = Integer.toString( + ( << 4) + ( << 8)) + locale.toString();
            DateTimeFormatter f = null;
            synchronized () {
                f = .get(key);
                if (f == null) {
                    String pattern = getPattern(locale);
                    f = DateTimeFormat.forPattern(pattern);
                    .put(keyf);
                }
            }
            return f;
        }
        String getPattern(Locale locale) {
            DateFormat f = null;
            switch () {
                case :
                    f = DateFormat.getDateInstance(locale);
                    break;
                case :
                    f = DateFormat.getTimeInstance(locale);
                    break;
                case :
                    f = DateFormat.getDateTimeInstance(locale);
                    break;
            }
            if (f instanceof SimpleDateFormat == false) {
                throw new IllegalArgumentException("No datetime pattern for locale: " + locale);
            }
            return ((SimpleDateFormatf).toPattern();
        }
    }
New to GrepCode? Check out our FAQ X