Java Code Examples for java.time.OffsetDateTime#parse()

The following examples show how to use java.time.OffsetDateTime#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConvertersTest.java    From dropwizard-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testToOffsetDateTime() {
  final Timestamp timestamp =
      Timestamp.newBuilder().setSeconds(1515761132).setNanos(123000000).build();

  final OffsetDateTime actual = Converters.toOffsetDateTimeUTC.convert(timestamp);
  final OffsetDateTime expected = OffsetDateTime.parse("2018-01-12T12:45:32.123Z");

  assertThat(actual).isEqualTo(expected);
}
 
Example 2
Source File: OffsetDateTimeRangeRandomizerTest.java    From easy-random with MIT License 5 votes vote down vote up
@Test
void generatedOffsetDateTimeShouldBeAlwaysTheSameForTheSameSeed() {
    // Given
    randomizer = aNewOffsetDateTimeRangeRandomizer(minOffsetDateTime, maxOffsetDateTime, SEED);
    OffsetDateTime expected = OffsetDateTime.parse("2046-10-12T17:24:27Z");

    // When
    OffsetDateTime randomValue = randomizer.getRandomValue();

    // Then
    assertThat(randomValue).isEqualTo(expected);
}
 
Example 3
Source File: JSON.java    From influxdb-client-java with MIT License 5 votes vote down vote up
@Override
public OffsetDateTime read(JsonReader in) throws IOException {
    switch (in.peek()) {
        case NULL:
            in.nextNull();
            return null;
        default:
            String date = in.nextString();
            if (date.endsWith("+0000")) {
                date = date.substring(0, date.length()-5) + "Z";
            }
            return OffsetDateTime.parse(date, formatter);
    }
}
 
Example 4
Source File: TestExpressionToSearchArgument.java    From iceberg with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimezoneSensitiveTypes() {
  TimeZone currentTz = TimeZone.getDefault();
  try {
    for (String timezone : new String[]{"America/New_York", "Asia/Kolkata", "UTC/Greenwich"}) {
      TimeZone.setDefault(TimeZone.getTimeZone(timezone));
      OffsetDateTime tsTzPredicate = OffsetDateTime.parse("2019-10-02T00:47:28.207366Z");
      OffsetDateTime tsPredicate = OffsetDateTime.parse("1968-01-16T13:07:59.048625Z");
      OffsetDateTime epoch = Instant.ofEpochSecond(0).atOffset(ZoneOffset.UTC);

      Schema schema = new Schema(
          required(1, "date", Types.DateType.get()),
          required(2, "tsTz", Types.TimestampType.withZone()),
          required(3, "ts", Types.TimestampType.withoutZone())
      );

      Expression expr = and(
          and(equal("date", 10L), equal("tsTz", ChronoUnit.MICROS.between(epoch, tsTzPredicate))),
          equal("ts", ChronoUnit.MICROS.between(epoch, tsPredicate))
      );
      Expression boundFilter = Binder.bind(schema.asStruct(), expr, true);
      SearchArgument expected = SearchArgumentFactory.newBuilder()
          .startAnd()
          .equals("`date`", Type.DATE, Date.valueOf(LocalDate.parse("1970-01-11", DateTimeFormatter.ISO_LOCAL_DATE)))
          // .equals("`tsTz`", Type.TIMESTAMP, Timestamp.from(tsTzPredicate.toInstant()))
          // .equals("`ts`", Type.TIMESTAMP, Timestamp.from(tsPredicate.toInstant()))
          .end()
          .build();

      SearchArgument actual = ExpressionToSearchArgument.convert(boundFilter, ORCSchemaUtil.convert(schema));
      Assert.assertEquals(expected.toString(), actual.toString());
    }
  } finally {
    TimeZone.setDefault(currentTz);
  }
}
 
Example 5
Source File: StringToOffsetDateTimeConverter.java    From sample-data-generator with Apache License 2.0 5 votes vote down vote up
@Override
public OffsetDateTime convert(String source) {

    if (source == null) {
        return null;
    }

    return OffsetDateTime.parse(source);
}
 
Example 6
Source File: OffsetDateTimeKeyDeserializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
protected OffsetDateTime deserialize(String key, DeserializationContext ctxt) throws IOException {
    try {
        return OffsetDateTime.parse(key, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    } catch (DateTimeException e) {
        return _handleDateTimeException(ctxt, OffsetDateTime.class, e, key);
    }
}
 
Example 7
Source File: OffsetDateTimeConverter.java    From incubator-tamaya with Apache License 2.0 5 votes vote down vote up
@Override
public OffsetDateTime convert(String value, ConversionContext ctx) {
    ctx.addSupportedFormats(getClass(), OffsetDateTime.now().toString());
    if(value==null){
        return null;
    }
    try{
        return OffsetDateTime.parse(value);
    }catch(Exception e){
        LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetDateTime: " + value);
        return null;
    }
}
 
Example 8
Source File: TCKOffsetDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullText() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
    OffsetDateTime.parse((String) null, f);
}
 
Example 9
Source File: OffsetDateTimeParam.java    From dropwizard-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected OffsetDateTime parse(final String input) throws Exception {
    return OffsetDateTime.parse(input);
}
 
Example 10
Source File: TCKOffsetDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
    OffsetDateTime.parse("ANY", null);
}
 
Example 11
Source File: TCKOffsetDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullText() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
    OffsetDateTime.parse((String) null, f);
}
 
Example 12
Source File: TCKOffsetDateTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_parse_formatter() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s XXX");
    OffsetDateTime test = OffsetDateTime.parse("2010 12 3 11 30 0 +01:00", f);
    assertEquals(test, OffsetDateTime.of(LocalDate.of(2010, 12, 3), LocalTime.of(11, 30), ZoneOffset.ofHours(1)));
}
 
Example 13
Source File: TCKOffsetDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullText() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
    OffsetDateTime.parse((String) null, f);
}
 
Example 14
Source File: TCKOffsetDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_invalidValue() {
    OffsetDateTime.parse("2008-06-31T11:15+01:00");
}
 
Example 15
Source File: TCKOffsetDateTime.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
    OffsetDateTime.parse("ANY", null);
}
 
Example 16
Source File: TCKOffsetDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_nullText() {
    OffsetDateTime.parse((String) null);
}
 
Example 17
Source File: TCKOffsetDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_nullText() {
    OffsetDateTime.parse((String) null);
}
 
Example 18
Source File: TCKOffsetDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
    OffsetDateTime.parse("ANY", null);
}
 
Example 19
Source File: TCKOffsetDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_parse_formatter() {
    DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s XXX");
    OffsetDateTime test = OffsetDateTime.parse("2010 12 3 11 30 0 +01:00", f);
    assertEquals(test, OffsetDateTime.of(LocalDate.of(2010, 12, 3), LocalTime.of(11, 30), ZoneOffset.ofHours(1)));
}
 
Example 20
Source File: TCKOffsetDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_illegalValue() {
    OffsetDateTime.parse("2008-06-32T11:15+01:00");
}