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

📄 datetimestamp.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.datetime;

/**
 * Generic date time stamp just stores and holds date and time information.
 * This class does not contain any date/time manipulation.
 *
 * @see JDateTime
 */
public class DateTimeStamp implements Comparable, java.io.Serializable {

	/**
	 * Default empty constructor.
	 */
	public DateTimeStamp() {
	}

	/**
	 * Constructor that sets date and time.
	 */
	public DateTimeStamp(int year, int month, int day, int hour, int minute, double second) {
		this.year = year;
		this.month = month;
		this.day = day;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	/**
	 * Constructor that sets just date. Time is set to zeros.
	 */
	public DateTimeStamp(int year, int month, int day) {
		this(year, month, day, 0, 0, 0);
	}
	
	/**
	 * Year
	 */
	public int year;

	/**
	 * Month, range: [1 - 12]
	 */
	public int month = 1;
	
	/**
	 * Day, range: [1 - 31]
	 */
	public int day = 1;

	/**
	 * Hour, range: [0 - 23]
	 */
	public int hour;
	
	/**
	 * Minute, range [0 - 59]
	 */
	public int minute;

	/**
	 * Second, range: [0.000 - 59.999]
	 */
	public double second;


	// ---------------------------------------------------------------- compare

	/**
	 * Compares this object with the specified object for order.  Returns a
	 * negative integer, zero, or a positive integer as this object is less
	 * than, equal to, or greater than the specified object.<p>
	 *
	 * @param   o the Object to be compared.
	 * @return  a negative integer, zero, or a positive integer as this object
	 *		is less than, equal to, or greater than the specified object.
	 *
	 * @throws ClassCastException if the specified object's type prevents it
	 *         from being compared to this Object.
	 */
	public int compareTo(Object o) {
		DateTimeStamp gts = (DateTimeStamp) o;

		int date1 = year * 10000 + month * 100 + day;
		int date2 = gts.year * 10000 + gts.month * 100 + gts.day;

		if (date1 < date2) {
			return -1;
		}
		if (date1 > date2) {
			return 1;
		}

		date1 = hour * 10000000 + minute * 100000 + (int) (second * 1000);
		date2 = gts.hour * 10000000 + gts.minute * 100000 + (int) (gts.second * 1000);

		if (date1 < date2) {
			return -1;
		}
		if (date1 > date2) {
			return 1;
		}

		return 0;
	}


	// ---------------------------------------------------------------- toString

	/**
	 * Simple to string conversion.
	 *
	 * @return date/time string in 'y-m-d h:m:m.s' format
	 */
	public String toString() {
		double sec = (int)(second * 1000) / 1000.0;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + sec;
	}
}

⌨️ 快捷键说明

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