下面列出了java.time.OffsetDateTime#parse ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@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;
}
}
@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);
}
}
@Override
public OffsetDateTime convert(String source) {
if (source == null) {
return null;
}
return OffsetDateTime.parse(source);
}
@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);
}
}
@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);
}
@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);
}
@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);
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_illegalValue() {
OffsetDateTime.parse("2008-06-32T11:15+01:00");
}
@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)));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
OffsetDateTime.parse("ANY", null);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_nullText() {
OffsetDateTime.parse((String) null);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_nullText() {
OffsetDateTime.parse((String) null);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
OffsetDateTime.parse("ANY", null);
}
@Test(expectedExceptions=DateTimeParseException.class)
public void factory_parse_invalidValue() {
OffsetDateTime.parse("2008-06-31T11:15+01:00");
}
@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);
}
@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)));
}
@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);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_formatter_nullFormatter() {
OffsetDateTime.parse("ANY", null);
}
@Override
protected OffsetDateTime parse(final String input) throws Exception {
return OffsetDateTime.parse(input);
}