org.joda.time.format.PeriodParser Java Examples
The following examples show how to use
org.joda.time.format.PeriodParser.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DurationUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Parses a duration from a period formatted string. Values are accepted in the following formats: * * <p>Formats Ns - Seconds. Example: 5s<br> * Nm - Minutes. Example: 13m<br> * Nh - Hours. Example: 2h * * <pre> * parseDuration(null) = NullPointerException() * parseDuration("") = Duration.standardSeconds(0) * parseDuration("2s") = Duration.standardSeconds(2) * parseDuration("5m") = Duration.standardMinutes(5) * parseDuration("3h") = Duration.standardHours(3) * </pre> * * @param value The period value to parse. * @return The {@link Duration} parsed from the supplied period string. */ public static Duration parseDuration(String value) { checkNotNull(value, "The specified duration must be a non-null value!"); PeriodParser parser = new PeriodFormatterBuilder() .appendSeconds() .appendSuffix("s") .appendMinutes() .appendSuffix("m") .appendHours() .appendSuffix("h") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, value, 0, Locale.getDefault()); Duration duration = period.toDurationFrom(new DateTime(0)); checkArgument(duration.getMillis() > 0, "The window duration must be greater than 0!"); return duration; }
Example #2
Source File: DurationUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Parses a duration from a period formatted string. Values are accepted in the following formats: * * <p>Formats Ns - Seconds. Example: 5s<br> * Nm - Minutes. Example: 13m<br> * Nh - Hours. Example: 2h * * <pre> * parseDuration(null) = NullPointerException() * parseDuration("") = Duration.standardSeconds(0) * parseDuration("2s") = Duration.standardSeconds(2) * parseDuration("5m") = Duration.standardMinutes(5) * parseDuration("3h") = Duration.standardHours(3) * </pre> * * @param value The period value to parse. * @return The {@link Duration} parsed from the supplied period string. */ public static Duration parseDuration(String value) { checkNotNull(value, "The specified duration must be a non-null value!"); PeriodParser parser = new PeriodFormatterBuilder() .appendSeconds() .appendSuffix("s") .appendMinutes() .appendSuffix("m") .appendHours() .appendSuffix("h") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, value, 0, Locale.getDefault()); Duration duration = period.toDurationFrom(new DateTime(0)); checkArgument(duration.getMillis() > 0, "The window duration must be greater than 0!"); return duration; }
Example #3
Source File: DateTimeUtils.java From presto with Apache License 2.0 | 5 votes |
@Override public int parseInto(ReadWritablePeriod period, String text, int position, Locale locale) { int bestValidPos = position; ReadWritablePeriod bestValidPeriod = null; int bestInvalidPos = position; for (PeriodParser parser : parsers) { ReadWritablePeriod parsedPeriod = new MutablePeriod(); int parsePos = parser.parseInto(parsedPeriod, text, position, locale); if (parsePos >= position) { if (parsePos > bestValidPos) { bestValidPos = parsePos; bestValidPeriod = parsedPeriod; if (parsePos >= text.length()) { break; } } } else if (parsePos < 0) { parsePos = ~parsePos; if (parsePos > bestInvalidPos) { bestInvalidPos = parsePos; } } } if (bestValidPos > position || (bestValidPos == position)) { // Restore the state to the best valid parse. if (bestValidPeriod != null) { period.setPeriod(bestValidPeriod); } return bestValidPos; } return ~bestInvalidPos; }
Example #4
Source File: DateTimeUtils.java From presto with Apache License 2.0 | 4 votes |
private static PeriodFormatter cretePeriodFormatter(IntervalField startField, IntervalField endField) { if (endField == null) { endField = startField; } List<PeriodParser> parsers = new ArrayList<>(); PeriodFormatterBuilder builder = new PeriodFormatterBuilder(); switch (startField) { case YEAR: builder.appendYears(); parsers.add(builder.toParser()); if (endField == IntervalField.YEAR) { break; } builder.appendLiteral("-"); // fall through 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(" "); // fall through case HOUR: builder.appendHours(); parsers.add(builder.toParser()); if (endField == IntervalField.HOUR) { break; } builder.appendLiteral(":"); // fall through case MINUTE: builder.appendMinutes(); parsers.add(builder.toParser()); if (endField == IntervalField.MINUTE) { break; } builder.appendLiteral(":"); // fall through case SECOND: builder.appendSecondsWithOptionalMillis(); parsers.add(builder.toParser()); break; } return new PeriodFormatter(builder.toPrinter(), new OrderedPeriodParser(parsers)); }
Example #5
Source File: DateTimeUtils.java From presto with Apache License 2.0 | 4 votes |
private OrderedPeriodParser(List<PeriodParser> parsers) { this.parsers = parsers; }