Java Code Examples for java.time.temporal.Temporal#until()

The following examples show how to use java.time.temporal.Temporal#until() . 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: Duration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 2
Source File: Duration.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
  return temporal1Inclusive.until(temporal2Exclusive, this);
}
 
Example 3
Source File: Duration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 4
Source File: Duration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 5
Source File: Duration.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 6
Source File: Duration.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 7
Source File: Duration.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 8
Source File: Duration.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 9
Source File: Duration.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 10
Source File: TimeUnit.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
public long between( Temporal temporal1Inclusive, Temporal temporal2Exclusive )
{
  return temporal1Inclusive.until( temporal2Exclusive, this );
}
 
Example 11
Source File: Duration.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 12
Source File: Duration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 13
Source File: TestJDBC42JavaTimeConversions.java    From jaybird with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
    return temporal1Inclusive.until(temporal2Exclusive, this);
}
 
Example 14
Source File: Duration.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 15
Source File: Duration.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 16
Source File: Duration.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 17
Source File: Duration.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 18
Source File: Duration.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 19
Source File: Duration.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}
 
Example 20
Source File: Duration.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtains a {@code Duration} representing the duration between two temporal objects.
 * <p>
 * This calculates the duration between two temporal objects. If the objects
 * are of different types, then the duration is calculated based on the type
 * of the first object. For example, if the first argument is a {@code LocalTime}
 * then the second argument is converted to a {@code LocalTime}.
 * <p>
 * The specified temporal objects must support the {@link ChronoUnit#SECONDS SECONDS} unit.
 * For full accuracy, either the {@link ChronoUnit#NANOS NANOS} unit or the
 * {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} field should be supported.
 * <p>
 * The result of this method can be a negative period if the end is before the start.
 * To guarantee to obtain a positive duration call {@link #abs()} on the result.
 *
 * @param startInclusive  the start instant, inclusive, not null
 * @param endExclusive  the end instant, exclusive, not null
 * @return a {@code Duration}, not null
 * @throws DateTimeException if the seconds between the temporals cannot be obtained
 * @throws ArithmeticException if the calculation exceeds the capacity of {@code Duration}
 */
public static Duration between(Temporal startInclusive, Temporal endExclusive) {
    try {
        return ofNanos(startInclusive.until(endExclusive, NANOS));
    } catch (DateTimeException | ArithmeticException ex) {
        long secs = startInclusive.until(endExclusive, SECONDS);
        long nanos;
        try {
            nanos = endExclusive.getLong(NANO_OF_SECOND) - startInclusive.getLong(NANO_OF_SECOND);
            if (secs > 0 && nanos < 0) {
                secs++;
            } else if (secs < 0 && nanos > 0) {
                secs--;
            }
        } catch (DateTimeException ex2) {
            nanos = 0;
        }
        return ofSeconds(secs, nanos);
    }
}