📄 date.java
字号:
if (punct < 0 || punctOffset + 1 >= tok.length()) tok = null; else tok = tok.substring(punctOffset + 1); } } else if (firstch >= 'A' && firstch <= 'Z') { if (tok.equals("AM")) { if (hour < 1 || hour > 12) throw new IllegalArgumentException(tok); if (hour == 12) hour = 0; } else if (tok.equals("PM")) { if (hour < 1 || hour > 12) throw new IllegalArgumentException(tok); if (hour < 12) hour += 12; } else if (parseDayOfWeek(tok)) ; // Ignore it; throw the token away. else if (tok.equals("UT") || tok.equals("UTC") || tok.equals("GMT")) localTimezone = false; else if (tok.startsWith("UT") || tok.startsWith("GMT")) { int signOffset = 3; if (tok.charAt(1) == 'T' && tok.charAt(2) != 'C') signOffset = 2; char sign = tok.charAt(signOffset); if (sign != '+' && sign != '-') throw new IllegalArgumentException(tok); timezone = parseTz(tok.substring(signOffset), sign); localTimezone = false; } else if ((tmpMonth = parseMonth(tok)) >= 0) month = tmpMonth; else if (tok.length() == 3 && tok.charAt(2) == 'T') { // Convert timezone offset from hours to minutes. char ch = tok.charAt(0); if (ch == 'E') timezone = -5 * 60; else if (ch == 'C') timezone = -6 * 60; else if (ch == 'M') timezone = -7 * 60; else if (ch == 'P') timezone = -8 * 60; else throw new IllegalArgumentException(tok); // Shift 60 minutes for Daylight Savings Time. if (tok.charAt(1) == 'D') timezone += 60; else if (tok.charAt(1) != 'S') throw new IllegalArgumentException(tok); localTimezone = false; } else throw new IllegalArgumentException(tok); } else throw new IllegalArgumentException(tok); } // Unspecified hours, minutes, or seconds should default to 0. if (hour < 0) hour = 0; if (minute < 0) minute = 0; if (second < 0) second = 0; // Throw exception if any other fields have not been recognized and set. if (year < 0 || month < 0 || day < 0) throw new IllegalArgumentException("Missing field"); // Return the time in either local time or relative to GMT as parsed. // If no time-zone was specified, get the local one (in minutes) and // convert to milliseconds before adding to the UTC. GregorianCalendar cal = new GregorianCalendar(year, month, day, hour, minute, second); if (!localTimezone) { cal.set(Calendar.ZONE_OFFSET, timezone * 60 * 1000); cal.set(Calendar.DST_OFFSET, 0); } return cal.getTimeInMillis(); } /** * Returns the difference between the year represented by this * <code>Date</code> object and 1900. * * @return the year minus 1900 represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.YEAR) * instead. Note the 1900 difference in the year. * @see Calendar * @see #setYear(int) */ public int getYear() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.YEAR) - 1900; } /** * Sets the year to the specified year, plus 1900. The other * fields are only altered as required to match the same date * and time in the new year. Usually, this will mean that * the fields are not changed at all, but in the case of * a leap day or leap second, the fields will change in * relation to the existence of such an event in the new year. * For example, if the date specifies February the 29th, 2000, * then this will become March the 1st if the year is changed * to 2001, as 2001 is not a leap year. Similarly, a seconds * value of 60 or 61 may result in the seconds becoming 0 and * the minute increasing by 1, if the new time does not include * a leap second. * * @param year the year minus 1900. * @deprecated Use Calendar instead of Date, and use * set(Calendar.YEAR, year) instead. Note about the 1900 * difference in year. * @see #getYear() * @see Calendar */ public void setYear(int year) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.YEAR, 1900 + year); time = cal.getTimeInMillis(); } /** * Returns the month represented by this <code>Date</code> object, * as a value between 0 (January) and 11 (December). * * @return the month represented by this date object (zero based). * @deprecated Use Calendar instead of Date, and use get(Calendar.MONTH) * instead. * @see #setMonth(int) * @see Calendar */ public int getMonth() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.MONTH); } /** * Sets the month to the given value. The other * fields are only altered as necessary to match * the same date and time in the new month. In most * cases, the other fields won't change at all. However, * in the case of a shorter month or a leap second, values * may be adjusted. For example, if the day of the month * is currently 31, and the month value is changed from * January (0) to September (8), the date will become * October the 1st, as September only has 30 days. Similarly, * a seconds value of 60 or 61 (a leap second) may result * in the seconds value being reset to 0 and the minutes * value being incremented by 1, if the new time does * not include a leap second. * * @param month the month, with a zero-based index * from January. * @deprecated Use Calendar instead of Date, and use * set(Calendar.MONTH, month) instead. * @see #getMonth() * @see Calendar */ public void setMonth(int month) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.MONTH, month); time = cal.getTimeInMillis(); } /** * Returns the day of the month of this <code>Date</code> * object, as a value between 0 and 31. * * @return the day of month represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.DATE) * instead. * @see Calendar * @see #setDate(int) */ public int getDate() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.DATE); } /** * Sets the date to the given value. The other * fields are only altered as necessary to match * the same date and time on the new day of the month. In most * cases, the other fields won't change at all. However, * in the case of a leap second or the day being out of * the range of the current month, values * may be adjusted. For example, if the day of the month * is currently 30 and the month is June, a new day of the * month value of 31 will cause the month to change to July, * as June only has 30 days . Similarly, * a seconds value of 60 or 61 (a leap second) may result * in the seconds value being reset to 0 and the minutes * value being incremented by 1, if the new time does * not include a leap second. * * @param date the date. * @deprecated Use Calendar instead of Date, and use * set(Calendar.DATE, date) instead. * @see Calendar * @see #getDate() */ public void setDate(int date) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.DATE, date); time = cal.getTimeInMillis(); } /** * Returns the day represented by this <code>Date</code> * object as an integer between 0 (Sunday) and 6 (Saturday). * * @return the day represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK) * instead. * @see Calendar */ public int getDay() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); // For Calendar, Sunday is 1. For Date, Sunday is 0. return cal.get(Calendar.DAY_OF_WEEK) - 1; } /** * Returns the hours represented by this <code>Date</code> * object as an integer between 0 and 23. * * @return the hours represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY) * instead. * @see Calendar * @see #setHours(int) */ public int getHours() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.HOUR_OF_DAY); } /** * Sets the hours to the given value. The other * fields are only altered as necessary to match * the same date and time in the new hour. In most * cases, the other fields won't change at all. However, * in the case of a leap second, values * may be adjusted. For example, * a seconds value of 60 or 61 (a leap second) may result * in the seconds value being reset to 0 and the minutes * value being incremented by 1 if the new hour does * not contain a leap second. * * @param hours the hours. * @deprecated Use Calendar instead of Date, and use * set(Calendar.HOUR_OF_DAY, hours) instead. * @see Calendar * @see #getHours() */ public void setHours(int hours) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.HOUR_OF_DAY, hours); time = cal.getTimeInMillis(); } /** * Returns the number of minutes represented by the <code>Date</code> * object, as an integer between 0 and 59. * * @return the minutes represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.MINUTE) * instead. * @see Calendar * @see #setMinutes(int) */ public int getMinutes() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.MINUTE); } /** * Sets the minutes to the given value. The other * fields are only altered as necessary to match * the same date and time in the new minute. In most * cases, the other fields won't change at all. However, * in the case of a leap second, values * may be adjusted. For example, * a seconds value of 60 or 61 (a leap second) may result * in the seconds value being reset to 0 and the minutes * value being incremented by 1 if the new minute does * not contain a leap second. * * @param minutes the minutes. * @deprecated Use Calendar instead of Date, and use * set(Calendar.MINUTE, minutes) instead. * @see Calendar * @see #getMinutes() */ public void setMinutes(int minutes) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.MINUTE, minutes); time = cal.getTimeInMillis(); } /** * Returns the number of seconds represented by the <code>Date</code> * object, as an integer between 0 and 61 (60 and 61 being leap seconds). * * @return the seconds represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.SECOND) * instead. * @see Calendar * @see #setSeconds(int) */ public int getSeconds() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.SECOND); } /** * Sets the seconds to the given value. The other * fields are only altered as necessary to match * the same date and time in the new minute. In most * cases, the other fields won't change at all. However, * in the case of a leap second, values * may be adjusted. For example, setting the * seconds value to 60 or 61 (a leap second) may result * in the seconds value being reset to 0 and the minutes * value being incremented by 1, if the current time does * not contain a leap second. * * @param seconds the seconds. * @deprecated Use Calendar instead of Date, and use * set(Calendar.SECOND, seconds) instead. * @see Calendar * @see #getSeconds() */ public void setSeconds(int seconds) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.SECOND, seconds); time = cal.getTimeInMillis(); } /** * Deserializes a <code>Date</code> object from an * input stream, setting the time (in milliseconds * since the epoch) to the long value read from the * stream. * * @param input the input stream. * @throws IOException if an I/O error occurs in the stream. * @throws ClassNotFoundException if the class of the * serialized object could not be found. */ private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { input.defaultReadObject(); time = input.readLong(); } /** * Serializes a <code>Date</code> object to an output stream, * storing the time (in milliseconds since the epoch) as a long * value in the stream. * * @serialdata A long value representing the offset from the epoch * in milliseconds. This is the same value that is returned by the * method getTime(). * @param output the output stream. * @throws IOException if an I/O error occurs in the stream. */ private void writeObject(ObjectOutputStream output) throws IOException { output.defaultWriteObject(); output.writeLong(time); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -