Package org.elasticsearch.common.joda.time.format

Examples of org.elasticsearch.common.joda.time.format.DateTimeFormatter


        parsers[0] = DateTimeFormat.forPattern("MM/dd/yyyy").withZone(DateTimeZone.UTC).getParser();
        parsers[1] = DateTimeFormat.forPattern("MM-dd-yyyy").withZone(DateTimeZone.UTC).getParser();
        parsers[2] = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(DateTimeZone.UTC).getParser();
        builder.append(DateTimeFormat.forPattern("MM/dd/yyyy").withZone(DateTimeZone.UTC).getPrinter(), parsers);

        DateTimeFormatter formatter = builder.toFormatter();

        formatter.parseMillis("2009-11-15 14:12:12");
    }
View Full Code Here


        formatter.parseMillis("2009-11-15 14:12:12");
    }

    @Test public void testIsoDateFormatDateTimeNoMillisUTC() {
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC);
        long millis = formatter.parseMillis("1970-01-01T00:00:00Z");

        assertThat(millis, equalTo(0l));
    }
View Full Code Here

        assertThat(millis, equalTo(0l));
    }

    @Test public void testIsoDateFormatDateOptionalTimeUTC() {
        DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
        long millis = formatter.parseMillis("1970-01-01T00:00:00Z");
        assertThat(millis, equalTo(0l));
        millis = formatter.parseMillis("1970-01-01T00:00:00.001Z");
        assertThat(millis, equalTo(1l));
        millis = formatter.parseMillis("1970-01-01T00:00:00.1Z");
        assertThat(millis, equalTo(100l));
        millis = formatter.parseMillis("1970-01-01T00:00:00.1");
        assertThat(millis, equalTo(100l));
        millis = formatter.parseMillis("1970-01-01T00:00:00");
        assertThat(millis, equalTo(0l));
        millis = formatter.parseMillis("1970-01-01");
        assertThat(millis, equalTo(0l));

        millis = formatter.parseMillis("1970");
        assertThat(millis, equalTo(0l));

        try {
            formatter.parseMillis("1970 kuku");
            assert false : "formatting should fail";
        } catch (IllegalArgumentException e) {
            // all is well
        }

        // test offset in format
        millis = formatter.parseMillis("1970-01-01T00:00:00-02:00");
        assertThat(millis, equalTo(TimeValue.timeValueHours(2).millis()));
    }
View Full Code Here

        millis = formatter.parseMillis("1970-01-01T00:00:00-02:00");
        assertThat(millis, equalTo(TimeValue.timeValueHours(2).millis()));
    }

    @Test public void testIsoVsCustom() {
        DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
        long millis = formatter.parseMillis("1970-01-01T00:00:00");
        assertThat(millis, equalTo(0l));

        formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").withZone(DateTimeZone.UTC);
        millis = formatter.parseMillis("1970/01/01 00:00:00");
        assertThat(millis, equalTo(0l));

        FormatDateTimeFormatter formatter2 = Joda.forPattern("yyyy/MM/dd HH:mm:ss");
        millis = formatter2.parser().parseMillis("1970/01/01 00:00:00");
        assertThat(millis, equalTo(0l));
View Full Code Here

        millis = formatter2.parser().parseMillis("1970/01/01 00:00:00");
        assertThat(millis, equalTo(0l));
    }

    @Test 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()));
    }
View Full Code Here

    /**
     * Parses a joda based pattern, including some named ones (similar to the built in Joda ISO ones).
     */
    public static FormatDateTimeFormatter forPattern(String input) {
        DateTimeFormatter formatter;
        if ("basicDate".equals(input) || "basic_date".equals(input)) {
            formatter = ISODateTimeFormat.basicDate();
        } else if ("basicDateTime".equals(input) || "basic_date_time".equals(input)) {
            formatter = ISODateTimeFormat.basicDateTime();
        } else if ("basicDateTimeNoMillis".equals(input) || "basic_date_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.basicDateTimeNoMillis();
        } else if ("basicOrdinalDate".equals(input) || "basic_ordinal_date".equals(input)) {
            formatter = ISODateTimeFormat.basicOrdinalDate();
        } else if ("basicOrdinalDateTime".equals(input) || "basic_ordinal_date_time".equals(input)) {
            formatter = ISODateTimeFormat.basicOrdinalDateTime();
        } else if ("basicOrdinalDateTimeNoMillis".equals(input) || "basic_ordinal_date_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.basicOrdinalDateTimeNoMillis();
        } else if ("basicTime".equals(input) || "basic_time".equals(input)) {
            formatter = ISODateTimeFormat.basicTime();
        } else if ("basicTimeNoMillis".equals(input) || "basic_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.basicTimeNoMillis();
        } else if ("basicTTime".equals(input) || "basic_t_Time".equals(input)) {
            formatter = ISODateTimeFormat.basicTTime();
        } else if ("basicTTimeNoMillis".equals(input) || "basic_t_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.basicTTimeNoMillis();
        } else if ("basicWeekDate".equals(input) || "basic_week_date".equals(input)) {
            formatter = ISODateTimeFormat.basicWeekDate();
        } else if ("basicWeekDateTime".equals(input) || "basic_week_date_time".equals(input)) {
            formatter = ISODateTimeFormat.basicWeekDateTime();
        } else if ("basicWeekDateTimeNoMillis".equals(input) || "basic_week_date_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.basicWeekDateTimeNoMillis();
        } else if ("date".equals(input)) {
            formatter = ISODateTimeFormat.date();
        } else if ("dateHour".equals(input) || "date_hour".equals(input)) {
            formatter = ISODateTimeFormat.dateHour();
        } else if ("dateHourMinute".equals(input) || "date_hour_minute".equals(input)) {
            formatter = ISODateTimeFormat.dateHourMinute();
        } else if ("dateHourMinuteSecond".equals(input) || "date_hour_minute_second".equals(input)) {
            formatter = ISODateTimeFormat.dateHourMinuteSecond();
        } else if ("dateHourMinuteSecondFraction".equals(input) || "date_hour_minute_second_fraction".equals(input)) {
            formatter = ISODateTimeFormat.dateHourMinuteSecondFraction();
        } else if ("dateHourMinuteSecondMillis".equals(input) || "date_hour_minute_second_millis".equals(input)) {
            formatter = ISODateTimeFormat.dateHourMinuteSecondMillis();
        } else if ("dateOptionalTime".equals(input) || "date_optional_time".equals(input)) {
            // in this case, we have a separate parser and printer since the dataOptionalTimeParser can't print
            return new FormatDateTimeFormatter(input,
                    ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC),
                    ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC));
        } else if ("dateTime".equals(input) || "date_time".equals(input)) {
            formatter = ISODateTimeFormat.dateTime();
        } else if ("dateTimeNoMillis".equals(input) || "date_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.dateTimeNoMillis();
        } else if ("hour".equals(input)) {
            formatter = ISODateTimeFormat.hour();
        } else if ("hourMinute".equals(input) || "hour_minute".equals(input)) {
            formatter = ISODateTimeFormat.hourMinute();
        } else if ("hourMinuteSecond".equals(input) || "hour_minute_second".equals(input)) {
            formatter = ISODateTimeFormat.hourMinuteSecond();
        } else if ("hourMinuteSecondFraction".equals(input) || "hour_minute_second_fraction".equals(input)) {
            formatter = ISODateTimeFormat.hourMinuteSecondFraction();
        } else if ("hourMinuteSecondMillis".equals(input) || "hour_minute_second_millis".equals(input)) {
            formatter = ISODateTimeFormat.hourMinuteSecondMillis();
        } else if ("ordinalDate".equals(input) || "ordinal_date".equals(input)) {
            formatter = ISODateTimeFormat.ordinalDate();
        } else if ("ordinalDateTime".equals(input) || "ordinal_date_time".equals(input)) {
            formatter = ISODateTimeFormat.ordinalDateTime();
        } else if ("ordinalDateTimeNoMillis".equals(input) || "ordinal_date_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.ordinalDateTimeNoMillis();
        } else if ("time".equals(input)) {
            formatter = ISODateTimeFormat.time();
        } else if ("tTime".equals(input) || "t_time".equals(input)) {
            formatter = ISODateTimeFormat.tTime();
        } else if ("tTimeNoMillis".equals(input) || "t_time_no_millis".equals(input)) {
            formatter = ISODateTimeFormat.tTimeNoMillis();
        } else if ("weekDate".equals(input) || "week_date".equals(input)) {
            formatter = ISODateTimeFormat.weekDate();
        } else if ("weekDateTime".equals(input) || "week_date_time".equals(input)) {
            formatter = ISODateTimeFormat.weekDateTime();
        } else if ("weekyear".equals(input) || "week_year".equals(input)) {
            formatter = ISODateTimeFormat.weekyear();
        } else if ("weekyearWeek".equals(input)) {
            formatter = ISODateTimeFormat.weekyearWeek();
        } else if ("year".equals(input)) {
            formatter = ISODateTimeFormat.year();
        } else if ("yearMonth".equals(input) || "year_month".equals(input)) {
            formatter = ISODateTimeFormat.yearMonth();
        } else if ("yearMonthDay".equals(input) || "year_month_day".equals(input)) {
            formatter = ISODateTimeFormat.yearMonthDay();
        } else {
            String[] formats = Strings.delimitedListToStringArray(input, "||");
            if (formats == null || formats.length == 1) {
                formatter = DateTimeFormat.forPattern(input);
            } else {
                DateTimeParser[] parsers = new DateTimeParser[formats.length];
                for (int i = 0; i < formats.length; i++) {
                    parsers[i] = DateTimeFormat.forPattern(formats[i]).withZone(DateTimeZone.UTC).getParser();
                }
                DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
                        .append(DateTimeFormat.forPattern(formats[0]).withZone(DateTimeZone.UTC).getPrinter(), parsers);
                formatter = builder.toFormatter();
            }
        }
        return new FormatDateTimeFormatter(input, formatter.withZone(DateTimeZone.UTC));
    }
View Full Code Here

    String maxTimestamp = null;
    long maxTimestampParsed = 0;

    Object sourceData = XContentMapValues.extractValue(fieldSource, data);
    if (sourceData != null) {
      DateTimeFormatter dateParser = ISODateTimeFormat.dateTimeParser();
      if (sourceData instanceof Iterable) {
        for (Object o : (Iterable<?>) sourceData) {
          if (o instanceof String) {
            try {
              String timestamp = (String) o;
              if (timestamp != null && !timestamp.trim().isEmpty()) {
                long timestampParsed = dateParser.parseMillis(timestamp.trim());
                if (timestampParsed > maxTimestampParsed) {
                  maxTimestampParsed = timestampParsed;
                  maxTimestamp = timestamp;
                }
              }
            } catch (Exception e) {
              String msg = "Value '" + o + "' in filed '" + fieldSource + "'is not valid timestamp";
              addDataWarning(chainContext, msg);
              logger.debug(msg);
            }
          } else {
            String msg = "Value for field '" + fieldSource + "' is not String but is "
                + sourceData.getClass().getName();
            addDataWarning(chainContext, msg);
            logger.debug(msg);
          }
        }
      } else if (sourceData instanceof String) {
        try {
          String timestamp = (String) sourceData;
          if (timestamp != null && !timestamp.trim().isEmpty()) {
            timestamp = timestamp.trim();
            // parse it to check format
            dateParser.parseMillis(timestamp);
            maxTimestamp = timestamp;
          }
        } catch (Exception e) {
          String msg = "Value '" + sourceData + "' in filed '" + fieldSource + "'is not valid timestamp";
          addDataWarning(chainContext, msg);
View Full Code Here

    String maxTimestamp = null;
    long maxTimestampParsed = 0;

    Object sourceData = XContentMapValues.extractValue(fieldSource, data);
    if (sourceData != null) {
      DateTimeFormatter dateParser = ISODateTimeFormat.dateTimeParser();
      if (sourceData instanceof Iterable) {
        for (Object o : (Iterable<?>) sourceData) {
          if (o instanceof String) {
            try {
              String timestamp = (String) o;
              if (timestamp != null && !timestamp.trim().isEmpty()) {
                long timestampParsed = dateParser.parseMillis(timestamp.trim());
                if (timestampParsed > maxTimestampParsed) {
                  maxTimestampParsed = timestampParsed;
                  maxTimestamp = timestamp;
                }
              }
            } catch (Exception e) {
              logger.debug("Value {} is not valid timestamp", 0);
            }
          }
        }
      } else if (sourceData instanceof String) {
        try {
          String timestamp = (String) sourceData;
          if (timestamp != null && !timestamp.trim().isEmpty()) {
            timestamp = timestamp.trim();
            // parse it to check format
            dateParser.parseMillis(timestamp);
            maxTimestamp = timestamp;
          }
        } catch (Exception e) {
          logger.debug("Value {} is not valid timestamp", 0);
        }
View Full Code Here

    String maxTimestamp = null;
    long maxTimestampParsed = 0;

    Object sourceData = XContentMapValues.extractValue(fieldSource, data);
    if (sourceData != null) {
      DateTimeFormatter dateParser = ISODateTimeFormat.dateTimeParser();
      if (sourceData instanceof Iterable) {
        for (Object o : (Iterable<?>) sourceData) {
          if (o instanceof String) {
            try {
              String timestamp = (String) o;
              if (timestamp != null && !timestamp.trim().isEmpty()) {
                long timestampParsed = dateParser.parseMillis(timestamp.trim());
                if (timestampParsed > maxTimestampParsed) {
                  maxTimestampParsed = timestampParsed;
                  maxTimestamp = timestamp;
                }
              }
            } catch (Exception e) {
              logger.debug("Value {} is not valid timestamp", 0);
            }
          }
        }
      } else if (sourceData instanceof String) {
        try {
          String timestamp = (String) sourceData;
          if (timestamp != null && !timestamp.trim().isEmpty()) {
            timestamp = timestamp.trim();
            // parse it to check format
            dateParser.parseMillis(timestamp);
            maxTimestamp = timestamp;
          }
        } catch (Exception e) {
          logger.debug("Value {} is not valid timestamp", 0);
        }
View Full Code Here

TOP

Related Classes of org.elasticsearch.common.joda.time.format.DateTimeFormatter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.