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

x33g5p2x  于2022-01-26 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(151)

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

Period.parse介绍

[英]Obtains a Period from a text string such as PnYnMnD.

This will parse the string produced by toString() which is based on the ISO-8601 period formats PnYnMnD and PnW.

The string starts with an optional sign, denoted by the ASCII negative or positive symbol. If negative, the whole period is negated. The ASCII letter "P" is next in upper or lower case. There are then four sections, each consisting of a number and a suffix. At least one of the four sections must be present. The sections have suffixes in ASCII of "Y", "M", "W" and "D" for years, months, weeks and days, accepted in upper or lower case. The suffixes must occur in order. The number part of each section must consist of ASCII digits. The number may be prefixed by the ASCII negative or positive symbol. The number must parse to an int.

The leading plus/minus sign, and negative values for other units are not part of the ISO-8601 standard. In addition, ISO-8601 does not permit mixing between the PnYnMnD and PnW formats. Any week-based input is multiplied by 7 and treated as a number of days.

For example, the following are valid inputs:

"P2Y"             -- Period.ofYears(2) 
"P3M"             -- Period.ofMonths(3) 
"P4W"             -- Period.ofWeeks(4) 
"P5D"             -- Period.ofDays(5) 
"P1Y2M3D"         -- Period.of(1, 2, 3) 
"P1Y2M3W4D"       -- Period.of(1, 2, 25) 
"P-1Y2M"          -- Period.of(-1, 2, 0) 
"-P1Y2M"          -- Period.of(-1, -2, 0)

[中]从文本字符串(如PnYnMnD)中获取句点。
这将解析toString()生成的字符串,该字符串基于ISO-8601句点格式PnYnMnD和PnW。
字符串以可选符号开头,由ASCII负号或正号表示。如果为负值,则整个周期为负值。ASCII字母“P”是下一个大写或小写字母。然后有四个部分,每个部分由一个数字和一个后缀组成。必须至少有四个部分中的一个。这些部分的后缀是ASCII中的“Y”、“M”、“W”和“D”,表示年、月、周和日,可以用大写或小写。后缀必须按顺序出现。每个部分的数字部分必须由ASCII数字组成。数字的前缀可以是ASCII负数或正数。数字必须解析为整数。
前导加号/减号和其他单位的负值不属于ISO-8601标准的一部分。此外,ISO-8601不允许PnYnMnD和PnW格式之间的混合。任何基于周的输入都会乘以7,并被视为天数。
例如,以下是有效的输入:

"P2Y"             -- Period.ofYears(2) 
"P3M"             -- Period.ofMonths(3) 
"P4W"             -- Period.ofWeeks(4) 
"P5D"             -- Period.ofDays(5) 
"P1Y2M3D"         -- Period.of(1, 2, 3) 
"P1Y2M3W4D"       -- Period.of(1, 2, 25) 
"P-1Y2M"          -- Period.of(-1, 2, 0) 
"-P1Y2M"          -- Period.of(-1, -2, 0)

代码示例

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

@Override
public Period parse(String text, Locale locale) throws ParseException {
  return Period.parse(text);
}

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

@Override
public Period parse(String text, Locale locale) throws ParseException {
  return Period.parse(text);
}

代码示例来源:origin: oracle/helidon

/**
 * Maps {@code stringValue} to {@code Period}.
 *
 * @param stringValue source value as a {@code String}
 * @return mapped {@code stringValue} to {@code Period}
 * @see Period#parse(CharSequence)
 */
public static Period toPeriod(String stringValue) {
  return Period.parse(stringValue);
}

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

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

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

@Override
  public Period convertToEntityAttribute(String dbData) {
    return Period.parse( dbData );
  }
}

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

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

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

@Override
  public Period convert(byte[] source) {
    return Period.parse(toString(source));
  }
}

代码示例来源:origin: SonarSource/sonarqube

private void setCreatedAfterFromDates(IssueQuery.Builder builder, @Nullable Date createdAfter, @Nullable String createdInLast, boolean createdAfterInclusive) {
 checkArgument(createdAfter == null || createdInLast == null, format("Parameters %s and %s cannot be set simultaneously", PARAM_CREATED_AFTER, PARAM_CREATED_IN_LAST));
 Date actualCreatedAfter = createdAfter;
 if (createdInLast != null) {
  actualCreatedAfter = Date.from(
   OffsetDateTime.now(clock)
    .minus(Period.parse("P" + createdInLast.toUpperCase(Locale.ENGLISH)))
    .toInstant());
 }
 builder.createdAfter(actualCreatedAfter, createdAfterInclusive);
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

@Override
  public Period fromString(final String str) {
    try {
      return Period.parse(str);
    } catch (final DateTimeParseException ex) {
      final ConversionException exception = new ConversionException("Cannot parse period value", ex);
      exception.add("period", str);
      throw exception;
    }
  }
}

代码示例来源:origin: alibaba/fastjson

Period period = Period.parse(text);

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

switch (_valueType) {
case TYPE_PERIOD:
  return Period.parse(string);
case TYPE_ZONE_ID:
  return ZoneId.of(string);

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

switch (_valueType) {
case TYPE_PERIOD:
  return Period.parse(string);
case TYPE_ZONE_ID:
  return ZoneId.of(string);

代码示例来源:origin: com.alibaba/fastjson

Period period = Period.parse(text);

代码示例来源:origin: apache/tinkerpop

@Override
  public Period parse(final String val) {
    return Period.parse(val);
  }
}

代码示例来源:origin: apache/tinkerpop

@Override
  public Period parse(final String val) {
    return Period.parse(val);
  }
}

代码示例来源:origin: apache/tinkerpop

@Override
  public Period parse(final String val) {
    return Period.parse(val);
  }
}

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

@Override
public Period parse(String value) {
 return Period.parse(value);
}

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

),
testInput( "Duration", Values::durationValue, (TemporalAmount) Duration.parse( "P2DT3H4M" ), Period.parse( "P1Y2M3W4D" ) ),
testInput( "DurationArray", Values::durationArray,
    new TemporalAmount[] {Duration.parse( "P2DT3H4M" ), Period.parse( "P1Y2M3W4D" )}, new TemporalAmount[0] ),

代码示例来源:origin: pholser/junit-quickcheck

@Property public void shouldHold(
    @InRange(min = "P1Y2M3D", max = "P36Y2M3D") Period p) {
    assertThat(
      toBigInteger(p),
      allOf(
        greaterThanOrEqualTo(toBigInteger(Period.parse("P1Y2M3D"))),
        lessThanOrEqualTo(toBigInteger(Period.parse("P36Y2M3D")))));
  }
}

代码示例来源:origin: pholser/junit-quickcheck

@Property public void shouldHold(@InRange(min = "P36Y2M3D") Period p) {
    assertThat(
      toBigInteger(p),
      greaterThanOrEqualTo(toBigInteger(Period.parse("P36Y2M3D"))));
  }
}

相关文章