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

The following examples show how to use java.time.Duration#plusHours() . 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: TCKDuration.java    From j2objc with 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 File: TCKDuration.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@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 3
Source File: TCKDuration.java    From jdk8u_jdk with 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 4
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@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 5
Source File: TCKDuration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 6
Source File: TCKDuration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKDuration.java    From openjdk-8-source with 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 8
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 plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 9
Source File: TCKDuration.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 10
Source File: TCKDuration.java    From openjdk-jdk8u with 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 11
Source File: Main.java    From Java-Coding-Problems with 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 12
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 plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 13
Source File: TCKDuration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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 14
Source File: TCKDuration.java    From jdk8u-jdk with 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 15
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 plusHours_long_overflowTooSmall() {
    Duration t = Duration.ofHours(-1);
    t.plusHours(Long.MIN_VALUE/3600);
}
 
Example 16
Source File: TCKDuration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {ArithmeticException.class})
public void plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 17
Source File: TCKDuration.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKDuration.java    From j2objc with 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 19
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 plusHours_long_overflowTooBig() {
    Duration t = Duration.ofHours(1);
    t.plusHours(Long.MAX_VALUE/3600);
}
 
Example 20
Source File: TCKDuration.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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);
}