java.time.format.DateTimeFormatter.parse()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(219)

本文整理了Java中java.time.format.DateTimeFormatter.parse()方法的一些代码示例,展示了DateTimeFormatter.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter.parse()方法的具体详情如下:
包路径:java.time.format.DateTimeFormatter
类名称:DateTimeFormatter
方法名:parse

DateTimeFormatter.parse介绍

[英]Fully parses the text producing a temporal object.

This parses the entire text producing a temporal object. It is typically more useful to use #parse(CharSequence,TemporalQuery). The result of this method is TemporalAccessor which has been resolved, applying basic validation checks to help ensure a valid date-time.

If the parse completes without reading the entire length of the text, or a problem occurs during parsing or merging, then an exception is thrown.
[中]完全解析文本,生成一个时态对象。
这将解析整个文本,生成一个时态对象。使用#parse(CharSequence,TemporalQuery)通常更有用。此方法的结果是已解决的TemporalAccessor,它应用基本验证检查来帮助确保有效的日期和时间。
如果解析完成时没有读取整个文本长度,或者在解析或合并过程中出现问题,则会引发异常。

代码示例

代码示例来源:origin: prestodb/presto

private ZonedDateTime zonedDateTime(String value)
{
  return ZONED_DATE_TIME_FORMAT.parse(value, ZonedDateTime::from);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Instant parse(String text, Locale locale) throws ParseException {
  if (text.length() > 0 && Character.isDigit(text.charAt(0))) {
    // assuming UTC instant a la "2007-12-03T10:15:30.00Z"
    return Instant.parse(text);
  }
  else {
    // assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
    return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
  }
}

代码示例来源:origin: prestodb/presto

@Override
  protected Instant deserialize(String key, DeserializationContext ctxt) throws IOException {
    try {
      return DateTimeFormatter.ISO_INSTANT.parse(key, Instant::from);
    } catch (DateTimeException e) {
      return _handleDateTimeException(ctxt, Instant.class, e, key);
    }
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public Instant instantValue() {
 return value != null ? Instant.from(ISO_INSTANT.parse((CharSequence) value)) : null;
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Like {@link #getInstant(String)} but specifying a default value to return if there is no entry.
 *
 * @param key  the key to lookup
 * @param def  the default value to use if the entry is not present
 * @return the value or {@code def} if no entry present
 */
public Instant getInstant(String key, Instant def) {
 Objects.requireNonNull(key);
 Object val = map.get(key);
 return val != null || map.containsKey(key) ?
   (val == null ? null : Instant.from(ISO_INSTANT.parse((String) val))) : def;
}

代码示例来源:origin: org.springframework/spring-context

@Override
public Instant parse(String text, Locale locale) throws ParseException {
  if (text.length() > 0 && Character.isDigit(text.charAt(0))) {
    // assuming UTC instant a la "2007-12-03T10:15:30.00Z"
    return Instant.parse(text);
  }
  else {
    // assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
    return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
  }
}

代码示例来源:origin: prestodb/presto

private boolean meetsPredicate(List<String> fields)
{
  if (!timestampOrdinalPosition.isPresent() || !domain.isPresent()) {
    return true;
  }
  long millis = Instant.from(ISO_FORMATTER.parse(fields.get(timestampOrdinalPosition.getAsInt()))).toEpochMilli();
  return domain.get().includesNullableValue(millis);
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public ZonedDateTime fromString(String string) {
  return ZonedDateTime.from( ZonedDateTimeType.FORMATTER.parse( string ) );
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public LocalDateTime fromString(String string) {
  return LocalDateTime.from( LocalDateTimeType.FORMATTER.parse( string ) );
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Get the instant value with the specified key.
 * <p>
 * JSON itself has no notion of a temporal types, this extension complies to the RFC-7493, so this method assumes
 * there is a String value with the key and it contains an ISO 8601 encoded date and time format
 * such as "2017-04-03T10:25:41Z", which it decodes if found and returns.
 * <p>
 * This method should be used in conjunction with {@link #put(String, java.time.Instant)}
 *
 * @param key  the key to return the value for
 * @return the value or null if no value for that key
 * @throws java.lang.ClassCastException if the value is not a String
 * @throws java.time.format.DateTimeParseException if the String value is not a legal ISO 8601 encoded value
 */
public Instant getInstant(String key) {
 Objects.requireNonNull(key);
 String encoded = (String) map.get(key);
 return encoded == null ? null : Instant.from(ISO_INSTANT.parse(encoded));
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public LocalTime fromString(String string) {
  return LocalTime.from( LocalTimeType.FORMATTER.parse( string ) );
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public OffsetTime fromString(String string) {
  return OffsetTime.from( OffsetTimeType.FORMATTER.parse( string ) );
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public LocalDate fromString(String string) {
  return LocalDate.from( LocalDateType.FORMATTER.parse( string ) );
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  String text = p.getText();
  try {
   return Instant.from(ISO_INSTANT.parse(text));
  } catch (DateTimeException e) {
   throw new InvalidFormatException(p, "Expected an ISO 8601 formatted date time", text, Instant.class);
  }
 }
}

代码示例来源:origin: prestodb/presto

@Override
public long getLong(int field)
{
  if (getType(field).equals(TIMESTAMP)) {
    return Instant.from(ISO_FORMATTER.parse(getFieldValue(field))).toEpochMilli();
  }
  else {
    checkFieldType(field, BIGINT, INTEGER);
    return Long.parseLong(getFieldValue(field));
  }
}

代码示例来源:origin: neo4j/neo4j

static ZoneId parseZoneName( String zoneName )
{
  ZoneId parsedName;
  try
  {
    parsedName = ZONE_NAME_PARSER.parse( zoneName.replace( ' ', '_' ) ).query( TemporalQueries.zoneId() );
  }
  catch ( DateTimeParseException e )
  {
    throw new TemporalParseException( "Invalid value for TimeZone: " + e.getMessage(), e.getParsedString(),
        e.getErrorIndex(), e );
  }
  return parsedName;
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void encodeCustomTypeInstant() {
 Instant now = Instant.now();
 String json = Json.encode(now);
 assertNotNull(json);
 // the RFC is one way only
 Instant decoded = Instant.from(ISO_INSTANT.parse(json.substring(1, json.length() - 1)));
 assertEquals(now, decoded);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void instantConversion() throws Exception {
  String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
  MockServerHttpRequest request = MockServerHttpRequest.get("/").header("name", rfc1123val).build();
  ServerWebExchange exchange = MockServerWebExchange.from(request);
  Mono<Object> mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, exchange);
  Object result = mono.block();
  assertTrue(result instanceof Instant);
  assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testInstantDecoding() {
 Pojo original = new Pojo();
 original.instant = Instant.from(ISO_INSTANT.parse("2018-06-20T07:25:38.397Z"));
 Pojo decoded = Json.decodeValue("{\"instant\":\"2018-06-20T07:25:38.397Z\"}", Pojo.class);
 assertEquals(original.instant, decoded.instant);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void instantConversion() throws Exception {
  String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
  servletRequest.addHeader("name", rfc1123val);
  ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
  bindingInitializer.setConversionService(new DefaultFormattingConversionService());
  Object result = resolver.resolveArgument(paramInstant, null, webRequest,
      new DefaultDataBinderFactory(bindingInitializer));
  assertTrue(result instanceof Instant);
  assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result);
}

相关文章