Java Code Examples for java.time.Duration#ofDays()

The following examples show how to use java.time.Duration#ofDays() . 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: Parser.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/**
 * resolve time expression
 *
 * @param ds
 * @return
 */
public static Duration parseDuration(String ds) {
    java.util.regex.Matcher matcher = pattern.matcher(ds);
    if (!matcher.matches()) {
        throw new IllegalStateException("illegal duration:" + ds);
    }
    if (ds.endsWith("ms")) {
        return Duration.ofMillis(Long.parseLong(ds.substring(0, ds.length() - 2)));
    } else if (ds.endsWith("s")) {
        return Duration.ofSeconds(Long.parseLong(ds.substring(0, ds.length() - 1)));
    } else if (ds.endsWith("m")) {
        return Duration.ofMinutes(Long.parseLong(ds.substring(0, ds.length() - 1)));
    } else if (ds.endsWith("h")) {
        return Duration.ofHours(Long.parseLong(ds.substring(0, ds.length() - 1)));
    } else if (ds.endsWith("d")) {
        return Duration.ofDays(Long.parseLong(ds.substring(0, ds.length() - 1)));
    } else if (ds.endsWith("w")) {
        return Duration.ofDays(7 * Long.parseLong(ds.substring(0, ds.length() - 1)));
    } else if (ds.endsWith("y")) {
        return Duration.ofDays(365 * Long.parseLong(ds.substring(0, ds.length() - 1)));
    }
    throw new IllegalStateException("illegal duration expr:" + ds);
}
 
Example 2
Source File: TCKYearMonth.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="plus_TemporalAmount")
Object[][] data_plus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(-11, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(-999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(1, 2), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(11, 2), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(-1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(2, 3), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(-12, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(2, 3), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(-12, 12), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
Example 3
Source File: TCKDuration.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test()
@UseDataProvider("provider_minusDays_long")
public void minusDays_long(long days, long amount, long expectedDays) {
    Duration t = Duration.ofDays(days);
    t = t.minusDays(amount);
    assertEquals(t.toDays(), expectedDays);
}
 
Example 4
Source File: SpareCapacityMaintainerTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
private SpareCapacityMaintainerTester(int maxIterations) {
    NodeFlavors flavors = new NodeFlavors(new FlavorConfigBuilder().build());
    nodeRepository = new NodeRepository(flavors,
                                        new EmptyProvisionServiceProvider().getHostResourcesCalculator(),
                                        new MockCurator(),
                                        new ManualClock(),
                                        new Zone(Environment.prod, RegionName.from("us-east-3")),
                                        new MockNameResolver().mockAnyLookup(),
                                        DockerImage.fromString("docker-registry.domain.tld:8080/dist/vespa"),
                                        true,
                                        false,
                                        1);
    deployer = new MockDeployer(nodeRepository);
    maintainer = new SpareCapacityMaintainer(deployer, nodeRepository, metric, Duration.ofDays(1), maxIterations);
}
 
Example 5
Source File: JavaDurationUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void test2() {
    Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
    Instant end = Instant.parse("2017-10-03T10:16:30.00Z");

    Duration duration = Duration.between(start, end);

    assertFalse(duration.isNegative());

    assertEquals(60, duration.getSeconds());
    assertEquals(1, duration.toMinutes());

    Duration fromDays = Duration.ofDays(1);
    assertEquals(86400, fromDays.getSeconds());

    Duration fromMinutes = Duration.ofMinutes(60);
    assertEquals(1, fromMinutes.toHours());

    assertEquals(120, duration.plusSeconds(60)
        .getSeconds());
    assertEquals(30, duration.minusSeconds(30)
        .getSeconds());

    assertEquals(120, duration.plus(60, ChronoUnit.SECONDS)
        .getSeconds());
    assertEquals(30, duration.minus(30, ChronoUnit.SECONDS)
        .getSeconds());

    Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
    Duration fromChar2 = Duration.parse("PT10M");
}
 
Example 6
Source File: TCKYear.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_plusInvalidUnit() {
    return new Object[][] {
            {Period.of(0, 1, 0)},
            {Period.of(0, 0, 1)},
            {Period.of(0, 1, 1)},
            {Period.of(1, 1, 1)},
            {Duration.ofDays(1)},
            {Duration.ofHours(1)},
            {Duration.ofMinutes(1)},
            {Duration.ofSeconds(1)},
    };
}
 
Example 7
Source File: PingIntegrationTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@Test
public void testManualPingTimeout() throws Exception {
  // sending PING manually to check eth2RpcOutstandingPingThreshold works correctly
  Duration pingPeriod = Duration.ofDays(1);
  network1 =
      networkFactory
          .builder()
          .eth2RpcPingInterval(pingPeriod)
          .eth2RpcOutstandingPingThreshold(2)
          // remove PING method
          .rpcMethodsModifier(m -> Stream.of(m).filter(t -> !t.getId().contains("/ping")))
          .startNetwork();
  network2 =
      networkFactory
          .builder()
          .eth2RpcPingInterval(pingPeriod)
          .eth2RpcOutstandingPingThreshold(2)
          .peer(network1)
          .startNetwork();

  peer1 = network2.getPeer(network1.getNodeId()).orElseThrow();
  peer2 = network1.getPeer(network2.getNodeId()).orElseThrow();

  Eth2PeerManagerAccess.invokeSendPeriodicPing(getPeerManager(network2), peer1);
  assertThat(peer1.isConnected()).isTrue();
  Eth2PeerManagerAccess.invokeSendPeriodicPing(getPeerManager(network2), peer1);
  assertThat(peer1.isConnected()).isTrue();
  // the 3rd ping attempt should disconnect the peer since there are 2 unanswered PING requests
  Eth2PeerManagerAccess.invokeSendPeriodicPing(getPeerManager(network2), peer1);
  waitFor(() -> assertThat(peer1.isConnected()).isFalse());
}
 
Example 8
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="minus_TemporalAmount")
Object[][] data_minus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(13, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(-999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(0, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(-10, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(-1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
Example 9
Source File: TCKYearMonth.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="minus_TemporalAmount")
Object[][] data_minus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(13, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(-999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(0, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(-10, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(-1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
Example 10
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusDays_long_overflowTooBig() {
    Duration t = Duration.ofDays(1);
    t.plusDays(Long.MAX_VALUE/3600/24);
}
 
Example 11
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_days_min() {
    Duration test = Duration.ofDays(Long.MIN_VALUE / 86400);
    assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400);
    assertEquals(test.getNano(), 0);
}
 
Example 12
Source File: TCKDuration.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=ArithmeticException.class)
public void plusDays_long_overflowTooBig() {
    Duration t = Duration.ofDays(1);
    t.plusDays(Long.MAX_VALUE/3600/24);
}
 
Example 13
Source File: TCKDuration.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="PlusDays")
public void plusDays_long(long days, long amount, long expectedDays) {
    Duration t = Duration.ofDays(days);
    t = t.plusDays(amount);
    assertEquals(t.toDays(), expectedDays);
}
 
Example 14
Source File: TCKDuration.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void minusDays_long_overflowTooSmall() {
    Duration t = Duration.ofDays(Long.MIN_VALUE/3600/24);
    t.minusDays(1);
}
 
Example 15
Source File: SummarizerJob.java    From robozonky with Apache License 2.0 4 votes vote down vote up
@Override
public Duration repeatEvery() {
    return Duration.ofDays(7);
}
 
Example 16
Source File: TCKDuration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_days() {
    Duration test = Duration.ofDays(2);
    assertEquals(test.getSeconds(), 2 * 86400);
    assertEquals(test.getNano(), 0);
}
 
Example 17
Source File: TCKDuration.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void minusDays_long_overflowTooSmall() {
    Duration t = Duration.ofDays(Long.MIN_VALUE/3600/24);
    t.minusDays(1);
}
 
Example 18
Source File: LoadBalancerExpirerTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Test
public void expire_inactive() {
    LoadBalancerExpirer expirer = new LoadBalancerExpirer(tester.nodeRepository(),
                                                          Duration.ofDays(1),
                                                          tester.loadBalancerService());
    Supplier<Map<LoadBalancerId, LoadBalancer>> loadBalancers = () -> tester.nodeRepository().database().readLoadBalancers((ignored) -> true);

    // Deploy two applications with a total of three load balancers
    ClusterSpec.Id cluster1 = ClusterSpec.Id.from("qrs");
    ClusterSpec.Id cluster2 = ClusterSpec.Id.from("qrs2");
    ApplicationId app1 = tester.makeApplicationId();
    ApplicationId app2 = tester.makeApplicationId();
    LoadBalancerId lb1 = new LoadBalancerId(app1, cluster1);
    LoadBalancerId lb2 = new LoadBalancerId(app2, cluster1);
    LoadBalancerId lb3 = new LoadBalancerId(app2, cluster2);
    deployApplication(app1, cluster1);
    deployApplication(app2, cluster1, cluster2);
    assertEquals(3, loadBalancers.get().size());

    // Remove one application deactivates load balancers for that application
    removeApplication(app1);
    assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb1).state());
    assertNotSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb2).state());

    // Expirer defers removal while nodes are still allocated to application
    expirer.maintain();
    assertEquals(3, tester.loadBalancerService().instances().size());
    assertEquals(2, tester.loadBalancerService().instances().get(lb1).reals().size());
    dirtyNodesOf(app1, cluster1);

    // Expirer prunes reals before expiration time of load balancer itself
    expirer.maintain();
    assertEquals(Set.of(), tester.loadBalancerService().instances().get(lb1).reals());
    assertEquals(Set.of(), loadBalancers.get().get(lb1).instance().reals());

    // Expirer defers removal of load balancer until expiration time passes
    expirer.maintain();
    assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb1).state());
    assertTrue("Inactive load balancer not removed", tester.loadBalancerService().instances().containsKey(lb1));

    // Expirer removes load balancers once expiration time passes
    tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1)));
    expirer.maintain();
    assertFalse("Inactive load balancer removed", tester.loadBalancerService().instances().containsKey(lb1));

    // Active load balancer is left alone
    assertSame(LoadBalancer.State.active, loadBalancers.get().get(lb2).state());
    assertTrue("Active load balancer is not removed", tester.loadBalancerService().instances().containsKey(lb2));

    // A single cluster is removed
    deployApplication(app2, cluster1);
    expirer.maintain();
    assertSame(LoadBalancer.State.inactive, loadBalancers.get().get(lb3).state());

    // Expirer defers removal while nodes are still allocated to cluster
    expirer.maintain();
    assertEquals(2, tester.loadBalancerService().instances().size());
    dirtyNodesOf(app2, cluster2);

    // Expirer removes load balancer for removed cluster
    tester.clock().advance(Duration.ofHours(1).plus(Duration.ofSeconds(1)));
    expirer.maintain();
    assertFalse("Inactive load balancer removed", tester.loadBalancerService().instances().containsKey(lb3));
}
 
Example 19
Source File: TCKDuration.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_days_min() {
    Duration test = Duration.ofDays(Long.MIN_VALUE / 86400);
    assertEquals(test.getSeconds(), (Long.MIN_VALUE / 86400) * 86400);
    assertEquals(test.getNano(), 0);
}
 
Example 20
Source File: SystemUpgraderTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
private SystemUpgrader systemUpgrader(UpgradePolicy upgradePolicy) {
    tester.zoneRegistry().setUpgradePolicy(upgradePolicy);
    return new SystemUpgrader(tester.controller(), Duration.ofDays(1)
    );
}