Search in sources :

Example 6 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testWriteAndParse.

public void testWriteAndParse() {
    DateTimeFormatter dateTimeWriter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
    DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    Date date = new Date();
    assertThat(formatter.parseMillis(dateTimeWriter.print(date.getTime())), equalTo(date.getTime()));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) Date(java.util.Date)

Example 7 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testUpperBound.

public void testUpperBound() {
    MutableDateTime dateTime = new MutableDateTime(3000, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC);
    DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    String value = "2000-01-01";
    int i = formatter.parseInto(dateTime, value, 0);
    assertThat(i, equalTo(value.length()));
    assertThat(dateTime.toString(), equalTo("2000-01-01T23:59:59.999Z"));
}
Also used : MutableDateTime(org.joda.time.MutableDateTime) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 8 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class DateMathParser method parseDateTime.

private long parseDateTime(String value, DateTimeZone timeZone, boolean roundUpIfNoTime) {
    DateTimeFormatter parser = dateTimeFormatter.parser();
    if (timeZone != null) {
        parser = parser.withZone(timeZone);
    }
    try {
        MutableDateTime 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());
    }
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MutableDateTime(org.joda.time.MutableDateTime) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 9 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class Joda method getStrictStandardDateFormatter.

public static FormatDateTimeFormatter getStrictStandardDateFormatter() {
    // 2014/10/10
    DateTimeFormatter shortFormatter = new DateTimeFormatterBuilder().appendFixedDecimal(DateTimeFieldType.year(), 4).appendLiteral('/').appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2).appendLiteral('/').appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2).toFormatter().withZoneUTC();
    // 2014/10/10 12:12:12
    DateTimeFormatter longFormatter = new DateTimeFormatterBuilder().appendFixedDecimal(DateTimeFieldType.year(), 4).appendLiteral('/').appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2).appendLiteral('/').appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2).appendLiteral(' ').appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2).appendLiteral(':').appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2).appendLiteral(':').appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2).toFormatter().withZoneUTC();
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[] { longFormatter.getParser(), shortFormatter.getParser(), new EpochTimeParser(true) });
    return new FormatDateTimeFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis", builder.toFormatter().withZone(DateTimeZone.UTC), Locale.ROOT);
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeFormatterBuilder(org.joda.time.format.DateTimeFormatterBuilder)

Example 10 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project newts by OpenNMS.

the class TimestampOptionHandler method parse.

@Override
protected Timestamp parse(String argument) throws NumberFormatException, CmdLineException {
    DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
    DateTime dateTime = parser.parseDateTime(argument);
    return Timestamp.fromEpochMillis(dateTime.getMillis());
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Aggregations

DateTimeFormatter (org.joda.time.format.DateTimeFormatter)189 DateTime (org.joda.time.DateTime)88 Date (java.util.Date)40 Test (org.junit.Test)25 DateTimeZone (org.joda.time.DateTimeZone)19 ArrayList (java.util.ArrayList)14 SolrInputDocument (org.apache.solr.common.SolrInputDocument)12 IndexSchema (org.apache.solr.schema.IndexSchema)12 DateTimeFormatterBuilder (org.joda.time.format.DateTimeFormatterBuilder)12 HashMap (java.util.HashMap)10 CswSourceConfiguration (org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration)10 IOException (java.io.IOException)9 Calendar (java.util.Calendar)8 Map (java.util.Map)8 DatasetConfigDTO (com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO)7 FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)7 Test (org.testng.annotations.Test)7 TimeSpec (com.linkedin.thirdeye.api.TimeSpec)6 FilterType (net.opengis.filter.v_1_1_0.FilterType)6 Deployment (org.activiti.engine.test.Deployment)6