jdatetime.java

来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 1,806 行 · 第 1/4 页

JAVA
1,806
字号
	 * @param day
	 *            delta days
	 * @param hour
	 *            delta hours
	 * @param minute
	 *            delta minutes
	 * @param second
	 *            delta seconds
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */

	public void add(int year, int month, int day, int hour, int minute, double second, boolean monthFix) {
		second += time.second;
		minute += time.minute;
		hour += time.hour;
		day += time.day;
		if (monthFix == false) {
			month += time.month;
			year += time.year;
			set(year, month, day, hour, minute, second);
		} else {
			// month fix:
			// 1. add all except month and year
			// 2. store day value
			// 3. add just months
			// 4. if new date is not equal to stored, return to last day of
			// previous month
			setJdOnly(time.year, time.month, day, hour, minute, second);
			int from = time.day;
			month += time.month + (year * 12); // delta years to add are
			// converted to delta months
			setJdOnly(time.year, month, time.day, time.hour, time.minute, time.second);
			if (time.day < from) {
				set(time.year, time.month, 0, time.hour, time.minute, time.second);
			} else {
				setParams();
			}

			/*
			 * // 5. store month value // 6. add just year // 7. if new month is
			 * not equal to stored, rturn to last day of previous month from =
			 * time.month; year += time.year; setJdOnly(year, time.month,
			 * time.day, time.hour, time.minute, time.second); if (time.month >
			 * from) { set(time.year, time.month, 0, time.hour, time.minute,
			 * time.second); }
			 */
		}
	}

	/**
	 * Performs time adding with preset value of monthFix attribute.
	 * 
	 * @param year
	 *            delta year
	 * @param month
	 *            delta month
	 * @param day
	 *            delta days
	 * @param hour
	 *            delta hours
	 * @param minute
	 *            delta minutes
	 * @param second
	 *            delta seconds
	 * 
	 * @see #add(int, int, int, int, int, double, boolean)
	 */
	public void add(int year, int month, int day, int hour, int minute, double second) {
		add(year, month, day, hour, minute, second, getMonthFix());
	}

	/**
	 * Adds date, leaving time unchanged.
	 * 
	 * @param year
	 *            years to add
	 * @param month
	 *            months to add
	 * @param day
	 *            days to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 * 
	 * @see #add(int, int, int, int, int, double, boolean)
	 */
	public void add(int year, int month, int day, boolean monthFix) {
		add(year, month, day, 0, 0, 0, monthFix);
	}

	/**
	 * Adds date, leaving time unchanged, with preset value of monthFix.
	 * attribute.
	 * 
	 * @param year
	 *            years to add
	 * @param month
	 *            months to add
	 * @param day
	 *            days to add
	 * 
	 * @see #add(int, int, int, boolean)
	 */
	public void add(int year, int month, int day) {
		add(year, month, day, getMonthFix());
	}

	/**
	 * Adds time.
	 * 
	 * @param hour
	 *            hours to add
	 * @param minute
	 *            minutes to add
	 * @param second
	 *            seconds to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 * 
	 * @see #add(int, int, int, int, int, double)
	 */
	public void addTime(int hour, int minute, double second, boolean monthFix) {
		add(0, 0, 0, hour, minute, second, monthFix);
	}

	/**
	 * Adds time, with preset value of monthFix.
	 * 
	 * @param hour
	 *            hours to add
	 * @param minute
	 *            minutes to add
	 * @param second
	 *            seconds to add
	 * 
	 * @see #addTime(int, int, double, boolean)
	 */
	public void addTime(int hour, int minute, double second) {
		addTime(hour, minute, second, getMonthFix());
	}

	/**
	 * Adds year.
	 * 
	 * @param y
	 *            year to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addYear(int y, boolean monthFix) {
		add(y, 0, 0, monthFix);
	}

	/**
	 * Adds year, with preset value of monthFix.
	 * 
	 * @param y
	 *            year to add
	 */
	public void addYear(int y) {
		addYear(y, getMonthFix());
	}

	/**
	 * Adds month.
	 * 
	 * @param m
	 *            month to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addMonth(int m, boolean monthFix) {
		add(0, m, 0, monthFix);
	}

	/**
	 * Adds month, with preset value of monthFix.
	 * 
	 * @param m
	 *            month to add
	 */
	public void addMonth(int m) {
		addMonth(m, getMonthFix());
	}

	/**
	 * Adds days.
	 * 
	 * @param d
	 *            days to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addDay(int d, boolean monthFix) {
		add(0, 0, d, monthFix);
	}

	/**
	 * Adds days, with preset value of monthFix.
	 * 
	 * @param d
	 *            days to add
	 */
	public void addDay(int d) {
		addDay(d, getMonthFix());
	}

	/**
	 * Adds hours.
	 * 
	 * @param h
	 *            hours to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addHour(int h, boolean monthFix) {
		addTime(h, 0, 0, monthFix);
	}

	/**
	 * Adds hours, with preset value of monthFix.
	 * 
	 * @param h
	 *            hours to add
	 */
	public void addHour(int h) {
		addHour(h, getMonthFix());
	}

	/**
	 * Adds minutes.
	 * 
	 * @param m
	 *            minutes to add.
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addMinute(int m, boolean monthFix) {
		addTime(0, m, 0, monthFix);
	}

	/**
	 * Adds minutes, with preset value of monthFix.
	 * 
	 * @param m
	 *            minutes to add.
	 */
	public void addMinute(int m) {
		addMinute(m, getMonthFix());
	}

	/**
	 * Adds seconds.
	 * 
	 * @param s
	 *            seconds to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addSecond(double s, boolean monthFix) {
		addTime(0, 0, s, monthFix);
	}

	/**
	 * Adds seconds, with preset value of monthFix.
	 * 
	 * @param s
	 *            seconds to add
	 */
	public void addSecond(double s) {
		addSecond(s, getMonthFix());
	}

	/**
	 * Adds milliseconds.
	 * 
	 * @param ms
	 *            miliseconds to add
	 * @param monthFix
	 *            <code>true</code> for month fixing, <code>false</code>
	 *            otherwise
	 */
	public void addMillisecond(int ms, boolean monthFix) {
		addTime(0, 0, ms / 1000.0, monthFix);
	}

	/**
	 * Adds milliseconds, with preset value of monthFix.
	 * 
	 * @param ms
	 *            miliseconds to add
	 */
	public void addMillisecond(int ms) {
		addMillisecond(ms, getMonthFix());
	}

	// ----------------------------------------------------------------
	// constructors/sets/gets

	/**
	 * Constructor that set date and time.
	 * 
	 * @param year
	 *            year to set
	 * @param month
	 *            month to set
	 * @param day
	 *            day to set
	 * @param hour
	 *            hours to set
	 * @param minute
	 *            minutes to set
	 * @param second
	 *            seconds to set
	 * 
	 * @see #set(int, int, int, int, int, double)
	 */
	public JDateTime(int year, int month, int day, int hour, int minute, double second) {
		this.set(year, month, day, hour, minute, second);
	}

	/**
	 * Sets date, time is set to midnight (00:00:00.000).
	 * 
	 * @param year
	 *            year to set
	 * @param month
	 *            month to set
	 * @param day
	 *            day to set
	 */
	public void set(int year, int month, int day) {
		set(year, month, day, 0, 0, 0);
	}

	/**
	 * Constructor that sets just date. Time is set to 00:00:00.
	 * 
	 * @param year
	 *            year to set
	 * @param month
	 *            month to set
	 * @param day
	 *            day to set
	 * 
	 * @see #set(int, int, int)
	 */
	public JDateTime(int year, int month, int day) {
		this.set(year, month, day);
	}

	/**
	 * Sets time, date is unchanged.
	 * 
	 * @param hour
	 *            hours to set
	 * @param minute
	 *            minutes to set
	 * @param second
	 *            secnds to set
	 */
	public void setTime(int hour, int minute, double second) {
		set(time.year, time.month, time.day, hour, minute, second);
	}

	/**
	 * Sets date, time remains unchanged.
	 * 
	 * @param year
	 *            year
	 * @param month
	 *            month
	 * @param day
	 *            day
	 */
	public void setDate(int year, int month, int day) {
		set(year, month, day, time.hour, time.minute, time.second);
	}

	// ---------------------------------------------------------------- set from
	// milliseconds

	/**
	 * Constructor that sets current time specified as time in milliseconds,
	 * from the midnight, January 1, 1970 UTC.
	 * 
	 * @param milis
	 *            time in milliseconds, from the midnight, January 1, 1970 UTC
	 * 
	 * @see #set(long )
	 */
	public JDateTime(long milis) {
		set(milis);
	}

	private static final double MILIS_IN_DAY = 1000 * 60 * 60 * 24;

	/**
	 * Sets the time based on current time in milliseconds. Current time is
	 * calculated from the midnight, January 1, 1970 UTC.
	 * 
	 * @param milis
	 *            time in milliseconds, from the midnight, January 1, 1970 UTC
	 */
	public void set(long milis) {
		BigDecimal bd = JD_1970.toBigDecimal();
		milis += TimeZone.getDefault().getOffset(milis);
		BigDecimal delta = new BigDecimal(milis / MILIS_IN_DAY);
		JulianDateStamp now = new JulianDateStamp(bd.add(delta));
		setJulianDate(now);
	}

	// ----------------------------------------------------------------
	// date/time sets

	/**
	 * Sets current year.
	 * 
	 * @param y
	 *            year to set
	 */
	public void setYear(int y) {
		setDate(y, time.month, time.day);
	}

	/**
	 * Sets current month.
	 * 
	 * @param m
	 *            month to set
	 */
	public void setMonth(int m) {
		setDate(time.year, m, time.day);
	}

	/**
	 * Sets current day.
	 * 
	 * @param d
	 *            day to set
	 */
	public void setDay(int d) {
		setDate(time.year, time.month, d);

⌨️ 快捷键说明

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