/** * Obtains an instance of {@code Instant} using seconds from the * epoch of 1970-01-01T00:00:00Z. * <p> * The nanosecond field is set to zero. * * @param epochSecond the number of seconds from 1970-01-01T00:00:00Z * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant */ public static Instant ofEpochSecond(long epochSecond) { return create(epochSecond, 0); }
/** * Obtains an instance of {@code Instant} using seconds from the * epoch of 1970-01-01T00:00:00Z. * <p> * The nanosecond field is set to zero. * * @param epochSecond the number of seconds from 1970-01-01T00:00:00Z * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant */ public static Instant ofEpochSecond(long epochSecond) { return create(epochSecond, 0); }
case MILLI_OF_SECOND: { int nval = (int) newValue * NANOS_PER_MILLI; return (nval != nanos ? create(seconds, nval) : this); return (nval != nanos ? create(seconds, nval) : this); case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this); case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
case MILLI_OF_SECOND: { int nval = (int) newValue * NANOS_PER_MILLI; return (nval != nanos ? create(seconds, nval) : this); return (nval != nanos ? create(seconds, nval) : this); case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this); case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
/** * Obtains an instance of {@code Instant} using milliseconds from the * epoch of 1970-01-01T00:00:00Z. * <p> * The seconds and nanoseconds are extracted from the specified milliseconds. * * @param epochMilli the number of milliseconds from 1970-01-01T00:00:00Z * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant */ public static Instant ofEpochMilli(long epochMilli) { long secs = Jdk8Methods.floorDiv(epochMilli, 1000); int mos = Jdk8Methods.floorMod(epochMilli, 1000); return create(secs, mos * NANOS_PER_MILLI); }
/** * Obtains an instance of {@code Instant} using milliseconds from the * epoch of 1970-01-01T00:00:00Z. * <p> * The seconds and nanoseconds are extracted from the specified milliseconds. * * @param epochMilli the number of milliseconds from 1970-01-01T00:00:00Z * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant */ public static Instant ofEpochMilli(long epochMilli) { long secs = Jdk8Methods.floorDiv(epochMilli, 1000); int mos = Jdk8Methods.floorMod(epochMilli, 1000); return create(secs, mos * NANOS_PER_MILLI); }
/** * Obtains an instance of {@code Instant} using seconds from the * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second. * <p> * This method allows an arbitrary number of nanoseconds to be passed in. * The factory will alter the values of the second and nanosecond in order * to ensure that the stored nanosecond is in the range 0 to 999,999,999. * For example, the following will result in the exactly the same instant: * <pre> * Instant.ofSeconds(3, 1); * Instant.ofSeconds(4, -999_999_999); * Instant.ofSeconds(2, 1000_000_001); * </pre> * * @param epochSecond the number of seconds from 1970-01-01T00:00:00Z * @param nanoAdjustment the nanosecond adjustment to the number of seconds, positive or negative * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) { long secs = Jdk8Methods.safeAdd(epochSecond, Jdk8Methods.floorDiv(nanoAdjustment, NANOS_PER_SECOND)); int nos = Jdk8Methods.floorMod(nanoAdjustment, NANOS_PER_SECOND); return create(secs, nos); }
/** * Obtains an instance of {@code Instant} using seconds from the * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second. * <p> * This method allows an arbitrary number of nanoseconds to be passed in. * The factory will alter the values of the second and nanosecond in order * to ensure that the stored nanosecond is in the range 0 to 999,999,999. * For example, the following will result in the exactly the same instant: * <pre> * Instant.ofSeconds(3, 1); * Instant.ofSeconds(4, -999_999_999); * Instant.ofSeconds(2, 1000_000_001); * </pre> * * @param epochSecond the number of seconds from 1970-01-01T00:00:00Z * @param nanoAdjustment the nanosecond adjustment to the number of seconds, positive or negative * @return an instant, not null * @throws DateTimeException if the instant exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) { long secs = Jdk8Methods.safeAdd(epochSecond, Jdk8Methods.floorDiv(nanoAdjustment, NANOS_PER_SECOND)); int nos = Jdk8Methods.floorMod(nanoAdjustment, NANOS_PER_SECOND); return create(secs, nos); }