Example usage for org.joda.time.format DateTimeFormatterBuilder appendMonthOfYear

List of usage examples for org.joda.time.format DateTimeFormatterBuilder appendMonthOfYear

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatterBuilder appendMonthOfYear.

Prototype

public DateTimeFormatterBuilder appendMonthOfYear(int minDigits) 

Source Link

Document

Instructs the printer to emit a numeric monthOfYear field.

Usage

From source file:com.facebook.presto.operator.scalar.UnixTimeFunctions.java

License:Apache License

@SuppressWarnings("fallthrough")
public static DateTimeFormatter createDateTimeFormatter(Slice format) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

    String formatString = format.toString(Charsets.UTF_8);
    boolean escaped = false;
    for (int i = 0; i < format.length(); i++) {
        char character = formatString.charAt(i);

        if (escaped) {
            switch (character) {
            case 'a': // %a Abbreviated weekday name (Sun..Sat)
                builder.appendDayOfWeekShortText();
                break;
            case 'b': // %b Abbreviated month name (Jan..Dec)
                builder.appendMonthOfYearShortText();
                break;
            case 'c': // %c Month, numeric (0..12)
                builder.appendMonthOfYear(1);
                break;
            case 'd': // %d Day of the month, numeric (00..31)
                builder.appendDayOfMonth(2);
                break;
            case 'e': // %e Day of the month, numeric (0..31)
                builder.appendDayOfMonth(1);
                break;
            case 'f': // %f Microseconds (000000..999999)
                builder.appendMillisOfSecond(6);
                break;
            case 'H': // %H Hour (00..23)
                builder.appendHourOfDay(2);
                break;
            case 'h': // %h Hour (01..12)
            case 'I': // %I Hour (01..12)
                builder.appendClockhourOfHalfday(2);
                break;
            case 'i': // %i Minutes, numeric (00..59)
                builder.appendMinuteOfHour(2);
                break;
            case 'j': // %j Day of year (001..366)
                builder.appendDayOfYear(3);
                break;
            case 'k': // %k Hour (0..23)
                builder.appendClockhourOfDay(1);
                break;
            case 'l': // %l Hour (1..12)
                builder.appendClockhourOfHalfday(1);
                break;
            case 'M': // %M Month name (January..December)
                builder.appendMonthOfYearText();
                break;
            case 'm': // %m Month, numeric (00..12)
                builder.appendMonthOfYear(2);
                break;
            case 'p': // %p AM or PM
                builder.appendHalfdayOfDayText();
                break;
            case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or PM)
                builder.appendClockhourOfHalfday(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':')
                        .appendSecondOfMinute(2).appendLiteral(' ').appendHalfdayOfDayText();
                break;
            case 'S': // %S Seconds (00..59)
            case 's': // %s Seconds (00..59)
                builder.appendSecondOfMinute(2);
                break;
            case 'T': // %T Time, 24-hour (hh:mm:ss)
                builder.appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':')
                        .appendSecondOfMinute(2);
                break;
            case 'v': // %v Week (01..53), where Monday is the first day of the week; used with %x
                builder.appendWeekOfWeekyear(2);
                break;
            case 'W': // %W Weekday name (Sunday..Saturday)
                builder.appendDayOfWeekText();
                break;
            case 'w': // %w Day of the week (0=Sunday..6=Saturday)
                builder.appendDayOfWeek(1);
                break;
            case 'Y': // %Y Year, numeric, four digits
                builder.appendYear(4, 4);
                break;
            case 'y': // %y Year, numeric (two digits)
                builder.appendYearOfCentury(2, 2);
                break;
            case 'U': // %U Week (00..53), where Sunday is the first day of the week
            case 'u': // %u Week (00..53), where Monday is the first day of the week
            case 'V': // %V Week (01..53), where Sunday is the first day of the week; used with %X
            case 'X': // %X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
            case 'x': // %x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
            case 'D': // %D Day of the month with English suffix (0th, 1st, 2nd, 3rd, )
                throw new UnsupportedOperationException(
                        String.format("%%%s not supported in date format string", character));
            case '%': // %% A literal %? character
                builder.appendLiteral('%');
                break;
            default: // %<x> The literal character represented by <x>
                builder.appendLiteral(character);
                break;
            }/*from   w  w w. ja  v a 2s.  c o  m*/
            escaped = false;
        } else if (character == '%') {
            escaped = true;
        } else {
            builder.appendLiteral(character);
        }
    }

    return builder.toFormatter();
}

From source file:com.facebook.presto.teradata.functions.dateformat.DateFormatParser.java

License:Apache License

public static DateTimeFormatter createDateTimeFormatter(String format) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    for (Token token : tokenize(format)) {
        switch (token.getType()) {
        case DateFormat.TEXT:
            builder.appendLiteral(token.getText());
            break;
        case DateFormat.DD:
            builder.appendDayOfMonth(2);
            break;
        case DateFormat.HH24:
            builder.appendHourOfDay(2);/*  w  w w .  j  a  va 2 s  . c  om*/
            break;
        case DateFormat.HH:
            builder.appendHourOfHalfday(2);
            break;
        case DateFormat.MI:
            builder.appendMinuteOfHour(2);
            break;
        case DateFormat.MM:
            builder.appendMonthOfYear(2);
            break;
        case DateFormat.SS:
            builder.appendSecondOfMinute(2);
            break;
        case DateFormat.YY:
            builder.appendTwoDigitYear(2050);
            break;
        case DateFormat.YYYY:
            builder.appendYear(4, 4);
            break;
        case DateFormat.UNRECOGNIZED:
        default:
            throw new PrestoException(StandardErrorCode.INVALID_FUNCTION_ARGUMENT,
                    String.format("Failed to tokenize string [%s] at offset [%d]", token.getText(),
                            token.getCharPositionInLine()));
        }
    }

    try {
        return builder.toFormatter();
    } catch (UnsupportedOperationException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}

From source file:com.miraisolutions.xlconnect.utils.RPOSIXDateTimeFormatter.java

License:Open Source License

private org.joda.time.format.DateTimeFormatter getFormatter(String format) {
    if (cache.containsKey(format))
        return cache.get(format);

    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

    for (int i = 0; i < format.length(); ++i) {
        if (format.charAt(i) == '%' && i + 1 < format.length()) {
            char specifier = format.charAt(++i);
            switch (specifier) {
            case '%':
                builder.appendLiteral("%");
                break;
            case 'a':
                // Abbreviated weekday name in the current locale. (Also matches
                // full name on input.)
                builder.appendDayOfWeekShortText();
                break;
            case 'A':
                // Full weekday name in the current locale.  (Also matches
                // abbreviated name on input.)
                builder.appendDayOfWeekText();
                break;
            case 'b':
                // Abbreviated month name in the current locale. (Also matches
                // full name on input.)
                builder.appendMonthOfYearShortText();
                break;
            case 'B':
                // Full month name in the current locale.  (Also matches
                // abbreviated name on input.)
                builder.appendMonthOfYearText();
                break;
            case 'c':
                //  Date and time.  Locale-specific on output, "%a %b %e
                // %H:%M:%S %Y" on input.
                throw new UnsupportedOperationException("%c not yet implemented");
            case 'd':
                // Day of the month as decimal number (01-31).
                builder.appendDayOfMonth(2);
                break;
            case 'H':
                // Hours as decimal number (00-23).
                builder.appendHourOfDay(2);
                break;
            case 'I':
                // Hours as decimal number (01-12).
                builder.appendHourOfHalfday(2);
                break;
            case 'j':
                // Day of year as decimal number (001-366).
                builder.appendDayOfYear(3);
                break;
            case 'm':
                // Month as decimal number (01-12).
                builder.appendMonthOfYear(2);
                break;
            case 'M':
                // Minute as decimal number (00-59).
                builder.appendMinuteOfHour(2);
                break;
            case 'p':
                // AM/PM indicator in the locale.  Used in conjunction with %I
                // and *not* with %H.  An empty string in some locales.
                builder.appendHalfdayOfDayText();
                break;
            case 'O':
                if (i + 1 >= format.length()) {
                    builder.appendLiteral("%O");
                } else {
                    switch (format.charAt(++i)) {
                    case 'S':
                        // Specific to R is %OSn, which for output gives the seconds to 0
                        // <= n <= 6 decimal places (and if %OS is not followed by a
                        // digit, it uses the setting of getOption("digits.secs"), or if
                        // that is unset, n = 3).  Further, for strptime %OS will input
                        // seconds including fractional seconds.  Note that %S ignores (and
                        // not rounds) fractional parts on output.

                        // TODO: not sure how to handle fractional seconds here
                        builder.appendSecondOfMinute(2);
                        break;
                    default:
                        throw new UnsupportedOperationException("%O[dHImMUVwWy] not yet implemented");
                    }//ww w  . j  ava  2 s.c om
                }
                break;
            case 'S':
                // Second as decimal number (00-61), allowing for up to two
                // leap-seconds (but POSIX-compliant implementations will ignore
                // leap seconds).
                // TODO: I have no idea what the docs are talking about in relation
                // to leap seconds
                builder.appendSecondOfMinute(2);
                break;
            // case 'U':
            // Week of the year as decimal number (00-53) using Sunday as
            // the first day 1 of the week (and typically with the first
            //  Sunday of the year as day 1 of week 1).  The US convention.
            // case 'w':
            // Weekday as decimal number (0-6, Sunday is 0).

            // case 'W':
            // Week of the year as decimal number (00-53) using Monday as
            // the first day of week (and typically with the first Monday of
            // the year as day 1 of week 1). The UK convention.

            // %x Date.  Locale-specific on output, "%y/%m/%d" on input.

            //%X Time.  Locale-specific on output, "%H:%M:%S" on input.

            case 'y':
                // Year without century (00-99). Values 00 to 68 are prefixed by
                // 20 and 69 to 99 by 19 - that is the behaviour specified by
                // the 2004 POSIX standard, but it does also say it is expected
                // that in a future version the default century inferred from a
                // 2-digit year will change.
                builder.appendTwoDigitYear(1968, true);
                break;
            case 'Y':
                // Year with century
                builder.appendYear(1, 4);
                break;
            case 'z':
                // Signed offset in hours and minutes from UTC, so -0800 is 8
                // hours behind UTC.
                builder.appendTimeZoneOffset(null /* always show offset, even when zero */,
                        true /* show seperators */, 1 /* min fields (hour, minute, etc) */,
                        2 /* max fields */ );
                break;
            case 'Z':
                // (output only.) Time zone as a character string (empty if not
                // available).
                builder.appendTimeZoneName();
                break;
            default:
                throw new UnsupportedOperationException("%" + specifier + " not yet implemented");
            }
        } else {
            builder.appendLiteral(format.substring(i, i + 1));
        }
    }
    org.joda.time.format.DateTimeFormatter formatter = builder.toFormatter();
    cache.put(format, formatter);
    return formatter;
}

From source file:io.prestosql.operator.scalar.DateTimeFunctions.java

License:Apache License

@SuppressWarnings("fallthrough")
public static DateTimeFormatter createDateTimeFormatter(Slice format) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();

    String formatString = format.toStringUtf8();
    boolean escaped = false;
    for (int i = 0; i < format.length(); i++) {
        char character = formatString.charAt(i);

        if (escaped) {
            switch (character) {
            case 'a': // %a Abbreviated weekday name (Sun..Sat)
                builder.appendDayOfWeekShortText();
                break;
            case 'b': // %b Abbreviated month name (Jan..Dec)
                builder.appendMonthOfYearShortText();
                break;
            case 'c': // %c Month, numeric (0..12)
                builder.appendMonthOfYear(1);
                break;
            case 'd': // %d Day of the month, numeric (00..31)
                builder.appendDayOfMonth(2);
                break;
            case 'e': // %e Day of the month, numeric (0..31)
                builder.appendDayOfMonth(1);
                break;
            case 'f': // %f Microseconds (000000..999999)
                builder.appendFractionOfSecond(6, 9);
                break;
            case 'H': // %H Hour (00..23)
                builder.appendHourOfDay(2);
                break;
            case 'h': // %h Hour (01..12)
            case 'I': // %I Hour (01..12)
                builder.appendClockhourOfHalfday(2);
                break;
            case 'i': // %i Minutes, numeric (00..59)
                builder.appendMinuteOfHour(2);
                break;
            case 'j': // %j Day of year (001..366)
                builder.appendDayOfYear(3);
                break;
            case 'k': // %k Hour (0..23)
                builder.appendHourOfDay(1);
                break;
            case 'l': // %l Hour (1..12)
                builder.appendClockhourOfHalfday(1);
                break;
            case 'M': // %M Month name (January..December)
                builder.appendMonthOfYearText();
                break;
            case 'm': // %m Month, numeric (00..12)
                builder.appendMonthOfYear(2);
                break;
            case 'p': // %p AM or PM
                builder.appendHalfdayOfDayText();
                break;
            case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or PM)
                builder.appendClockhourOfHalfday(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':')
                        .appendSecondOfMinute(2).appendLiteral(' ').appendHalfdayOfDayText();
                break;
            case 'S': // %S Seconds (00..59)
            case 's': // %s Seconds (00..59)
                builder.appendSecondOfMinute(2);
                break;
            case 'T': // %T Time, 24-hour (hh:mm:ss)
                builder.appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':')
                        .appendSecondOfMinute(2);
                break;
            case 'v': // %v Week (01..53), where Monday is the first day of the week; used with %x
                builder.appendWeekOfWeekyear(2);
                break;
            case 'x': // %x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
                builder.appendWeekyear(4, 4);
                break;
            case 'W': // %W Weekday name (Sunday..Saturday)
                builder.appendDayOfWeekText();
                break;
            case 'Y': // %Y Year, numeric, four digits
                builder.appendYear(4, 4);
                break;
            case 'y': // %y Year, numeric (two digits)
                builder.appendTwoDigitYear(PIVOT_YEAR);
                break;
            case 'w': // %w Day of the week (0=Sunday..6=Saturday)
            case 'U': // %U Week (00..53), where Sunday is the first day of the week
            case 'u': // %u Week (00..53), where Monday is the first day of the week
            case 'V': // %V Week (01..53), where Sunday is the first day of the week; used with %X
            case 'X': // %X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
            case 'D': // %D Day of the month with English suffix (0th, 1st, 2nd, 3rd, )
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT,
                        String.format("%%%s not supported in date format string", character));
            case '%': // %% A literal %? character
                builder.appendLiteral('%');
                break;
            default: // %<x> The literal character represented by <x>
                builder.appendLiteral(character);
                break;
            }/*from ww  w . j av  a2  s . co m*/
            escaped = false;
        } else if (character == '%') {
            escaped = true;
        } else {
            builder.appendLiteral(character);
        }
    }

    try {
        return builder.toFormatter();
    } catch (UnsupportedOperationException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}

From source file:org.codelibs.elasticsearch.common.joda.StrictISODateTimeFormat.java

License:Apache License

/**
 * Creates a date using the calendar date format.
 * Specification reference: 5.2.1.//from   w w w  .jav a  2s.  c  om
 *
 * @param bld  the builder
 * @param fields  the fields
 * @param extended  true to use extended format
 * @param strictISO  true to only allow ISO formats
 * @return true if reduced precision
 * @since 1.1
 */
private static boolean dateByMonth(DateTimeFormatterBuilder bld, Collection<DateTimeFieldType> fields,
        boolean extended, boolean strictISO) {

    boolean reducedPrec = false;
    if (fields.remove(DateTimeFieldType.year())) {
        bld.append(Constants.ye);
        if (fields.remove(DateTimeFieldType.monthOfYear())) {
            if (fields.remove(DateTimeFieldType.dayOfMonth())) {
                // YYYY-MM-DD/YYYYMMDD
                appendSeparator(bld, extended);
                bld.appendMonthOfYear(2);
                appendSeparator(bld, extended);
                bld.appendDayOfMonth(2);
            } else {
                // YYYY-MM/YYYY-MM
                bld.appendLiteral('-');
                bld.appendMonthOfYear(2);
                reducedPrec = true;
            }
        } else {
            if (fields.remove(DateTimeFieldType.dayOfMonth())) {
                // YYYY--DD/YYYY--DD (non-iso)
                checkNotStrictISO(fields, strictISO);
                bld.appendLiteral('-');
                bld.appendLiteral('-');
                bld.appendDayOfMonth(2);
            } else {
                // YYYY/YYYY
                reducedPrec = true;
            }
        }

    } else if (fields.remove(DateTimeFieldType.monthOfYear())) {
        bld.appendLiteral('-');
        bld.appendLiteral('-');
        bld.appendMonthOfYear(2);
        if (fields.remove(DateTimeFieldType.dayOfMonth())) {
            // --MM-DD/--MMDD
            appendSeparator(bld, extended);
            bld.appendDayOfMonth(2);
        } else {
            // --MM/--MM
            reducedPrec = true;
        }
    } else if (fields.remove(DateTimeFieldType.dayOfMonth())) {
        // ---DD/---DD
        bld.appendLiteral('-');
        bld.appendLiteral('-');
        bld.appendLiteral('-');
        bld.appendDayOfMonth(2);
    }
    return reducedPrec;
}