Search in sources :

Example 1 with PeriodParser

use of org.joda.time.format.PeriodParser in project presto by prestodb.

the class DateTimeUtils method cretePeriodFormatter.

private static PeriodFormatter cretePeriodFormatter(IntervalField startField, IntervalField endField) {
    if (endField == null) {
        endField = startField;
    }
    List<PeriodParser> parsers = new ArrayList<>();
    PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
    //CHECKSTYLE.OFF
    switch(startField) {
        case YEAR:
            builder.appendYears();
            parsers.add(builder.toParser());
            if (endField == IntervalField.YEAR) {
                break;
            }
            builder.appendLiteral("-");
        case MONTH:
            builder.appendMonths();
            parsers.add(builder.toParser());
            if (endField != IntervalField.MONTH) {
                throw new IllegalArgumentException("Invalid interval qualifier: " + startField + " to " + endField);
            }
            break;
        case DAY:
            builder.appendDays();
            parsers.add(builder.toParser());
            if (endField == IntervalField.DAY) {
                break;
            }
            builder.appendLiteral(" ");
        case HOUR:
            builder.appendHours();
            parsers.add(builder.toParser());
            if (endField == IntervalField.HOUR) {
                break;
            }
            builder.appendLiteral(":");
        case MINUTE:
            builder.appendMinutes();
            parsers.add(builder.toParser());
            if (endField == IntervalField.MINUTE) {
                break;
            }
            builder.appendLiteral(":");
        case SECOND:
            builder.appendSecondsWithOptionalMillis();
            parsers.add(builder.toParser());
    }
    return new PeriodFormatter(builder.toPrinter(), new OrderedPeriodParser(parsers));
}
Also used : PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) PeriodFormatter(org.joda.time.format.PeriodFormatter) PeriodParser(org.joda.time.format.PeriodParser) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 PeriodFormatter (org.joda.time.format.PeriodFormatter)1 PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)1 PeriodParser (org.joda.time.format.PeriodParser)1