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

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

Introduction

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

Prototype

public int parseInto(ReadWritableInstant instant, String text, int position) 

Source Link

Document

Parses a datetime from the given text, at the given position, saving the result into the fields of the given ReadWritableInstant.

Usage

From source file:net.danlew.android.joda.ZoneInfoCompiler.java

License:Apache License

static int parseTime(String str) {
    DateTimeFormatter p = ISODateTimeFormat.hourMinuteSecondFraction();
    MutableDateTime mdt = new MutableDateTime(0, getLenientISOChronology());
    int pos = 0;//ww  w  . j  ava2  s.  c om
    if (str.startsWith("-")) {
        pos = 1;
    }
    int newPos = p.parseInto(mdt, str, pos);
    if (newPos == ~pos) {
        throw new IllegalArgumentException(str);
    }
    int millis = (int) mdt.getMillis();
    if (pos == 1) {
        millis = -millis;
    }
    return millis;
}

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

License:Apache License

private long parseDateTime(String value, DateTimeZone timeZone, boolean roundUpIfNoTime) {
    DateTimeFormatter parser = dateTimeFormatter.parser();
    if (timeZone != null) {
        parser = parser.withZone(timeZone);
    }//from  w w w  .  j a v a 2s . c  o m
    try {
        MutableDateTime date;
        // We use 01/01/1970 as a base date so that things keep working with date
        // fields that are filled with times without dates
        if (roundUpIfNoTime) {
            date = new MutableDateTime(1970, 1, 1, 23, 59, 59, 999, DateTimeZone.UTC);
        } else {
            date = new MutableDateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
        }
        final int end = parser.parseInto(date, value, 0);
        if (end < 0) {
            int position = ~end;
            throw new IllegalArgumentException("Parse failure at index [" + position + "] of [" + value + "]");
        } else if (end != value.length()) {
            throw new IllegalArgumentException(
                    "Unrecognized chars at the end of [" + value + "]: [" + value.substring(end) + "]");
        }
        return date.getMillis();
    } catch (IllegalArgumentException e) {
        throw new ElasticsearchParseException("failed to parse date field [{}] with format [{}]", e, value,
                dateTimeFormatter.format());
    }
}

From source file:org.wicketopia.joda.util.format.JodaFormatSupport.java

License:Apache License

public T convertToObject(String value, Locale locale) {
    if (Strings.isEmpty(value)) {
        return null;
    }//from w  w  w . j  av  a  2  s  . c o  m

    DateTimeFormatter format = formatProvider.getFormatter();

    if (format == null) {
        throw new IllegalStateException("format must be not null");
    }
    format = format.withLocale(locale).withPivotYear(pivotYear);
    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        // instantiate now/ current time
        MutableDateTime dt = new MutableDateTime();
        if (zone != null) {
            // set time zone for client
            format = format.withZone(DateTimeZone.forTimeZone(zone));
            dt.setZone(DateTimeZone.forTimeZone(zone));
        }
        try {
            // parse date retaining the time of the submission
            int result = format.parseInto(dt, value, 0);
            if (result < 0) {
                throw new ConversionException(new ParseException("unable to parse date " + value, ~result));
            }
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
        // apply the server time zone to the parsed value
        dt.setZone(getServerTimeZone());
        return translator.fromDateTime(dt.toDateTime());
    } else {
        try {
            DateTime date = format.parseDateTime(value);
            return date == null ? null : translator.fromDateTime(date);
        } catch (RuntimeException e) {
            throw new ConversionException(e);
        }
    }
}

From source file:org.wicketstuff.calendar.util.DateConverter.java

License:Apache License

/**
 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,
 *      java.util.Locale)/*from w ww.ja  v  a 2 s .com*/
 */
public Object convertToObject(String value, Locale locale) {
    DateTimeFormatter format = getFormat();

    if (applyTimeZoneDifference) {
        TimeZone zone = getClientTimeZone();
        // instantiate now/ current time
        MutableDateTime dt = new MutableDateTime();
        if (zone != null) {
            // set time zone for client
            format = format.withZone(DateTimeZone.forTimeZone(zone));
            dt.setZone(DateTimeZone.forTimeZone(zone));
        }
        // parse date retaining the time of the submission
        format.parseInto(dt, value, 0);
        // apply the server time zone to the parsed value
        dt.setZone(getTimeZone());
        return dt.toDate();
    } else {
        return format.parseDateTime(value).toDate();
    }
}