jdatetime.java

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

JAVA
1,806
字号
	}

	/**
	 * Set current hour.
	 * 
	 * @param h
	 *            hour to set
	 */
	public void setHour(int h) {
		setTime(h, time.minute, time.second);
	}

	/**
	 * Set current minute.
	 * 
	 * @param m
	 *            minutes to set
	 */
	public void setMinute(int m) {
		setTime(time.hour, m, time.second);

	}

	/**
	 * Sets current second and millisecond.
	 * 
	 * @param s
	 *            seconds and milliseconds to set
	 */
	public void setSecond(double s) {
		setTime(time.hour, time.minute, s);
	}

	/**
	 * Sets current second.
	 * 
	 * @param s
	 *            seconds to set
	 */
	public void setSecond(int s) {
		double milis = s + (time.second - (int) time.second);
		setTime(time.hour, time.minute, milis);
	}

	/**
	 * Sets current millisecond.
	 * 
	 * @param m
	 *            milliseconds to set
	 */
	public void setMillisecond(int m) {
		setTime(time.hour, time.minute, ((int) time.second) + m / 1000.0);
	}

	// ----------------------------------------------------------------
	// date/time gets

	/**
	 * Returns current year.
	 * 
	 * @return current year
	 */
	public int getYear() {
		return time.year;
	}

	/**
	 * Returns current month.
	 * 
	 * @return current month
	 */
	public int getMonth() {
		return time.month;
	}

	/**
	 * Returns current day of month.
	 * 
	 * @return current day of month
	 * @see #getDayOfMonth
	 */
	public int getDay() {
		return time.day;
	}

	/**
	 * Returns current day of month.
	 * 
	 * @return current day of month
	 * @see #getDay
	 */
	public int getDayOfMonth() {
		return time.day;
	}

	/**
	 * Returns current hour.
	 * 
	 * @return current hour
	 */
	public int getHour() {
		return time.hour;
	}

	/**
	 * Returns current minutes.
	 * 
	 * @return current minutes
	 */
	public int getMinute() {
		return time.minute;
	}

	/**
	 * Return current secodns. For an integer value, just cast the returned
	 * value.
	 * 
	 * @return current seconds.
	 */
	public double getSecond() {
		return time.second;
	}

	/**
	 * Returns current milliseconds.
	 * 
	 * @return current milliseconds
	 */
	public int getMillisecond() {
		return (int) ((time.second - (int) time.second) * 1000 + 1e-9);
	}

	// ---------------------------------------------------------------- other
	// gets

	/**
	 * Returns current day of week.
	 * 
	 * @return current day of week
	 */
	public int getDayOfWeek() {
		return dayofweek;
	}

	/**
	 * Returns current day of year.
	 * 
	 * @return current day of year
	 */
	public int getDayOfYear() {
		return dayofyear;
	}

	/**
	 * Returns current leap year flag.
	 * 
	 * @return current leap year flag
	 */
	public boolean isLeap() {
		return leap;
	}

	/**
	 * Returns current week of year.
	 * 
	 * @return current week of year
	 */
	public int getWeekOfYear() {
		return weekofyear;
	}

	public int getWeekOfMonth() {
		return weekofmonth;
	}

	/**
	 * Length of months. Modified by main setJulianDate() method for February
	 * and leap years.
	 */
	private static final int MONTH_LENGTH[] = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	/**
	 * Returns the length of the specified month in days.
	 * 
	 * @return length of the specified month in days
	 */
	public int getMonthLength(int m) {
		if ((m < 1) || (m > 12)) {
			return -1;
		}
		return MONTH_LENGTH[m];
	}

	/**
	 * Returns the length of the current month in days.
	 * 
	 * @return length of the current month in days
	 */
	public int getMonthLength() {
		return MONTH_LENGTH[time.month];
	}

	// ---------------------------------------------------------------- setting
	// the current time

	/**
	 * Sets current local date and time.
	 */
	public void set() {
		loadFrom(Calendar.getInstance());
	}

	/**
	 * Constructor that sets current local date and time.
	 */
	public JDateTime() {
		this.set();
	}

	// ---------------------------------------------------------------- objects
	// and instances

	private static HashMap converters = new HashMap();
	static {
		registerDefaults();
	}

	/**
	 * Registers default converters.
	 */
	public static void registerDefaults() {
		converters.clear();
		register(JDateTime.class, new JDateTimeConverter());
		register(Calendar.class, new CalendarConverter());
		register(java.util.GregorianCalendar.class, new GregorianCalendarConverter());
		register(java.util.Date.class, new DateConverter());
		register(java.sql.Date.class, new SqlDateConverter());
		register(java.sql.Timestamp.class, new SqlTimestampConverter());
	}

	/**
	 * Registers a <code>JdtConvertor</code> for a specific class. Convertor
	 * knows how to read (extract) data from an object, but also how to populate
	 * the same object after it creates a new instance of it.
	 * <p>
	 * 
	 * Because both reading and creating new instance is required by converter,
	 * it is not possible to register a superclass and work with its subclasses.
	 * Instead, all classes that would be used for the time converters must be
	 * registered separately. Example: java.util.Calendar has a subclass
	 * java.util.GregorianCalendar. But it is not possible to have just
	 * converter for Calendar - there must be converter for GregorianCalendar,
	 * too (of course, if it attended to be used).
	 * 
	 * @param c
	 *            class of an object that will be instanced and populated with
	 *            time
	 * @param gtc
	 *            converter
	 */
	public static void register(Class c, JdtConverter gtc) {
		converters.put(c, gtc);
	}

	/**
	 * Loads time from an object by using registered converters.
	 * 
	 * @param o
	 *            object to read time from
	 */
	public void loadFrom(Object o) {
		JdtConverter gtc = (JdtConverter) converters.get(o.getClass());
		if (gtc != null) {
			gtc.load(this, o);
		}
	}

	/**
	 * Stores time to a new instance of desired class, by using converters.
	 * 
	 * @param c
	 *            class of new object
	 * 
	 * @return new instance of specified class, populated with time
	 */
	public Object getInstance(Class c) {
		JdtConverter gtc = (JdtConverter) converters.get(c);
		if (gtc == null) {
			return null;
		}
		return gtc.get(this);
	}

	/**
	 * Shortcut for getInstance() that builds and returns <code>Calendar</code>
	 * instance.
	 * 
	 * @return Calendar instance
	 * @see #getInstance
	 */
	public java.util.Calendar getCalendarInstance() {
		return (java.util.Calendar) getInstance(java.util.Calendar.class);
	}

	/**
	 * Shortcut for getInstance() that builds and returns <code>Date</code>
	 * instance.
	 * 
	 * @return Date instance
	 * @see #getInstance
	 */
	public java.util.Date getDateInstance() {
		return (java.util.Date) getInstance(java.util.Date.class);
	}

	/**
	 * Shortcut for getInstance() that builds and returns
	 * <code>GregorianCalendar</code> instance.
	 * 
	 * @return GregorianCalendar instance
	 * @see #getInstance
	 */
	public java.util.GregorianCalendar getGregorianCalendarInstace() {
		return (java.util.GregorianCalendar) getInstance(java.util.GregorianCalendar.class);
	}

	/**
	 * Shortcut for getInstance() that builds and returns
	 * <code>java.sql.Date</code> instance.
	 * 
	 * @return java.sql.Date instance
	 * @see #getInstance
	 */
	public java.sql.Date getSqlDateInstace() {
		return (java.sql.Date) getInstance(java.sql.Date.class);
	}

	/**
	 * Shortcut for getInstance() that builds and returns
	 * <code>java.sql.Timestamp</code> instance.
	 * 
	 * @return java.sql.Date instance
	 * @see #getInstance
	 */
	public java.sql.Timestamp getSqlTimestampInstance() {
		return (java.sql.Timestamp) getInstance(java.sql.Timestamp.class);
	}

	/**
	 * Shortcut for getInstance() that builds and returns new
	 * <code>JDateTime</code> instance with the date/time set as the original
	 * one.
	 * 
	 * @return new JDateTime instance
	 * @see #getInstance
	 */
	public JDateTime getJDateTimeInstance() {
		return (JDateTime) getInstance(JDateTime.class);
	}

	/**
	 * Stores time to an existing instance of desired class, by using
	 * converters.
	 * 
	 * @param o
	 *            object where to store time info
	 */

	public void storeTo(Object o) {
		JdtConverter gtc = (JdtConverter) converters.get(o.getClass());
		if (gtc != null) {
			gtc.store(this, o);
		}
	}

	/**
	 * Contructs <code>JDateTime</code> from various objects.
	 * 
	 * @param o
	 *            object from where to read date info
	 */
	public JDateTime(Object o) {
		loadFrom(o);
	}

	/**
	 * Contructs <code>JDateTime</code> from <code>DateTimeStamp</code>.
	 * 
	 * @param dts
	 *            date time stamp
	 */
	public JDateTime(DateTimeStamp dts) {
		setDateTimeStamp(dts);
	}

	/**
	 * Contructs <code>JDateTime</code> from <code>JulianDateStamp</code>.
	 * 
	 * @param jds
	 *            date time stamp
	 */
	public JDateTime(JulianDateStamp jds) {
		setJulianDate(jds);
	}

	/**
	 * Sets the new timestamp.
	 * 
	 * @param t
	 * 
	 * @see #setDateTimeStamp
	 * @deprecated use setDateTimeStamp() instead
	 */
	public void setTimeStamp(DateTimeStamp t) {
		set(t.year, t.month, t.day, t.hour, t.minute, t.second);
	}

	/**
	 * Sets the new timestamp.
	 * 
	 * @param t
	 */
	public void setDateTimeStamp(DateTimeStamp t) {
		set(t.year, t.month, t.day, t.hour, t.minute, t.second);
	}

	/**
	 * Returns new object generic time stamp.
	 * 
	 * @return timestamp
	 */
	public DateTimeStamp getDateTimeStamp() {
		return new DateTimeStamp(time.year, time.month, time.day, time.hour, time.minute, time.second);
	}

	// ---------------------------------------------------------------- monthFix

	private static boolean defaultMonFix = true;
	private Boolean monFix = null;

	/**
	 * Sets default month fix value.
	 * 
	 * @param v
	 *            default month fix value
	 */
	public static void setDefaultMonthFix(boolean v) {
		defaultMonFix = v;
	}

	/**
	 * Resets default month fix value to <code>true</code>.

⌨️ 快捷键说明

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