/** * Creates an instance representing the value of {@code seconds} and {@code nanos} since January * 1, 1970, 00:00:00 UTC. * * @param seconds seconds since January 1, 1970, 00:00:00 UTC. A negative value is the number of * seconds before January 1, 1970, 00:00:00 UTC. * @param nanos the fractional seconds component, in the range 0..999999999. * @throws IllegalArgumentException if the timestamp is outside the representable range */ public static Timestamp ofTimeSecondsAndNanos(long seconds, int nanos) { checkArgument( Timestamps.isValid(seconds, nanos), "timestamp out of range: %s, %s", seconds, nanos); return new Timestamp(seconds, nanos); }
/** * Creates an instance representing the value of {@code seconds} and {@code nanos} since January * 1, 1970, 00:00:00 UTC. * * @param seconds seconds since January 1, 1970, 00:00:00 UTC. A negative value is the number of * seconds before January 1, 1970, 00:00:00 UTC. * @param nanos the fractional seconds component, in the range 0..999999999. * @throws IllegalArgumentException if the timestamp is outside the representable range */ public static Timestamp ofTimeSecondsAndNanos(long seconds, int nanos) { checkArgument( Timestamps.isValid(seconds, nanos), "timestamp out of range: %s, %s", seconds, nanos); return new Timestamp(seconds, nanos); }
/** Throws an {@link IllegalArgumentException} if the given {@link Timestamp} is not valid. */ public static Timestamp checkValid(Timestamp timestamp) { long seconds = timestamp.getSeconds(); int nanos = timestamp.getNanos(); if (!isValid(seconds, nanos)) { throw new IllegalArgumentException(String.format( "Timestamp is not valid. See proto definition for valid values. " + "Seconds (%s) must be in range [-62,135,596,800, +253,402,300,799]. " + "Nanos (%s) must be in range [0, +999,999,999].", seconds, nanos)); } return timestamp; }
/** * Returns true if the given {@link Timestamp} is valid. The {@code seconds} value must be in the * range [-62,135,596,800, +253,402,300,799] (i.e., between 0001-01-01T00:00:00Z and * 9999-12-31T23:59:59Z). The {@code nanos} value must be in the range [0, +999,999,999]. * * <p><b>Note:</b> Negative second values with fractional seconds must still have non-negative * nanos values that count forward in time. */ public static boolean isValid(Timestamp timestamp) { return isValid(timestamp.getSeconds(), timestamp.getNanos()); }