📄 date.java
字号:
* @deprecated Use DateFormat.parse(String) */ public static long parse(String string) { // Initialize date/time fields before parsing begins. int year = -1; int month = -1; int day = -1; int hour = -1; int minute = -1; int second = -1; int timezone = 0; boolean localTimezone = true; // Trim out any nested stuff in parentheses now to make parsing easier. StringBuffer buf = new StringBuffer(); int off = 0; int openParenOffset, tmpMonth; while ((openParenOffset = string.indexOf('(', off)) >= 0) { // Copy part of string leading up to open paren. buf.append(string.substring(off, openParenOffset)); off = skipParens(string, openParenOffset); } buf.append(string.substring(off)); // Make all chars upper case to simplify comparisons later. // Also ignore commas; treat them as delimiters. StringTokenizer strtok = new StringTokenizer(buf.toString().toUpperCase(), " \t\n\r,"); while (strtok.hasMoreTokens()) { String tok = strtok.nextToken(); char firstch = tok.charAt(0); if ((firstch == '+' || firstch == '-') && year >= 0) { timezone = parseTz(tok, firstch); localTimezone = false; } else if (firstch >= '0' && firstch <= '9') { while (tok != null && tok.length() > 0) { // A colon or slash may be valid in the number. // Find the first of these before calling parseInt. int colon = tok.indexOf(':'); int slash = tok.indexOf('/'); int hyphen = tok.indexOf('-'); // We choose tok.length initially because it makes // processing simpler. int punctOffset = tok.length(); if (colon >= 0) punctOffset = Math.min(punctOffset, colon); if (slash >= 0) punctOffset = Math.min(punctOffset, slash); if (hyphen >= 0) punctOffset = Math.min(punctOffset, hyphen); // Following code relies on -1 being the exceptional // case. if (punctOffset == tok.length()) punctOffset = -1; int num; try { num = Integer.parseInt(punctOffset < 0 ? tok : tok.substring(0, punctOffset)); } catch (NumberFormatException ex) { throw new IllegalArgumentException(tok); } // TBD: Spec says year can be followed by a slash. That might // make sense if using YY/MM/DD formats, but it would fail in // that format for years <= 70. Also, what about 1900? That // is interpreted as the year 3800; seems that the comparison // should be num >= 1900 rather than just > 1900. // What about a year of 62 - 70? (61 or less could be a (leap) // second). 70/MM/DD cause an exception but 71/MM/DD is ok // even though there's no ambiguity in either case. // For the parse method, the spec as written seems too loose. // Until shown otherwise, we'll follow the spec as written. if (num > 70 && (punctOffset < 0 || punctOffset == slash)) year = num > 1900 ? num - 1900 : num; else if (punctOffset > 0 && punctOffset == colon) { if (hour < 0) hour = num; else minute = num; } else if (punctOffset > 0 && punctOffset == slash) { if (month < 0) month = num - 1; else day = num; } else if (hour >= 0 && minute < 0) minute = num; else if (minute >= 0 && second < 0) second = num; else if (day < 0) day = num; else throw new IllegalArgumentException(tok); // Advance string if there's more to process in this token. if (punctOffset < 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 minutes and seconds should default to 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 || hour < 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. return UTC(year, month, day, hour, minute, second) + (localTimezone ? new Date(year, month, day).getTimezoneOffset() * 60 * 1000: -timezone * 60 * 1000); } /** * @return the year minus 1900 represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.YEAR) * instead. Note about the 1900 difference in year. */ public int getYear() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.YEAR) - 1900; } /** * Sets the year to year minus 1900, not changing the other fields. * @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. */ public void setYear(int year) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.YEAR, 1900 + year); time = cal.getTimeInMillis(); } /** * @return the month represented by this date object (zero based). * @deprecated Use Calendar instead of Date, and use get(Calendar.MONTH) * instead. */ public int getMonth() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.MONTH); } /** * Sets the month to the given value, not changing the other fields. * @param month the month, zero based. * @deprecated Use Calendar instead of Date, and use * set(Calendar.MONTH, month) instead. */ public void setMonth(int month) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.MONTH, month); time = cal.getTimeInMillis(); } /** * @return the day of month represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.DATE) * instead. */ public int getDate() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.DATE); } /** * Sets the date to the given value, not changing the other fields. * @param date the date. * @deprecated Use Calendar instead of Date, and use * set(Calendar.DATE, date) instead. */ public void setDate(int date) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.DATE, date); time = cal.getTimeInMillis(); } /** * @return the day represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK) * instead. */ 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; } /** * @return the hours represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY) * instead. */ public int getHours() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.HOUR_OF_DAY); } /** * Sets the hours to the given value, not changing the other fields. * @param hours the hours. * @deprecated Use Calendar instead of Date, and use * set(Calendar.HOUR_OF_DAY, hours) instead. */ public void setHours(int hours) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.HOUR_OF_DAY, hours); time = cal.getTimeInMillis(); } /** * @return the minutes represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.MINUTE) * instead. */ public int getMinutes() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.MINUTE); } /** * Sets the minutes to the given value, not changing the other fields. * @param minutes the minutes. * @deprecated Use Calendar instead of Date, and use * set(Calendar.MINUTE, minutes) instead. */ public void setMinutes(int minutes) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.MINUTE, minutes); time = cal.getTimeInMillis(); } /** * @return the seconds represented by this date object. * @deprecated Use Calendar instead of Date, and use get(Calendar.SECOND) * instead. */ public int getSeconds() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); return cal.get(Calendar.SECOND); } /** * Sets the seconds to the given value, not changing the other fields. * @param seconds the seconds. * @deprecated Use Calendar instead of Date, and use * set(Calendar.SECOND, seconds) instead. */ public void setSeconds(int seconds) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); cal.set(Calendar.SECOND, seconds); time = cal.getTimeInMillis(); } /** * Reads an Object from the stream. */ private void readObject(java.io.ObjectInputStream input) throws java.io.IOException, ClassNotFoundException { input.defaultReadObject(); time = input.readLong(); } /** * Writes an Object to 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(). */ private void writeObject(java.io.ObjectOutputStream output) throws java.io.IOException { output.defaultWriteObject(); output.writeLong(time); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -