📄 date.java
字号:
/* java.util.Date Copyright (C) 1998, 1999, 2000, 2001, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.util;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.text.DateFormat;import java.text.SimpleDateFormat;/** * <p> * This class represents a specific time in milliseconds since the epoch. * The epoch is 1970, January 1 00:00:00.0000 UTC. * </p> * <p> * <code>Date</code> is intended to reflect universal time coordinate (UTC), * but this depends on the underlying host environment. Most operating systems * don't handle the leap second, which occurs about once every year or * so. The leap second is added to the last minute of the day on either * the 30th of June or the 31st of December, creating a minute 61 seconds * in length. * </p> * <p> * The representations of the date fields are as follows: * <ul> * <li> * Years are specified as the difference between the year * and 1900. Thus, the final year used is equal to * 1900 + y, where y is the input value. * </li> * <li> * Months are represented using zero-based indexing, * making 0 January and 11 December. * </li> * <li> * Dates are represented with the usual values of * 1 through to 31. * </li> * <li> * Hours are represented in the twenty-four hour clock, * with integer values from 0 to 23. 12am is 0, and * 12pm is 12. * </li> * <li> * Minutes are again as usual, with values from 0 to 59. * </li> * <li> * Seconds are represented with the values 0 through to 61, * with 60 and 61 being leap seconds (as per the ISO C standard). * </li> * </ul> * </p> * <p> * Prior to JDK 1.1, this class was the sole class handling date and time * related functionality. However, this particular solution was not * amenable to internationalization. The new <code>Calendar</code> * class should now be used to handle dates and times, with <code>Date</code> * being used only for values in milliseconds since the epoch. The * <code>Calendar</code> class, and its concrete implementations, handle * the interpretation of these values into minutes, hours, days, months * and years. The formatting and parsing of dates is left to the * <code>DateFormat</code> class, which is able to handle the different * types of date format which occur in different locales. * </p> * * @see Calendar * @see GregorianCalendar * @see java.text.DateFormat * @author Jochen Hoenicke * @author Per Bothner (bothner@cygnus.com) * @author Andrew John Hughes (gnu_andrew@member.fsf.org) */public class Date implements Cloneable, Comparable, Serializable{ /** * This is the serialization UID for this class * for compatability with Sun's JDK. */ private static final long serialVersionUID = 7523967970034938905L; /** * The time in milliseconds since the epoch. */ private transient long time; /** * An array of week names used to map names to integer values. */ private static final String[] weekNames = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; /** * An array of month names used to map names to integer values. */ private static final String[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /** * Creates a new Date Object representing the current time. */ public Date() { time = System.currentTimeMillis(); } /** * Creates a new Date Object representing the given time. * * @param time the time in milliseconds since the epoch. */ public Date(long time) { this.time = time; } /** * Creates a new Date Object representing the given time. * * @deprecated use <code>new GregorianCalendar(year+1900, month, * day)</code> instead. * @param year the difference between the required year and 1900. * @param month the month as a value between 0 and 11. * @param day the day as a value between 0 and 31. */ public Date(int year, int month, int day) { this(year, month, day, 0, 0, 0); } /** * Creates a new Date Object representing the given time. * * @deprecated use <code>new GregorianCalendar(year+1900, month, * day, hour, min)</code> instead. * @param year the difference between the required year and 1900. * @param month the month as a value between 0 and 11. * @param day the day as a value between 0 and 31. * @param hour the hour as a value between 0 and 23, in 24-hour * clock notation. * @param min the minute as a value between 0 and 59. */ public Date(int year, int month, int day, int hour, int min) { this(year, month, day, hour, min, 0); } /** * Creates a new Date Object representing the given time. * * @deprecated use <code>new GregorianCalendar(year+1900, month, * day, hour, min, sec)</code> instead. * @param year the difference between the required year and 1900. * @param month the month as a value between 0 and 11. * @param day the day as a value between 0 and 31. * @param hour the hour as a value between 0 and 23, in 24-hour * clock notation. * @param min the minute as a value between 0 and 59. * @param sec the second as a value between 0 and 61 (with 60 * and 61 being leap seconds). */ public Date(int year, int month, int day, int hour, int min, int sec) { GregorianCalendar cal = new GregorianCalendar(year + 1900, month, day, hour, min, sec); time = cal.getTimeInMillis(); } /** * Creates a new Date from the given string representation. This * does the same as <code>new Date(Date.parse(s))</code> * @see #parse * @deprecated use <code>java.text.DateFormat.parse(s)</code> instead. */ public Date(String s) { time = parse(s); } /** * Returns a copy of this <code>Date</code> object. * * @return a copy, or null if the object couldn't be * cloned. * @see Object#clone() */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } } /** * Returns the number of milliseconds since the epoch * specified by the given arguments. The arguments are * interpreted relative to UTC rather than the local * time zone. * * @deprecated Use <code>Calendar</code> with a UTC * <code>TimeZone</code> instead. * @param year the difference between the required year and 1900. * @param month the month as a value between 0 and 11. * @param date the day as a value between 0 and 31. * @param hrs the hour as a value between 0 and 23, in 24-hour * clock notation. * @param min the minute as a value between 0 and 59. * @param sec the second as a value between 0 and 61 (with 60 * and 61 being leap seconds). * @return the time in milliseconds since the epoch. */ public static long UTC(int year, int month, int date, int hrs, int min, int sec) { GregorianCalendar cal = new GregorianCalendar(year + 1900, month, date, hrs, min, sec); cal.set(Calendar.ZONE_OFFSET, 0); cal.set(Calendar.DST_OFFSET, 0); return cal.getTimeInMillis(); } /** * Gets the time represented by this object. * * @return the time in milliseconds since the epoch. */ public long getTime() { return time; } /** * Returns the number of minutes offset used with UTC to give the time * represented by this object in the current time zone. The date information * from this object is also used to determine whether or not daylight savings * time is in effect. For example, the offset for the UK would be 0 if the * month of the date object was January, and 1 if the month was August. * * @deprecated use * <code>Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET)</code> * instead. * @return The time zone offset in minutes of the local time zone * relative to UTC. The time represented by this object is used to * determine if we should use daylight savings. */ public int getTimezoneOffset() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return - (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000); } /** * Sets the time which this object should represent. * * @param time the time in milliseconds since the epoch. */ public void setTime(long time) { this.time = time; } /** * Tests if this date is after the specified date. * * @param when the other date * @return true, if the date represented by this object is * strictly later than the time represented by when. */ public boolean after(Date when) { return time > when.time; } /** * Tests if this date is before the specified date. * * @param when the other date * @return true, if the date represented by when is strictly later * than the time represented by this object. */ public boolean before(Date when) { return time < when.time; } /** * Compares two dates for equality. * * @param obj the object to compare. * @return true, if obj is a Date object and the time represented * by obj is exactly the same as the time represented by this * object. */ public boolean equals(Object obj) { return (obj instanceof Date && time == ((Date) obj).time); } /** * Compares two dates. * * @param when the other date. * @return 0, if the date represented * by obj is exactly the same as the time represented by this * object, a negative if this Date is before the other Date, and * a positive value otherwise. */ public int compareTo(Date when) { return (time < when.time) ? -1 : (time == when.time) ? 0 : 1; } /** * Compares this Date to another object. This behaves like * <code>compareTo(Date)</code>, but it takes a generic object * and throws a <code>ClassCastException</code> if obj is * not a <code>Date</code>. * * @param obj the other date. * @return 0, if the date represented * by obj is exactly the same as the time represented by this * object, a negative if this Date is before the other Date, and * a positive value otherwise. * @exception ClassCastException if obj is not of type Date. */ public int compareTo(Object obj) { return compareTo((Date) obj); } /** * Computes the hash code of this <code>Date</code> as the * XOR of the most significant and the least significant * 32 bits of the 64 bit milliseconds value. * * @return the hash code. */ public int hashCode() { return (int) time ^ (int) (time >>> 32); } /** * <p> * Returns a string representation of this date using * the following date format: * </p> * <p> * <code>day mon dd hh:mm:ss zz yyyy</code> * </p> * <p>where the fields used here are: * <ul> * <li> * <code>day</code> -- the day of the week * (Sunday through to Saturday). * </li> * <li> * <code>mon</code> -- the month (Jan to Dec). * </li> * <li> * <code>dd</code> -- the day of the month * as two decimal digits (01 to 31). * </li> * <li> * <code>hh</code> -- the hour of the day * as two decimal digits in 24-hour clock notation * (01 to 23). * </li> * <li> * <code>mm</code> -- the minute of the day * as two decimal digits (01 to 59). * </li> * <li> * <code>ss</code> -- the second of the day * as two decimal digits (01 to 61). * </li> * <li> * <code>zz</code> -- the time zone information if available. * The possible time zones used include the abbreviations
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -