Example usage for org.joda.time.format DateTimeParser parseInto

List of usage examples for org.joda.time.format DateTimeParser parseInto

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeParser parseInto.

Prototype

int parseInto(DateTimeParserBucket bucket, String text, int position);

Source Link

Document

Parse an element from the given text, saving any fields into the given DateTimeParserBucket.

Usage

From source file:com.spotify.heroic.shell.Tasks.java

License:Apache License

private static long parseTodayInstant(String input, final Chronology chrono, long now) {
    final DateTime n = new DateTime(now, chrono);

    for (final DateTimeParser p : today) {
        final DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, null, null, 2000);

        bucket.saveField(chrono.year(), n.getYear());
        bucket.saveField(chrono.monthOfYear(), n.getMonthOfYear());
        bucket.saveField(chrono.dayOfYear(), n.getDayOfYear());

        try {//from   www . ja  v  a 2 s .c om
            p.parseInto(bucket, input, 0);
        } catch (IllegalArgumentException e) {
            // pass-through
            continue;
        }

        return bucket.computeMillis();
    }

    throw new IllegalArgumentException(input + " is not a valid instant");
}

From source file:com.spotify.heroic.shell.Tasks.java

License:Apache License

private static long parseFullInstant(String input, final Chronology chrono) {
    for (final DateTimeParser p : full) {
        final DateTimeParserBucket bucket = new DateTimeParserBucket(0, chrono, null, null, 2000);

        try {/*ww w.  jav  a 2s  . com*/
            p.parseInto(bucket, input, 0);
        } catch (IllegalArgumentException e) {
            // pass-through
            continue;
        }

        return bucket.computeMillis();
    }

    throw new IllegalArgumentException(input + " is not a valid instant");
}

From source file:org.jpmml.evaluator.TypeUtil.java

License:Open Source License

@SuppressWarnings(value = { "deprecation" })
static private Seconds parseSeconds(String value) {
    DateTimeFormatter format = SecondsSinceMidnight.getFormat();

    DateTimeParser parser = format.getParser();

    DateTimeParserBucket bucket = new DateTimeParserBucket(0, null, null);
    bucket.setZone(null);//from   w  w w  .  j  av a2  s  .  c  o m

    int result = parser.parseInto(bucket, value, 0);
    if (result >= 0 && result >= value.length()) {
        long millis = bucket.computeMillis(true);

        return Seconds.seconds((int) (millis / 1000L));
    }

    throw new IllegalArgumentException(value);
}

From source file:org.renjin.primitives.time.Time.java

License:Open Source License

private static DateTime parseIgnoreTrailingCharacters(DateTimeFormatter formatter, String text) {
    // this is a modified version of DateTimeFormatter.parseDateTime() that does not
    // throw an exception on trailing characters

    Chronology chronology = DateTimeUtils.getChronology(null);
    DateTimeParser parser = formatter.getParser();

    Locale locale = null;/*  w w w.  j a  v a2s . co m*/
    Integer pivotYear = null;
    int defaultYear = 2000;
    DateTimeZone timeZone = null;

    DateTimeParserBucket bucket = new DateTimeParserBucket(0, chronology, locale, pivotYear, defaultYear);
    int newPos = parser.parseInto(bucket, text, 0);
    if (newPos >= 0) {
        long millis = bucket.computeMillis(true, text);
        if (formatter.isOffsetParsed() && bucket.getOffsetInteger() != null) {
            int parsedOffset = bucket.getOffsetInteger();
            DateTimeZone parsedZone = DateTimeZone.forOffsetMillis(parsedOffset);
            chronology = chronology.withZone(parsedZone);
        } else if (bucket.getZone() != null) {
            chronology = chronology.withZone(bucket.getZone());
        }
        DateTime dt = new DateTime(millis, chronology);
        if (timeZone != null) {
            dt = dt.withZone(timeZone);
        }
        return dt;
    }
    throw new IllegalArgumentException();
}

From source file:org.whole.lang.xsd.parsers.AbstractISO8601DataTypeParser.java

License:Open Source License

public Object parseObject(EntityDescriptor<?> ed, String value) {
    DateTimeParser parser = getFormatter().getParser();
    DateTimeParserBucket bucket = new DateTimeParserBucket(0, ISOChronology.getInstance(), null);

    // use a custom UTC to check if offset part is parsed
    if (bucket.getZone() == null)
        bucket.setZone(UTC);/*ww w .j  a  v a 2 s.  c  om*/

    if (parser.parseInto(bucket, value, 0) < 0)
        throw new WholeIllegalArgumentException(WholeMessages.no_data_type);

    DateTimeZone zone = bucket.getZone();
    if (zone == null)
        return parseWithTimeZone(bucket);
    else
        return parseWithoutTimeZone(bucket);
}