⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   * recognised by <code>parse()</code> (e.g. GMT, CET, etc.)   * and may reflect the fact that daylight savings time is in   * effect.  The empty string is used if there is no time zone   * information.   * </li>   * <li>   * <code>yyyy</code> -- the year as four decimal digits.   * </li>   * </ul>   * <p>   * The <code>DateFormat</code> class should now be    * preferred over using this method.   * </p>   *   * @return A string of the form 'day mon dd hh:mm:ss zz yyyy'   * @see #parse(String)   * @see DateFormat   */  public String toString()  {    Calendar cal = Calendar.getInstance();    cal.setTimeInMillis(time);    String day = "0" + cal.get(Calendar.DATE);    String hour = "0" + cal.get(Calendar.HOUR_OF_DAY);    String min = "0" + cal.get(Calendar.MINUTE);    String sec = "0" + cal.get(Calendar.SECOND);    String year = "000" + cal.get(Calendar.YEAR);    return weekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " "      + monthNames[cal.get(Calendar.MONTH)] + " "      + day.substring(day.length() - 2) + " "      + hour.substring(hour.length() - 2) + ":"      + min.substring(min.length() - 2) + ":"      + sec.substring(sec.length() - 2) + " "      +      cal.getTimeZone().getDisplayName(cal.getTimeZone().inDaylightTime(this),				       TimeZone.SHORT) + " " +      year.substring(year.length() - 4);  }  /**    * Returns a locale-dependent string representation of this   * <code>Date</code> object.   *   * @deprecated Use DateFormat.format(Date)   * @return A locale-dependent string representation.   * @see #parse(String)   * @see DateFormat   */  public String toLocaleString()  {    return java.text.DateFormat.getInstance().format(this);  }  /**    * <p>   * Returns a string representation of this <code>Date</code>   * object using GMT rather than the local timezone.   * The following date format is used:   * </p>   * <p>   * <code>d mon yyyy hh:mm:ss GMT</code>   * </p>   * <p>where the fields used here are:   * <ul>   * <li>   * <code>d</code> -- the day of the month   * as one or two decimal digits (1 to 31).   * </li>   * <li>   * <code>mon</code> -- the month (Jan to Dec).   * </li>   * <li>   * <code>yyyy</code> -- the year as four decimal digits.   * </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>GMT</code> -- the literal string "GMT"   * indicating Greenwich Mean Time as opposed to   * the local timezone.   * </li>   * </ul>   *    * @deprecated Use DateFormat.format(Date) with a GMT TimeZone.   * @return A string of the form 'd mon yyyy hh:mm:ss GMT' using   *         GMT as opposed to the local timezone.   * @see #parse(String)   * @see DateFormat   */  public String toGMTString()  {    java.text.DateFormat format = java.text.DateFormat.getInstance();    format.setTimeZone(TimeZone.getTimeZone("GMT"));    return format.format(this);  }  /**   * Parses the time zone string.   *   * @param tok The token containing the time zone.   * @param sign The sign (+ or -) used by the time zone.   * @return An integer representing the number of minutes offset   *         from GMT for the time zone.   */  private static int parseTz(String tok, char sign)    throws IllegalArgumentException  {    int num;    try      {	// parseInt doesn't handle '+' so strip off sign.	num = Integer.parseInt(tok.substring(1));      }    catch (NumberFormatException ex)      {	throw new IllegalArgumentException(tok);      }    // Convert hours to minutes.    if (num < 24)      num *= 60;    else      num = (num / 100) * 60 + num % 100;    return sign == '-' ? -num : num;  }  /**   * Parses the month string.   *   * @param tok the token containing the month.   * @return An integer between 0 and 11, representing   *         a month from January (0) to December (11),   *         or -1 if parsing failed.   */  private static int parseMonth(String tok)  {    // Initialize strings for month names.    // We could possibly use the fields of DateFormatSymbols but that is    // localized and thus might not match the English words specified.    String months[] = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",			"JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",			"NOVEMBER", "DECEMBER" };    int i;    for (i = 0; i < 12; i++)      if (months[i].startsWith(tok))        return i;    // Return -1 if not found.    return -1;  }  /**   * Parses the day of the week string.   *   * @param tok the token containing the day of the week.   * @return true if the token was parsed successfully.   */  private static boolean parseDayOfWeek(String tok)  {    // Initialize strings for days of the week names.    // We could possibly use the fields of DateFormatSymbols but that is    // localized and thus might not match the English words specified.    String daysOfWeek[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",			    "THURSDAY", "FRIDAY", "SATURDAY" };    int i;    for (i = 0; i < 7; i++)      if (daysOfWeek[i].startsWith(tok))        return true;    return false;  }  /**    * <p>   * Parses a String and returns the time, in milliseconds since the   * epoch, it represents.  Most syntaxes are handled, including   * the IETF date standard "day, dd mon yyyy hh:mm:ss zz" (see   * <code>toString()</code> for definitions of these fields).   * Standard U.S. time zone abbreviations are recognised, in   * addition to time zone offsets in positive or negative minutes.   * If a time zone is specified, the specified time is assumed to   * be in UTC and the appropriate conversion is applied, following   * parsing, to convert this to the local time zone.  If no zone   * is specified, the time is assumed to already be in the local   * time zone.   * </p>   * <p>   * The method parses the string progressively from left to right.   * At the end of the parsing process, either a time is returned   * or an <code>IllegalArgumentException</code> is thrown to signify   * failure.  The ASCII characters A-Z, a-z, 0-9, and ',', '+', '-',   * ':' and '/' are the only characters permitted within the string,   * besides whitespace and characters enclosed within parantheses   * '(' and ')'.     * </p>   * <p>   * A sequence of consecutive digits are recognised as a number,   * and interpreted as follows:   * <ul>   * <li>   * A number preceded by a sign (+ or -) is taken to be a time zone   * offset.  The time zone offset can be specified in either hours   * or minutes.  The former is assumed if the number is less than 24.   * Otherwise, the offset is assumed to be in minutes.  A - indicates   * a time zone west of GMT, while a + represents a time zone to the   * east of GMT.  The time zones are always assumed to be relative   * to GMT, and a (redundant) specification of this can be included   * with the time zone.  For example, '-9', 'utc-9' and 'GMT-9' all   * represent a time zone nine hours west of GMT.  Similarly,   * '+4', 'ut+4' and 'UTC+4' all give 4 hours east of GMT.   * </li>   * <li>   * A number equal to or greater than 70 is regarded as a year specification.   * Values lower than 70 are only assumed to indicate a year if both the   * day of the month and the month itself have already been recognised.   * Year values less than 100 are interpreted as being relative to the current   * century when the <code>Date</code> class is initialised..  Given a century,   * x, the year is assumed to be within the range x - 80 to x + 19.  The value   * itself is then used as a match against the two last digits of one of these   * years.  For example, take x to be 2004.  A two-digit year is assumed to fall   * within the range x - 80 (1924) and x + 19 (2023).  Thus, any intepreted value   * between 0 and 23 is assumed to be 2000 to 2023 and values between 24 and 99   * are taken as being 1924 to 1999.  This only applies for the case of 2004.   * With a different year, the values will be interpreted differently. 2005   * will used 0 to 24 as 2000 to 2024 and 25 to 99 as 1925 to 1999, for example.   * This behaviour differs from that of <code>SimpleDateFormat</code> and is   * time-dependent (a two-digit year will be interpreted differently depending   * on the time the code is run).   * </li>   * <li>   * Numbers followed by a colon are interpreted by first an hour, and then   * as a minute, once an hour has been found.   * </li>   * <li>   * <li>   * Numbers followed by a slash are regarded first as a month, and then as   * a day of the month once the month has been found.  This follows the   * U.S. date format of mm/dd, rather than the European dd/mm.  Months   * are converted to the recognised value - 1 before storage, in order   * to put the number within the range 0 to 11.   * </li>   * <li>   * Numbers followed by commas, whitespace, hyphens or the end of the string   * are interpreted in the following order: hour, minute, second, day of month.   * The first type not already recognised in the current string being parsed is   * assumed.   * </li>   * </ul>   * </p>   * <p>   * A sequence of consecutive alphabetic characters is recognised as a word,   * and interpreted as follows, in a case-insentive fashion:   * <ul>   * <li>   * The characters 'AM' or 'PM' restrict the hour value to a value between 0   * and 12.  In the latter case, 12 is added to the hour value before storage.   * </li>   * <li>   * Any words which match any prefix of one of the days of the week ('Monday',   * 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' and 'Sunday'),   * are simply ignored.   * </li>   * <li>   * Any words which match any prefix of one of the months of the year ('January',   * 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',   * 'October', 'November', 'December') are recognised and interpreted as the   * appropriate value between 0 and 11.  The first match made against a   * month is the one used, in the order specified here.  For example, 'Ma' is   * intepreted as 'March' (2) and not as 'May' (4).  Similarly, 'Ju' is 'June',   * and not 'July'.   * </li>   * <li>   * The words 'GMT', 'UT' and 'UTC' are interpreted as specifying UTC as the   * time zone in use for this date.   * </li>   * <li>   * The word pairs 'EST'/'EDT', 'CST'/'CDT', 'MST'/'MDT' and 'PST'/'PDT' are   * interpreted as the appropriate U.S. time zone abbreviation.  Each pair   * is the standard and daylight savings time zone specification, respectively,   * for each zone within the U.S, these being Eastern Standard/Daylight Time   * (-5), Central Standard/Daylight Time (-6), Mountain Standard/Daylight Time   * (-7) and Pacific Standard/Daylight Time (-8).   * </li>   * </ul>   *   * @param string The String to parse.   * @return The time in milliseconds since the epoch.   * @throws IllegalArgumentException if the string fails to parse.   * @deprecated Use DateFormat.parse(String)   * @see #toString()   * @see SimpleDateFormat   */  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 parenNesting = 0;    int len = string.length();    for (int i = 0;  i < len;  i++)      {	char ch = string.charAt(i);	if (ch >= 'a' && ch <= 'z')	  ch -= 'a' - 'A';	if (ch == '(')	  parenNesting++;	else if (parenNesting == 0)	  buf.append(ch);	else if (ch == ')')	  parenNesting--;      }    int tmpMonth;    // Make all chars upper case to simplify comparisons later.    // Also ignore commas; treat them as delimiters.    StringTokenizer strtok = new StringTokenizer(buf.toString(), " \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)	      {		int punctOffset = tok.length();		int num = 0;		int punct;		for (int i = 0;  ;  i++)		  {		    if (i >= punctOffset)		      {			punct = -1;			break;		      }		    else		      {			punct = tok.charAt(i);			if (punct >= '0' && punct <= '9')			  {			    if (num > 999999999) // in case of overflow			      throw new IllegalArgumentException(tok);			    num = 10 * num + (punct - '0');			  }			else			  {			    punctOffset = i;			    break;			  }		      }		      		  }		if (punct == ':')		  {		    if (hour < 0)		      hour = num;		    else		      minute = num;		  }	        else if ((num >= 70			  && (punct == ' ' || punct == ','			      || punct == '/' || punct < 0))			 || (num < 70 && day >= 0 && month >= 0 && year < 0))		  {		    if (num >= 100)		      year = num;		    else		      {			int curYear = 1900 + new Date().getYear();			int firstYear = curYear - 80;			year = firstYear / 100 * 100 + num;			if (year < firstYear)			  year += 100;		      }		  }		else if (punct == '/')		  {		    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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -