Search in sources :

Example 96 with Period

use of java.time.Period in project j2objc by google.

the class TCKPeriod method test_plusYears_overflowTooBig.

@Test(expected = ArithmeticException.class)
public void test_plusYears_overflowTooBig() {
    Period test = Period.ofYears(Integer.MAX_VALUE);
    test.plusYears(1);
}
Also used : Period(java.time.Period) Test(org.junit.Test)

Example 97 with Period

use of java.time.Period in project j2objc by google.

the class TCKPeriod method test_plusDays_overflowTooBig.

@Test(expected = ArithmeticException.class)
public void test_plusDays_overflowTooBig() {
    Period test = Period.ofDays(Integer.MAX_VALUE);
    test.plusDays(1);
}
Also used : Period(java.time.Period) Test(org.junit.Test)

Example 98 with Period

use of java.time.Period in project j2objc by google.

the class TCKPeriod method test_minusMonths_overflowTooBig.

@Test(expected = ArithmeticException.class)
public void test_minusMonths_overflowTooBig() {
    Period test = Period.ofMonths(Integer.MAX_VALUE);
    test.minusMonths(-1);
}
Also used : Period(java.time.Period) Test(org.junit.Test)

Example 99 with Period

use of java.time.Period in project quickstart by wildfly.

the class SampleEndPoint method birthday.

@GET()
@Path("/birthday")
@RolesAllowed({ "Subscriber" })
public String birthday() {
    if (birthdate.isPresent()) {
        LocalDate birthdate = LocalDate.parse(this.birthdate.get().toString());
        LocalDate today = LocalDate.now();
        LocalDate next = birthdate.withYear(today.getYear());
        if (today.equals(next)) {
            return "Happy Birthday";
        }
        if (next.isBefore(today)) {
            next = next.withYear(next.getYear() + 1);
        }
        Period wait = today.until(next);
        return String.format("%d months and %d days until your next birthday.", wait.getMonths(), wait.getDays());
    }
    return "Sorry, we don't know your birthdate.";
}
Also used : Period(java.time.Period) LocalDate(java.time.LocalDate) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) GET(javax.ws.rs.GET)

Example 100 with Period

use of java.time.Period in project tutorials by eugenp.

the class JavaPeriodUnitTest method whenTestPeriod_thenOk.

@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());
}
Also used : Period(java.time.Period) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

Period (java.time.Period)110 Test (org.junit.Test)37 Test (org.testng.annotations.Test)27 LocalDate (java.time.LocalDate)21 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)8 Duration (java.time.Duration)8 LocalDateTime (java.time.LocalDateTime)7 ZonedDateTime (java.time.ZonedDateTime)7 DateTimeFormatter (java.time.format.DateTimeFormatter)6 Date (java.util.Date)6 Instant (java.time.Instant)5 LocalTime (java.time.LocalTime)5 ZoneId (java.time.ZoneId)5 Test (org.junit.jupiter.api.Test)5 DateTimeParseException (java.time.format.DateTimeParseException)4 TemporalAmount (java.time.temporal.TemporalAmount)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 OffsetTime (java.time.OffsetTime)3