📄 date.java
字号:
* * @param anotherDate the <code>Date</code> to be compared. * @return the value <code>0</code> if the argument Date is equal to * this Date; a value less than <code>0</code> if this Date * is before the Date argument; and a value greater than * <code>0</code> if this Date is after the Date argument. * @since 1.2 */ public int compareTo(Date anotherDate) { long thisTime = this.getTime(); long anotherTime = anotherDate.getTime(); return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1)); } /** * Compares this Date to another Object. If the Object is a Date, * this function behaves like <code>compareTo(Date)</code>. Otherwise, * it throws a <code>ClassCastException</code> (as Dates are comparable * only to other Dates). * * @param o the <code>Object</code> to be compared. * @return the value <code>0</code> if the argument is a Date * equal to this Date; a value less than <code>0</code> if the * argument is a Date after this Date; and a value greater than * <code>0</code> if the argument is a Date before this Date. * @exception ClassCastException if the argument is not a * <code>Date</code>. * @see java.lang.Comparable * @since 1.2 */ public int compareTo(Object o) { return compareTo((Date)o); } /** * Returns a hash code value for this object. The result is the * exclusive OR of the two halves of the primitive <tt>long</tt> * value returned by the {@link Date#getTime} * method. That is, the hash code is the value of the expression: * <blockquote><pre> * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote> * * @return a hash code value for this object. */ public int hashCode() { long ht = getTime(); return (int) ht ^ (int) (ht >> 32); } /** * Converts this <code>Date</code> object to a <code>String</code> * of the form: * <blockquote><pre> * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote> * where:<ul> * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed, * Thu, Fri, Sat</tt>). * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, * Jul, Aug, Sep, Oct, Nov, Dec</tt>). * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through * <tt>31</tt>), as two decimal digits. * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through * <tt>23</tt>), as two decimal digits. * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through * <tt>59</tt>), as two decimal digits. * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through * <tt>61</tt>, as two decimal digits. * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving * time). Standard time zone abbreviations include those * recognized by the method <tt>parse</tt>. If time zone * information is not available, then <tt>zzz</tt> is empty - * that is, it consists of no characters at all. * <li><tt>yyyy</tt> is the year, as four decimal digits. * </ul> * * @return a string representation of this date. * @see java.util.Date#toLocaleString() * @see java.util.Date#toGMTString() */ public String toString() { DateFormat formatter = null; if (simpleFormatter != null) { formatter = (DateFormat)simpleFormatter.get(); } if (formatter == null) { /* No cache yet, or cached formatter GC'd */ formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); simpleFormatter = new SoftReference(formatter); } synchronized (formatter) { formatter.setTimeZone(TimeZone.getDefault()); return formatter.format(this); } } /** * Creates a string representation of this <tt>Date</tt> object in an * implementation-dependent form. The intent is that the form should * be familiar to the user of the Java application, wherever it may * happen to be running. The intent is comparable to that of the * "<code>%c</code>" format supported by the <code>strftime()</code> * function of ISO C. * * @return a string representation of this date, using the locale * conventions. * @see java.text.DateFormat * @see java.util.Date#toString() * @see java.util.Date#toGMTString() * @deprecated As of JDK version 1.1, * replaced by <code>DateFormat.format(Date date)</code>. */ public String toLocaleString() { DateFormat formatter = DateFormat.getDateTimeInstance(); return formatter.format(this); } /** * Creates a string representation of this <tt>Date</tt> object of * the form: * <blockquote<pre> * d mon yyyy hh:mm:ss GMT</pre></blockquote> * where:<ul> * <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>), * as one or two decimal digits. * <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul, * Aug, Sep, Oct, Nov, Dec</tt>). * <li><i>yyyy</i> is the year, as four decimal digits. * <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>), * as two decimal digits. * <li><i>mm</i> is the minute within the hour (<tt>00</tt> through * <tt>59</tt>), as two decimal digits. * <li><i>ss</i> is the second within the minute (<tt>00</tt> through * <tt>61</tt>), as two decimal digits. * <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate * Greenwich Mean Time. * </ul><p> * The result does not depend on the local time zone. * * @return a string representation of this date, using the Internet GMT * conventions. * @see java.text.DateFormat * @see java.util.Date#toString() * @see java.util.Date#toLocaleString() * @deprecated As of JDK version 1.1, * replaced by <code>DateFormat.format(Date date)</code>, using a * GMT <code>TimeZone</code>. */ public String toGMTString() { DateFormat formatter = null; if (gmtFormatter != null) { formatter = (DateFormat)gmtFormatter.get(); } if (formatter == null) { /* No cache yet, or cached formatter GC'd */ formatter = new SimpleDateFormat("d MMM yyyy HH:mm:ss 'GMT'", Locale.US); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); gmtFormatter = new SoftReference(formatter); } return formatter.format(this); } /** * Returns the offset, measured in minutes, for the local time zone * relative to UTC that is appropriate for the time represented by * this <tt>Date</tt> object. * <p> * For example, in Massachusetts, five time zones west of Greenwich: * <blockquote><pre> * new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote> * because on February 14, 1996, standard time (Eastern Standard Time) * is in use, which is offset five hours from UTC; but: * <blockquote><pre> * new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote> * because on June 1, 1996, daylight saving time (Eastern Daylight Time) * is in use, which is offset only four hours from UTC.<p> * This method produces the same result as if it computed: * <blockquote><pre> * (this.getTime() - UTC(this.getYear(), * this.getMonth(), * this.getDate(), * this.getHours(), * this.getMinutes(), * this.getSeconds())) / (60 * 1000) * </pre></blockquote> * * @return the time-zone offset, in minutes, for the current time zone. * @see java.util.Calendar#ZONE_OFFSET * @see java.util.Calendar#DST_OFFSET * @see java.util.TimeZone#getDefault * @deprecated As of JDK version 1.1, * replaced by <code>-(Calendar.get(Calendar.ZONE_OFFSET) + * Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)</code>. */ public int getTimezoneOffset() { int offset; if (cal == null) { if (staticCal == null) makeStaticCalendars(); synchronized (staticCal) { staticCal.setTimeZone(TimeZone.getDefault()); staticCal.setTimeInMillis(getTime()); offset = staticCal.get(Calendar.ZONE_OFFSET) + staticCal.get(Calendar.DST_OFFSET); } } else { TimeZone defaultZone = TimeZone.getDefault(); if (!defaultZone.equals(cal.getTimeZone())) { long ms = cal.getTimeInMillis(); cal.setTimeZone(TimeZone.getDefault()); cal.setTimeInMillis(ms); } offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); } return -(offset / 1000 / 60); // convert to minutes } /** * Save the state of this object to a stream (i.e., serialize it). * * @serialData The value returned by <code>getTime()</code> * is emitted (long). This represents the offset from * January 1, 1970, 00:00:00 GMT in milliseconds. */ private void writeObject(ObjectOutputStream s) throws IOException { s.writeLong(getTimeImpl()); } /** * Reconstitute this object from a stream (i.e., deserialize it). */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { fastTime = s.readLong(); // we expect to have cal == null here } /** * Return a field for this date by looking it up in a Calendar object. * * @return the field value * @see java.util.Calendar * @param field the field to return */ private final int getField(int field) { if (cal == null) { if (staticCal == null) makeStaticCalendars(); synchronized (staticCal) { staticCal.setTimeZone(TimeZone.getDefault()); staticCal.setTimeInMillis(fastTime); return staticCal.get(field); } } else { TimeZone defaultZone = TimeZone.getDefault(); if (!defaultZone.equals(cal.getTimeZone())) { long ms = cal.getTimeInMillis(); cal.setTimeZone(TimeZone.getDefault()); cal.setTimeInMillis(ms); } return cal.get(field); } } /** * Set a field for this day. * * @param field the field to set * @param value the value to set it to * @see java.util.Calendar */ private final void setField(int field, int value) { if (cal == null) { cal = new GregorianCalendar(); cal.setTimeInMillis(fastTime); } cal.set(field, value); } private synchronized static void makeStaticCalendars() { if (staticCal == null) { GregorianCalendar calendar = new GregorianCalendar(); utcCal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); defaultCenturyStart = calendar.get(Calendar.YEAR) - 80; staticCal = calendar; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -