--- old/src/java.base/share/classes/java/time/Duration.java 2016-02-01 07:42:57.660791509 +0300 +++ new/src/java.base/share/classes/java/time/Duration.java 2016-02-01 07:42:57.464791509 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1214,8 +1214,16 @@ * @throws ArithmeticException if numeric overflow occurs */ public long toMillis() { - long millis = Math.multiplyExact(seconds, 1000); - millis = Math.addExact(millis, nanos / 1000_000); + long tempSeconds = seconds; + long tempNanos = nanos; + if (tempSeconds < 0) { + // change the seconds and nano value to + // handle Long.MIN_VALUE case + tempSeconds = tempSeconds + 1; + tempNanos = tempNanos - 1000_000_000L; + } + long millis = Math.multiplyExact(tempSeconds, 1000); + millis = Math.addExact(millis, tempNanos / 1000_000); return millis; } @@ -1229,8 +1237,16 @@ * @throws ArithmeticException if numeric overflow occurs */ public long toNanos() { - long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND); - totalNanos = Math.addExact(totalNanos, nanos); + long tempSeconds = seconds; + long tempNanos = nanos; + if (tempSeconds < 0) { + // change the seconds and nano value to + // handle Long.MIN_VALUE case + tempSeconds = tempSeconds + 1; + tempNanos = tempNanos - 1000_000_000L; + } + long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND); + totalNanos = Math.addExact(totalNanos, tempNanos); return totalNanos; }