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

📄 date.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	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;			int yx = year;			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.		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();  }  /**   * @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 + -