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

📄 dates.java

📁 非常接近C/S操作方式的Java Ajax框架-ZK 用ZK框架使你的B/S应用程序更漂亮更易操作。 官网:www.zkoss.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		cal.setTimeInMillis(cal.getTimeInMillis() - 1);				return cal.getTime();	}	/**	 * Return the ending date of this year.	 */	final public static short twoMonthShort() {		return twoMonthShort(new Date(), null);	}	/**	 * Given a date, a proper TimeZone, return the two month int. eg. 1, 3, 5, 7, 9, 11.	 * If TimeZone is null, meaning use default TimeZone of the JVM.	 */	final public static short twoMonthShort(Date when, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime()); //don't call cal.setTime(Date) which will reset the TimeZone.		final int month = (cal.get(Calendar.MONTH) / 2) * 2 + 1;		return (short)month;	}		/**	 * Get the year of a date.	 * @param when The date.	 * @param tz The time zone; if null, the current time zone is assumed.	 * @see #localizedYearOfDate	 */	public static final int yearOfDate(Date when, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime()); //don't call cal.setTime(Date) which will reset the TimeZone.				return cal.get(Calendar.YEAR);	}		/**	 * Get the year of a date in the specified locale.	 *	 * <p>Currenty, only Locale.ZH_TW is supported, i.e.,	 * "year - 1911" and it's may be less than 0. Otherwise, it is the same	 * as {@link #yearOfDate}.	 *	 * @param when The date.	 * @param locale the locale; if null, the current locale is assumed.	 * @param tz The time zone; if null, the current time zone is assumed.	 * @see #yearOfDate	 */	public static final int	localizedYearOfDate(Date when, Locale locale, TimeZone tz) {		if (locale == null)			locale = Locales.getCurrent();		final int year = yearOfDate(when, tz);		if (locale.equals(Locale.TAIWAN))			return year - 1911;		return year;	}		/**	 * Get the month of a date. The first month of the year is JANUARY which is 0.	 * @param when The date.	 * @param tz The time zone; if null, the current time zone is assumed.	 */	public static final int monthOfDate(Date when, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime()); //don't call cal.setTime(Date) which will reset the TimeZone.				return cal.get(Calendar.MONTH);	}		/**	 * Get the month of a date. The first month of the year is JANUARY which is 1.	 * @param when The date.	 * @param tz The time zone; if null, the current time zone is assumed.	 */	public static final int monthOfDatePlus1(Date when, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime()); //don't call cal.setTime(Date) which will reset the TimeZone.				return cal.get(Calendar.MONTH) + 1;	}		/**	 * Get the day of month of a date. The first day of the month has value 1.	 * @param when The date.	 * @param tz The time zone; if null, the current time zone is assumed.	 */	public static final int dayMonthOfDate(Date when, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime()); //don't call cal.setTime(Date) which will reset the TimeZone.				return cal.get(Calendar.DAY_OF_MONTH);	}		/**	 * Date Arithmetic function. Adds the specified (signed) amount of time to the given date, 	 * based on the calendar's rules.	 * @param when The based date.	 * @param tz The time zone; if null, the current time zone is assumed.	 * @param field The time field.	 * @param amount The amount of date or time to be added to the field.	 */	public static final Date add(Date when, TimeZone tz, int field, int amount) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar cal = Calendar.getInstance(tz);		cal.setTimeInMillis(when.getTime());//don't call cal.setTime(Date) which will reset the TimeZone.				cal.add(field, amount);		return cal.getTime();	}	/**	 * Date Arithmetic function (date2 - date1). subtract a date from another date, return the	 * difference as the required fields. E.g. if specified Calendar.Date, the	 * smaller range of fields is ignored and this method return the difference	 * of days.	 *	 * @param date2 The date2.	 * @param tz The time zone.	 * @param field The time field; e.g., Calendar.DATE, Calendar.YEAR, it's default value is Calendar.DATE	 * @param date1 The date1.	 */	public static final long	subtract(Date date2, TimeZone tz, int field, Date date1) {		if (tz == null)			tz = TimeZones.getCurrent();		boolean negative = false;		if (date1.after(date2)) {			negative = true;			final Date d = date1;			date1 = date2;			date2 = d;		}				final Calendar cal1 = Calendar.getInstance(tz);		cal1.setTimeInMillis(date1.getTime());//don't call cal.setTime(Date) which will reset the TimeZone.		final Calendar cal2 = Calendar.getInstance(tz);		cal2.setTimeInMillis(date2.getTime());//don't call cal.setTime(Date) which will reset the TimeZone.				int year1 = cal1.get(Calendar.YEAR);		int year2 = cal2.get(Calendar.YEAR);				switch(field) {			case Calendar.YEAR:			{				return negative ? (year1-year2) : (year2-year1);			}			case Calendar.MONTH:			{				int month1 = cal1.get(Calendar.MONTH);				int month2 = cal2.get(Calendar.MONTH);				int months = 12 * (year2 - year1) + month2 - month1;				return negative ? -months : months;			}			case Calendar.HOUR:			{				int hour1 = cal1.get(Calendar.HOUR_OF_DAY);				int hour2 = cal2.get(Calendar.HOUR_OF_DAY);				int day1 = cal1.get(Calendar.DAY_OF_YEAR);				int day2 = cal2.get(Calendar.DAY_OF_YEAR);								int maxDay1 = year1 == year2 ? 					0 : cal1.getActualMaximum(Calendar.DAY_OF_YEAR);				int hours = 24 * (maxDay1 - day1 + day2) + hour2 - hour1;								final Calendar cal = Calendar.getInstance(tz);				for(int year = year1+1; year < year2; year++) {					cal.set(Calendar.YEAR, year);					hours += 24 * cal.getActualMaximum(Calendar.DAY_OF_YEAR);				}				return negative ? -hours : hours;			}			case Calendar.MINUTE:			{				int minute1 = cal1.get(Calendar.MINUTE);				int minute2 = cal2.get(Calendar.MINUTE);				int hour1 = cal1.get(Calendar.HOUR_OF_DAY);				int hour2 = cal2.get(Calendar.HOUR_OF_DAY);				int day1 = cal1.get(Calendar.DAY_OF_YEAR);				int day2 = cal2.get(Calendar.DAY_OF_YEAR);								int maxHour1 = day1 == day2 ? 0 : 24;				int maxDay1 = year1 == year2 ? 					0 : cal1.getActualMaximum(Calendar.DAY_OF_YEAR);					int minutes = 60 * (maxHour1 - hour1 + hour2) + minute2 - minute1					+ 24 * 60 * (maxDay1 - day1 + day2);								for(int year = year1+1; year < year2; year++) {					final Calendar cal = Calendar.getInstance(tz);					cal.set(Calendar.YEAR, year);					minutes += 24 * 60 * cal.getActualMaximum(Calendar.DAY_OF_YEAR);				}				return negative ? -minutes : minutes;			}			case Calendar.SECOND:			{				long sec1 = date1.getTime() / 1000;				long sec2 = date2.getTime() / 1000;				return negative ? (sec1 - sec2) : (sec2 - sec1);			}			case Calendar.MILLISECOND:			{				return negative ? 					 (date1.getTime() - date2.getTime()): 					 (date2.getTime() - date1.getTime());			}				case Calendar.DATE:			default:	/*default, like -1*/			{				int day1 = cal1.get(Calendar.DAY_OF_YEAR);				int day2 = cal2.get(Calendar.DAY_OF_YEAR);					int maxDay1 = year1 == year2 ? 					0 : cal1.getActualMaximum(Calendar.DAY_OF_YEAR);				int days = maxDay1 - day1 + day2;									final Calendar cal = Calendar.getInstance(tz);				for(int year = year1+1; year < year2; year++) {					cal.set(Calendar.YEAR, year);					days += cal.getActualMaximum(Calendar.DAY_OF_YEAR);				}				return negative ? -days : days;			}		}	}	/**	 * merge the date part and time part of two specified dates into a date.	 * @param datePart The date part date.	 * @param timePart The time part date.	 * @param tz The time zone.	 */	public static final Date merge(Date datePart, Date timePart, TimeZone tz) {		if (tz == null)			tz = TimeZones.getCurrent();		final Calendar dateCal = Calendar.getInstance(tz);		dateCal.setTimeInMillis(datePart.getTime());//don't call cal.setTime(Date) which will reset the TimeZone.		final Calendar timeCal = Calendar.getInstance(tz);		timeCal.setTimeInMillis(timePart.getTime());//don't call cal.setTime(Date) which will reset the TimeZone.				final int hour = timeCal.get(Calendar.HOUR);		final int minute = timeCal.get(Calendar.MINUTE);		final int second = timeCal.get(Calendar.SECOND);		final int msillisecond = timeCal.get(Calendar.MILLISECOND);				dateCal.set(Calendar.HOUR, hour);		dateCal.set(Calendar.MINUTE, minute);		dateCal.set(Calendar.SECOND, second);		dateCal.set(Calendar.MILLISECOND, msillisecond);				return dateCal.getTime();	}}

⌨️ 快捷键说明

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