下面列出了java.time.Period#parse ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
public static void main(String[] args) {
Period fromDays = Period.ofDays(120);
System.out.println("Period from days: " + fromDays);
Period periodFromUnits = Period.of(2000, 11, 24);
System.out.println("Period from units: " + periodFromUnits);
LocalDate localDate = LocalDate.now();
Period periodFromLocalDate = Period.of(localDate.getYear(),
localDate.getMonthValue(), localDate.getDayOfMonth());
System.out.println("Period from LocalDate: " + periodFromLocalDate);
Period periodFromString = Period.parse("P2019Y2M25D");
System.out.println("Period from String: " + periodFromString);
LocalDate startLocalDate = LocalDate.of(2018, 3, 12);
LocalDate endLocalDate = LocalDate.of(2019, 7, 20);
Period periodBetween = Period.between(startLocalDate, endLocalDate);
System.out.println("\nBetween " + startLocalDate + " and "
+ endLocalDate + " there are " + periodBetween.getYears() + " year(s)");
System.out.println("Between " + startLocalDate + " and "
+ endLocalDate + " there are " + periodBetween.getMonths() + " month(s)");
System.out.println("Between " + startLocalDate + " and "
+ endLocalDate + " there are " + periodBetween.getDays() + " days(s)");
System.out.println("Expressed as y:m:d: " + periodToYMD(periodBetween));
System.out.println(startLocalDate + " is after "
+ endLocalDate + " ? " + periodBetween.isNegative());
Period periodBetweenPlus1Year = periodBetween.plusYears(1L);
System.out.println("\n" + periodBetween + " has " + periodBetween.getYears() + " year,"
+ " after adding one year it has " + periodBetweenPlus1Year.getYears());
Period p1 = Period.ofDays(5);
Period p2 = Period.ofDays(20);
Period p1p2 = p1.plus(p2);
System.out.println(p1 + "+" + p2 + "=" + p1p2);
}
@Test(dataProvider="parseSuccess")
public void factory_parse_minus(String text, Period expected) {
Period p = null;
try {
p = Period.parse("-" + text);
} catch (DateTimeParseException ex) {
assertEquals(expected.getYears() == Integer.MIN_VALUE ||
expected.getMonths() == Integer.MIN_VALUE ||
expected.getDays() == Integer.MIN_VALUE, true);
return;
}
// not inside try/catch or it breaks test
assertEquals(p, expected.negated());
}
@Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
public void factory_parseFailures(String text) {
try {
Period.parse(text);
} catch (DateTimeParseException ex) {
assertEquals(ex.getParsedString(), text);
throw ex;
}
}
@Test
public void whenTestPeriod_thenOk() {
LocalDate startDate = LocalDate.of(2015, 2, 15);
LocalDate endDate = LocalDate.of(2017, 1, 21);
Period period = Period.between(startDate, endDate);
LOG.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));
assertFalse(period.isNegative());
assertEquals(56, period.plusDays(50)
.getDays());
assertEquals(9, period.minusMonths(2)
.getMonths());
Period fromUnits = Period.of(3, 10, 10);
Period fromDays = Period.ofDays(50);
Period fromMonths = Period.ofMonths(5);
Period fromYears = Period.ofYears(10);
Period fromWeeks = Period.ofWeeks(40);
assertEquals(280, fromWeeks.getDays());
Period fromCharYears = Period.parse("P2Y");
assertEquals(2, fromCharYears.getYears());
Period fromCharUnits = Period.parse("P2Y3M5D");
assertEquals(5, fromCharUnits.getDays());
}
/**
* Defaults to a Period of 1 days in case of empty.
* If partitionPeriod argument is a sub day unit (e.g. hourly), then uses Duration (stored in
* seconds).
* Otherwise use Period.
* @param partitionPeriod partitionPeriod parameter to be parsed
* @return
*/
private static TemporalAmount partitionPeriodTemporalAmount(final String partitionPeriod) {
if (partitionPeriod == null) {
return Period.ofDays(1);
} else if (partitionPeriod.contains("PT")) {
return Duration.parse(partitionPeriod);
} else {
return Period.parse(partitionPeriod);
}
}
@Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
public void factory_parseFailures(String text) {
try {
Period.parse(text);
} catch (DateTimeParseException ex) {
assertEquals(ex.getParsedString(), text);
throw ex;
}
}
@Test(dataProvider="parseSuccess")
public void factory_parse_plus(String text, Period expected) {
Period p = Period.parse("+" + text);
assertEquals(p, expected);
}
@Override
public Period parse(String text, Locale locale) throws ParseException {
return Period.parse(text);
}
public static Period convertPeriod(String period) {
return period != null ? Period.parse(period) : null;
}
@Override
public Period convertToEntityAttribute(String dbData) {
return Period.parse( dbData );
}
@Override
public Period parse(final String val) {
return Period.parse(val);
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_parse_null() {
Period.parse(null);
}
public AdditionalDurationScalars() {
this.duration = Duration.parse("PT1H2M3S");
this.period = Period.parse("P1Y2M3D");
}
@Test(dataProvider="parseSuccess")
public void factory_parse_lowerCase(String text, Period expected) {
Period p = Period.parse(text.toLowerCase(Locale.ENGLISH));
assertEquals(p, expected);
}
@Test(dataProvider="parseSuccess")
public void factory_parse_plus(String text, Period expected) {
Period p = Period.parse("+" + text);
assertEquals(p, expected);
}
@Override
public Period parse(String text, Locale locale) throws ParseException {
return Period.parse(text);
}
@Test(dataProvider="parseSuccess")
public void factory_parse(String text, Period expected) {
Period p = Period.parse(text);
assertEquals(p, expected);
}
@Test(expected=NullPointerException.class)
public void factory_parse_null() {
Period.parse(null);
}
/**
* Converts an FpML 'Period' to a {@code Period}.
*
* @param baseEl the FpML element to parse
* @return the period
* @throws RuntimeException if unable to parse
*/
public Period parsePeriod(XmlElement baseEl) {
// FpML content: ('periodMultiplier', 'period')
String multiplier = baseEl.getChild("periodMultiplier").getContent();
String unit = baseEl.getChild("period").getContent();
return Period.parse("P" + multiplier + unit);
}