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

The following examples show how to use java.time.Duration#plus() . 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: DurationParser.java    From LuckPerms with MIT License 6 votes vote down vote up
public static Duration parseDuration(String input) throws IllegalArgumentException {
    Matcher matcher = PATTERN.matcher(input);

    while (matcher.find()) {
        if (matcher.group() == null || matcher.group().isEmpty()) {
            continue;
        }

        Duration duration = Duration.ZERO;
        for (int i = 0; i < UNITS.length; i++) {
            ChronoUnit unit = UNITS[i];
            int g = i + 1;

            if (matcher.group(g) != null && !matcher.group(g).isEmpty()) {
                int n = Integer.parseInt(matcher.group(g));
                if (n > 0) {
                    duration = duration.plus(unit.getDuration().multipliedBy(n));
                }
            }
        }

        return duration;
    }

    throw new IllegalArgumentException("unable to parse duration: " + input);
}
 
Example 2
Source File: TCKDuration.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_seconds() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, SECONDS);
    assertEquals(2, t.getSeconds());
    assertEquals(0, t.getNano());
 }
 
Example 3
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_millis() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, MILLIS);
    assertEquals(1, t.getSeconds());
    assertEquals(1000000, t.getNano());
 }
 
Example 4
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_nanos() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, NANOS);
    assertEquals(1, t.getSeconds());
    assertEquals(1, t.getNano());
 }
 
Example 5
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_millis() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, MILLIS);
    assertEquals(1, t.getSeconds());
    assertEquals(1000000, t.getNano());
 }
 
Example 6
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_nanos() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, NANOS);
    assertEquals(1, t.getSeconds());
    assertEquals(1, t.getNano());
 }
 
Example 7
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_seconds() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, SECONDS);
    assertEquals(2, t.getSeconds());
    assertEquals(0, t.getNano());
 }
 
Example 8
Source File: TCKDuration.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_micros() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, MICROS);
    assertEquals(1, t.getSeconds());
    assertEquals(1000, t.getNano());
 }
 
Example 9
Source File: TCKDuration.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void plus_longTemporalUnit_millis() {
    Duration t = Duration.ofSeconds(1);
    t = t.plus(1, MILLIS);
    assertEquals(1, t.getSeconds());
    assertEquals(1000000, t.getNano());
 }
 
Example 10
Source File: TCKDuration.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 11
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 plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 12
Source File: TCKDuration.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 13
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
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 plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 15
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 plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 16
Source File: TCKDuration.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 17
Source File: TCKDuration.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 18
Source File: TCKDuration.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 19
Source File: TCKDuration.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 20
Source File: TCKDuration.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=ArithmeticException.class)
public void plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}