@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())); }
@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())); }
@Test public void testIsoDateFormatDateTimeNoMillisUTC() { DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC); long millis = formatter.parseMillis("1970-01-01T00:00:00Z"); assertThat(millis, equalTo(0l)); }
@Test 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")); }
@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)); }
private long utcTimeInMillis(String time) { return ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).parseMillis(time); }