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

The following examples show how to use java.time.Duration#plus() . These examples are extracted from open source projects. 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 Project: LuckPerms   File: DurationParser.java    License: 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
@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 3
Source Project: hottub   File: TCKDuration.java    License: 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 4
Source Project: j2objc   File: TCKDuration.java    License: 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 5
@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 6
@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
@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 8
@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 9
@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
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 11
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 12
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 13
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 14
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 15
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 16
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooSmall() {
   Duration t = Duration.ofSeconds(Long.MIN_VALUE);
   t.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 17
Source Project: j2objc   File: TCKDuration.java    License: 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 18
@Test(expectedExceptions=ArithmeticException.class)
public void plusOverflowTooBig() {
   Duration t = Duration.ofSeconds(Long.MAX_VALUE, 999999999);
   t.plus(Duration.ofSeconds(0, 1));
}
 
Example 19
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
Example 20
Source Project: j2objc   File: TCKDuration.java    License: 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));
}