Example usage for org.joda.time.format DateTimeFormatter getParser

List of usage examples for org.joda.time.format DateTimeFormatter getParser

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter getParser.

Prototype

public DateTimeParser getParser() 

Source Link

Document

Gets the internal parser object that performs the real parsing work.

Usage

From source file:org.opendatakit.common.android.utilities.DataUtil.java

License:Apache License

public DataUtil(Locale locale, TimeZone tz) {
    this.locale = locale;
    this.tz = DateTimeZone.forTimeZone(tz);
    DateTimeFormatterBuilder fpBuilder = new DateTimeFormatterBuilder();
    for (String pattern : USER_FULL_DATETIME_PATTERNS) {
        DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
        fpBuilder.appendOptional(f.getParser());
    }/*from w  w w  .j  av  a  2  s  .c o m*/
    userFullParser = fpBuilder.toFormatter().withLocale(locale).withZone(this.tz);
    userPartialParsers = new DateTimeFormatter[USER_PARTIAL_DATETIME_PATTERNS.length];
    for (int i = 0; i < USER_PARTIAL_DATETIME_PATTERNS.length; i++) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        for (String pattern : USER_PARTIAL_DATETIME_PATTERNS[i]) {
            DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
            dtfb.appendOptional(f.getParser());
        }
        userPartialParsers[i] = dtfb.toFormatter().withLocale(locale).withZone(this.tz);
    }
    userShortFormatter = DateTimeFormat.forPattern(USER_SHORT_FORMAT);
    userLongFormatter = DateTimeFormat.forPattern(USER_LONG_FORMAT);
}

From source file:org.opendatakit.utilities.DateUtils.java

License:Apache License

public DateUtils(Locale locale, TimeZone tz) {
    this.locale = locale;
    DateTimeZone timeZone = DateTimeZone.forTimeZone(tz);
    DateTimeFormatterBuilder fpBuilder = new DateTimeFormatterBuilder();
    for (String pattern : USER_FULL_DATETIME_PATTERNS) {
        DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
        fpBuilder.appendOptional(f.getParser());
    }// w w  w  .  ja  va2  s .  c o m
    userFullParser = fpBuilder.toFormatter().withLocale(locale).withZone(timeZone);
    userPartialParsers = new DateTimeFormatter[USER_PARTIAL_DATETIME_PATTERNS.length];
    for (int i = 0; i < USER_PARTIAL_DATETIME_PATTERNS.length; i++) {
        DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
        for (String pattern : USER_PARTIAL_DATETIME_PATTERNS[i]) {
            DateTimeFormatter f = DateTimeFormat.forPattern(pattern);
            dtfb.appendOptional(f.getParser());
        }
        userPartialParsers[i] = dtfb.toFormatter().withLocale(locale).withZone(timeZone);
    }
}

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  ww . 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();
}