893
894 /**
895 * Sets this {@code Date} object to represent a point in time that is
896 * {@code time} milliseconds after January 1, 1970 00:00:00 GMT.
897 *
898 * @param time the number of milliseconds.
899 */
900 public void setTime(long time) {
901 fastTime = time;
902 cdate = null;
903 }
904
905 /**
906 * Tests if this date is before the specified date.
907 *
908 * @param when a date.
909 * @return {@code true} if and only if the instant of time
910 * represented by this {@code Date} object is strictly
911 * earlier than the instant represented by {@code when};
912 * {@code false} otherwise.
913 * @exception NullPointerException if {@code when} is null.
914 */
915 public boolean before(Date when) {
916 return getMillisOf(this) < getMillisOf(when);
917 }
918
919 /**
920 * Tests if this date is after the specified date.
921 *
922 * @param when a date.
923 * @return {@code true} if and only if the instant represented
924 * by this {@code Date} object is strictly later than the
925 * instant represented by {@code when};
926 * {@code false} otherwise.
927 * @exception NullPointerException if {@code when} is null.
928 */
929 public boolean after(Date when) {
930 return getMillisOf(this) > getMillisOf(when);
931 }
932
933 /**
934 * Compares two dates for equality.
935 * The result is {@code true} if and only if the argument is
936 * not {@code null} and is a {@code Date} object that
937 * represents the same point in time, to the millisecond, as this object.
938 * <p>
939 * Thus, two {@code Date} objects are equal if and only if the
940 * {@code getTime} method returns the same {@code long}
941 * value for both.
942 *
943 * @param obj the object to compare with.
944 * @return {@code true} if the objects are the same;
945 * {@code false} otherwise.
946 * @see java.util.Date#getTime()
947 */
956 static final long getMillisOf(Date date) {
957 if (date.getClass() != Date.class) {
958 return date.getTime();
959 }
960 if (date.cdate == null || date.cdate.isNormalized()) {
961 return date.fastTime;
962 }
963 BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
964 return gcal.getTime(d);
965 }
966
967 /**
968 * Compares two Dates for ordering.
969 *
970 * @param anotherDate the {@code Date} to be compared.
971 * @return the value {@code 0} if the argument Date is equal to
972 * this Date; a value less than {@code 0} if this Date
973 * is before the Date argument; and a value greater than
974 * {@code 0} if this Date is after the Date argument.
975 * @since 1.2
976 * @exception NullPointerException if {@code anotherDate} is null.
977 */
978 public int compareTo(Date anotherDate) {
979 long thisTime = getMillisOf(this);
980 long anotherTime = getMillisOf(anotherDate);
981 return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
982 }
983
984 /**
985 * Returns a hash code value for this object. The result is the
986 * exclusive OR of the two halves of the primitive {@code long}
987 * value returned by the {@link Date#getTime}
988 * method. That is, the hash code is the value of the expression:
989 * <blockquote><pre>{@code
990 * (int)(this.getTime()^(this.getTime() >>> 32))
991 * }</pre></blockquote>
992 *
993 * @return a hash code value for this object.
994 */
995 public int hashCode() {
996 long ht = this.getTime();
1336 {
1337 s.defaultReadObject();
1338 fastTime = s.readLong();
1339 }
1340
1341 /**
1342 * Obtains an instance of {@code Date} from an {@code Instant} object.
1343 * <p>
1344 * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
1345 * uses a precision of milliseconds. The conversion will truncate any
1346 * excess precision information as though the amount in nanoseconds was
1347 * subject to integer division by one million.
1348 * <p>
1349 * {@code Instant} can store points on the time-line further in the future
1350 * and further in the past than {@code Date}. In this scenario, this method
1351 * will throw an exception.
1352 *
1353 * @param instant the instant to convert
1354 * @return a {@code Date} representing the same point on the time-line as
1355 * the provided instant
1356 * @exception NullPointerException if {@code instant} is null.
1357 * @exception IllegalArgumentException if the instant is too large to
1358 * represent as a {@code Date}
1359 * @since 1.8
1360 */
1361 public static Date from(Instant instant) {
1362 try {
1363 return new Date(instant.toEpochMilli());
1364 } catch (ArithmeticException ex) {
1365 throw new IllegalArgumentException(ex);
1366 }
1367 }
1368
1369 /**
1370 * Converts this {@code Date} object to an {@code Instant}.
1371 * <p>
1372 * The conversion creates an {@code Instant} that represents the same
1373 * point on the time-line as this {@code Date}.
1374 *
1375 * @return an instant representing the same point on the time-line as
1376 * this {@code Date} object
1377 * @since 1.8
|
893
894 /**
895 * Sets this {@code Date} object to represent a point in time that is
896 * {@code time} milliseconds after January 1, 1970 00:00:00 GMT.
897 *
898 * @param time the number of milliseconds.
899 */
900 public void setTime(long time) {
901 fastTime = time;
902 cdate = null;
903 }
904
905 /**
906 * Tests if this date is before the specified date.
907 *
908 * @param when a date.
909 * @return {@code true} if and only if the instant of time
910 * represented by this {@code Date} object is strictly
911 * earlier than the instant represented by {@code when};
912 * {@code false} otherwise.
913 * @throws NullPointerException if {@code when} is null.
914 */
915 public boolean before(Date when) {
916 return getMillisOf(this) < getMillisOf(when);
917 }
918
919 /**
920 * Tests if this date is after the specified date.
921 *
922 * @param when a date.
923 * @return {@code true} if and only if the instant represented
924 * by this {@code Date} object is strictly later than the
925 * instant represented by {@code when};
926 * {@code false} otherwise.
927 * @throws NullPointerException if {@code when} is null.
928 */
929 public boolean after(Date when) {
930 return getMillisOf(this) > getMillisOf(when);
931 }
932
933 /**
934 * Compares two dates for equality.
935 * The result is {@code true} if and only if the argument is
936 * not {@code null} and is a {@code Date} object that
937 * represents the same point in time, to the millisecond, as this object.
938 * <p>
939 * Thus, two {@code Date} objects are equal if and only if the
940 * {@code getTime} method returns the same {@code long}
941 * value for both.
942 *
943 * @param obj the object to compare with.
944 * @return {@code true} if the objects are the same;
945 * {@code false} otherwise.
946 * @see java.util.Date#getTime()
947 */
956 static final long getMillisOf(Date date) {
957 if (date.getClass() != Date.class) {
958 return date.getTime();
959 }
960 if (date.cdate == null || date.cdate.isNormalized()) {
961 return date.fastTime;
962 }
963 BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
964 return gcal.getTime(d);
965 }
966
967 /**
968 * Compares two Dates for ordering.
969 *
970 * @param anotherDate the {@code Date} to be compared.
971 * @return the value {@code 0} if the argument Date is equal to
972 * this Date; a value less than {@code 0} if this Date
973 * is before the Date argument; and a value greater than
974 * {@code 0} if this Date is after the Date argument.
975 * @since 1.2
976 * @throws NullPointerException if {@code anotherDate} is null.
977 */
978 public int compareTo(Date anotherDate) {
979 long thisTime = getMillisOf(this);
980 long anotherTime = getMillisOf(anotherDate);
981 return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
982 }
983
984 /**
985 * Returns a hash code value for this object. The result is the
986 * exclusive OR of the two halves of the primitive {@code long}
987 * value returned by the {@link Date#getTime}
988 * method. That is, the hash code is the value of the expression:
989 * <blockquote><pre>{@code
990 * (int)(this.getTime()^(this.getTime() >>> 32))
991 * }</pre></blockquote>
992 *
993 * @return a hash code value for this object.
994 */
995 public int hashCode() {
996 long ht = this.getTime();
1336 {
1337 s.defaultReadObject();
1338 fastTime = s.readLong();
1339 }
1340
1341 /**
1342 * Obtains an instance of {@code Date} from an {@code Instant} object.
1343 * <p>
1344 * {@code Instant} uses a precision of nanoseconds, whereas {@code Date}
1345 * uses a precision of milliseconds. The conversion will truncate any
1346 * excess precision information as though the amount in nanoseconds was
1347 * subject to integer division by one million.
1348 * <p>
1349 * {@code Instant} can store points on the time-line further in the future
1350 * and further in the past than {@code Date}. In this scenario, this method
1351 * will throw an exception.
1352 *
1353 * @param instant the instant to convert
1354 * @return a {@code Date} representing the same point on the time-line as
1355 * the provided instant
1356 * @throws NullPointerException if {@code instant} is null.
1357 * @throws IllegalArgumentException if the instant is too large to
1358 * represent as a {@code Date}
1359 * @since 1.8
1360 */
1361 public static Date from(Instant instant) {
1362 try {
1363 return new Date(instant.toEpochMilli());
1364 } catch (ArithmeticException ex) {
1365 throw new IllegalArgumentException(ex);
1366 }
1367 }
1368
1369 /**
1370 * Converts this {@code Date} object to an {@code Instant}.
1371 * <p>
1372 * The conversion creates an {@code Instant} that represents the same
1373 * point on the time-line as this {@code Date}.
1374 *
1375 * @return an instant representing the same point on the time-line as
1376 * this {@code Date} object
1377 * @since 1.8
|