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

The following examples show how to use java.time.Duration#plusHours() . 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: j2objc   File: TCKDuration.java    License: Apache License 2.0 5 votes vote down vote up
@Test()
@UseDataProvider("provider_plusHours_long")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 2
Source Project: Java-Coding-Problems   File: Main.java    License: MIT License 4 votes vote down vote up
public static void main(String[] args) {

        Duration t = Duration.ofSeconds(65);
        System.out.println("Seconds part: " + t.toSecondsPart());
        
        Duration fromHours = Duration.ofHours(10);
        System.out.println("\nDuration from hours: " + fromHours);

        Duration fromMinutes = Duration.of(3, ChronoUnit.MINUTES);
        System.out.println("Duration from minutes: " + fromMinutes);
        
        LocalDateTime localDateTime = LocalDateTime.of(2018, 3, 12, 4, 14, 20, 670);
        Duration fromLocalDateTime = Duration.ofMinutes(localDateTime.getMinute());
        System.out.println("Duration from minutes of a LocalDateTime: " + fromLocalDateTime);
        
        LocalTime localTime = LocalTime.of(4, 14, 20, 670);
        Duration fromLocalTime = Duration.ofNanos(localTime.getNano());
        System.out.println("Duration from nanos of a LocalTime: " + fromLocalTime);

        Duration durationFromString = Duration.parse("P2DT3H4M");
        System.out.println("Duration from String: " + durationFromString);

        Instant startInstant = Instant.parse("2015-11-03T12:11:30.00Z");
        Instant endInstant = Instant.parse("2016-12-06T15:17:10.00Z");
        Duration durationBetweenInstant = Duration.between(startInstant, endInstant);
        System.out.println("\nDuration between two Instant: " + durationBetweenInstant);
        System.out.println("Between " + startInstant + " and "
                + endInstant + " there are " + durationBetweenInstant.getSeconds() + " second(s)");
        System.out.println("Expressed in days and hours: "
                + durationBetweenInstant.toDays() + "d:" + durationBetweenInstant.toHoursPart() + "h");
        System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenInstant));

        LocalDateTime startLocalDateTime = LocalDateTime.of(2018, 3, 12, 4, 14, 20, 670);
        LocalDateTime endLocalDateTime = LocalDateTime.of(2019, 7, 20, 6, 10, 10, 720);
        Duration durationBetweenLDT = Duration.between(startLocalDateTime, endLocalDateTime);
        System.out.println("\nDuration between two LocalDateTime: " + durationBetweenLDT);
        System.out.println("Between " + startLocalDateTime + " and "
                + endLocalDateTime + " there are " + durationBetweenLDT.getSeconds() + " second(s)");
        System.out.println("Expressed in days and hours: "
                + durationBetweenLDT.toDays() + "d:" + durationBetweenLDT.toHoursPart() + "h");
        System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenLDT));

        LocalTime startLocalTime = LocalTime.of(4, 14, 20, 670);
        LocalTime endLocalTime = LocalTime.of(6, 10, 10, 720);
        Duration durationBetweenLT = Duration.between(startLocalTime, endLocalTime);
        System.out.println("\nDuration between two LocalTime: " + durationBetweenLT);
        System.out.println("Between " + startLocalTime + " and "
                + endLocalTime + " there are " + durationBetweenLT.getSeconds() + " second(s)");
        System.out.println("Expressed in hours and minutes: "
                + durationBetweenLT.toMinutes() + "m:" + durationBetweenLT.toSecondsPart() + "s");
        System.out.println("Expressed as d:h:m:s:n: " + durationToDHMSN(durationBetweenLT));

        System.out.println(startLocalTime + " is after "
                + endLocalTime + " ? " + durationBetweenLT.isNegative());

        Duration durationBetweenPlus5Hours = durationBetweenLT.plusHours(5);
        System.out.println("\nDuration between plus 5 hours: " + durationBetweenPlus5Hours);
        
        Duration d1 = Duration.ofMinutes(20);
        Duration d2 = Duration.ofHours(2);
        Duration d1d2 = d1.plus(d2);
        System.out.println(d1 + "+" + d2 + "=" + d1d2);
    }
 
Example 3
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 4
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 5
Source Project: j2objc   File: TCKDuration.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected = ArithmeticException.class)
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 6
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 7
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 8
Source Project: hottub   File: TCKDuration.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 9
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 10
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 11
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 12
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 13
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 14
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 15
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 16
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 17
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 18
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 19
@Test(dataProvider="PlusHours")
public void plusHours_long(long hours, long amount, long expectedHours) {
    Duration t = Duration.ofHours(hours);
    t = t.plusHours(amount);
    assertEquals(t.toHours(), expectedHours);
}
 
Example 20
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}