Java Code Examples for java.time.Period#parse()
The following examples show how to use
java.time.Period#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: PeriodKeyDeserializer.java From jackson-modules-java8 with Apache License 2.0 | 5 votes |
@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); } }
Example 2
Source File: Main.java From Java-Coding-Problems with MIT License | 5 votes |
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); }
Example 3
Source File: TCKPeriod.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@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()); }
Example 4
Source File: TCKPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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; } }
Example 5
Source File: TCKPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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; } }
Example 6
Source File: JavaPeriodUnitTest.java From tutorials with MIT License | 5 votes |
@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()); }
Example 7
Source File: JdbcExportArgsFactory.java From dbeam with Apache License 2.0 | 5 votes |
/** * 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); } }
Example 8
Source File: TCKPeriod.java From j2objc with Apache License 2.0 | 4 votes |
@Test(expected=NullPointerException.class) public void factory_parse_null() { Period.parse(null); }
Example 9
Source File: TCKPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="parseSuccess") public void factory_parse(String text, Period expected) { Period p = Period.parse(text); assertEquals(p, expected); }
Example 10
Source File: PeriodFormatter.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override public Period parse(String text, Locale locale) throws ParseException { return Period.parse(text); }
Example 11
Source File: TCKPeriod.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="parseSuccess") public void factory_parse_plus(String text, Period expected) { Period p = Period.parse("+" + text); assertEquals(p, expected); }
Example 12
Source File: TCKPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="parseSuccess") public void factory_parse_lowerCase(String text, Period expected) { Period p = Period.parse(text.toLowerCase(Locale.ENGLISH)); assertEquals(p, expected); }
Example 13
Source File: AdditionalDurationScalars.java From smallrye-graphql with Apache License 2.0 | 4 votes |
public AdditionalDurationScalars() { this.duration = Duration.parse("PT1H2M3S"); this.period = Period.parse("P1Y2M3D"); }
Example 14
Source File: TCKPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions=NullPointerException.class) public void factory_parse_null() { Period.parse(null); }
Example 15
Source File: JavaTimeSerializersV2d0.java From tinkerpop with Apache License 2.0 | 4 votes |
@Override public Period parse(final String val) { return Period.parse(val); }
Example 16
Source File: PeriodStringConverter.java From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 | 4 votes |
@Override public Period convertToEntityAttribute(String dbData) { return Period.parse( dbData ); }
Example 17
Source File: RecurrenceRule.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
public static Period convertPeriod(String period) { return period != null ? Period.parse(period) : null; }
Example 18
Source File: PeriodFormatter.java From spring-analysis-note with MIT License | 4 votes |
@Override public Period parse(String text, Locale locale) throws ParseException { return Period.parse(text); }
Example 19
Source File: TCKPeriod.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider="parseSuccess") public void factory_parse_plus(String text, Period expected) { Period p = Period.parse("+" + text); assertEquals(p, expected); }
Example 20
Source File: FpmlDocument.java From Strata with Apache License 2.0 | 3 votes |
/** * 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); }