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

📄 jdatetime.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * 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>.
	 */
	public static void resetDefaultMonthFix() {
		defaultMonFix = true;
	}

	/**
	 * Sets month fix value.
	 *
	 * @param v      month fix value
	 */
	public void setMonthFix(boolean v) {
		monFix = new Boolean(v);
	}

	/**
	 * Resets month fix value to default one.
	 */
	public void resetMonthFix() {
		monFix = null;
	}

	/**
	 * Returns actual mont fix value.
	 */
	public boolean getMonthFix() {
		if (monFix != null) {
			return monFix.booleanValue();
		}
		return defaultMonFix;
	}


	// ---------------------------------------------------------------- names

	private static JdtNames defaultNames = new EnglishNames();
	private JdtNames names = null;

	/**
	 * Sets default JDateTime names.
	 *
	 * @param newNames names
	 */
	public static void setDefaultNames(JdtNames newNames) {
		if (newNames != null) {
			defaultNames = newNames;
		}
		return;
	}

	/**
	 * Resets default date time names to English.
	 */
	public static void resetDefaultNames() {
		defaultNames = new EnglishNames();
	}

	/**
	 * Sets current date time names.
	 *
	 * @param newNames
	 */
	public void setNames(JdtNames newNames) {
		if (newNames != null) {
			names = newNames;
		}
		return;
	}

	/**
	 * Reset current names to default values.
	 */
	public void resetNames() {
		names = null;
	}

	/**
	 * Returns actual date time names.
	 *
	 * @return actual date time names.
	 */
	public JdtNames getNames() {
		if (names != null) {
			return names;
		}
		return defaultNames;
	}

	// ---------------------------------------------------------------- formats

	private static final String DEFAULT_FORMAT_TEMPLATE = "YYYY-MM-DD hh:mm:ss.mss";

	private static String defaultFormatTemplate = DEFAULT_FORMAT_TEMPLATE;

	private String formatTemplate = null;

	/**
	 * Sets default format template.
	 *
	 * @param newFormatTemplate
	 */
	public static void setDefaultFormatTemplate(String newFormatTemplate) {
		if (newFormatTemplate != null) {
			defaultFormatTemplate = newFormatTemplate;
		}
		return;
	}

	/**
	 * Resets default format to JDateTime defaults format: YYYY-MM-DD hh:mm:s.m
	 */
	public static void resetDefaultFormatTemplate() {
		defaultFormatTemplate = DEFAULT_FORMAT_TEMPLATE;
		return;
	}

	/**
	 * Sets current format template.
	 *
	 * @param newFormat
	 */
	public void setFormatTemplate(String newFormat) {
		if (newFormat != null) {
			formatTemplate = newFormat;
		}
		return;
	}

	/**
	 * Resets current format template to default value.
	 */
	public void resetFormatTemplate() {
		formatTemplate = null;
	}

	/**
	 * Returns actual format template.
	 *
	 * @return actual format template
	 */
	public String getFormatTemplate() {
		if (formatTemplate != null) {
			return formatTemplate;
		}
		return defaultFormatTemplate;
	}


	// ---------------------------------------------------------------- formatters
	
	private static JdtFormatter formatter = new DefaultFormatter();

	/**
	 * Sets default formatter
	 *
	 * @param f      formatter instance
	 */
	public static void setDefaultFormatter(JdtFormatter f) {
		if (f != null) {
			formatter = f;
		}
	}

	// ---------------------------------------------------------------- formatters usage (String conversions)

	/**
	 * Get current date/time in specified format.
	 *
	 * @param template format template
	 *
	 * @return current date/time string
	 */
	public String get(String template) {
		return formatter.get(this, template);
	}

	/**
	 * Get current date/time in default format.
	 *
	 * @return current date/time string
	 */
	public String get() {
		return get(getFormatTemplate());
	}


	/**
	 * Sets date/time from a string and specified template.
	 *
	 * @param s        string containing date time information
	 * @param template format template
	 */
	public void set(String s, String template) {
		DateTimeStamp dts = formatter.set(s, template);
		if (dts != null) {
			setDateTimeStamp(dts);
		}
	}

	/**
	 * Sets date/time from a string and default template.
	 *
	 * @param s        string containing date time information
	 */
	public void set(String s) {
		set(s, getFormatTemplate());
	}
	

	// ---------------------------------------------------------------- week definitions

	private static int defaultFirstDayOfWeek = 1;
	
	private int firstDayOfWeek = 0;

	private static int defaultMustHaveDayOfFirstWeek = 4;

	private int mustHaveDayOfFirstWeek = 0;

	/**
	 * Defines default week. Not valid values are ignored and may be used for
	 * individual settings of each of 2 input parameters.
	 *
	 * @param start  first day in week
	 * @param must   must have day of the 1st week
	 */
	public static void setDefaultWeekDefinition(int start, int must) {
		if ((start >= 1) && (start <= 7)) {
			defaultFirstDayOfWeek = start;
		}
		if ((must >= 1) && (must <= 7)) {
			defaultMustHaveDayOfFirstWeek = must;
			defaultMinimalDaysInFirstWeek = convertMin2Must(defaultFirstDayOfWeek, must);
		}
	}

	/**
	 * Defines week.
	 *
	 * @param start  first day in week
	 * @param must   must have day of the 1st week
	 */
	public void setWeekDefinition(int start, int must) {
		if ((start >= 1) && (start <= 7)) {
			firstDayOfWeek = start;
		}
		if ((must >= 1) && (must <= 7)) {
			mustHaveDayOfFirstWeek = must;
			minimalDaysInFirstWeek = convertMin2Must(getFirstDayOfWeek(), must);
		}
	}

	/**
	 * Resets default week definition.
	 */
	public static void resetDefaultWeekDefinition() {
		defaultFirstDayOfWeek = 1;
		defaultMustHaveDayOfFirstWeek = 4;
	}

	/**
	 * Resets week definition.
	 */
	public void resetWeekDefintion() {
		firstDayOfWeek = 0;
		mustHaveDayOfFirstWeek = 0;
	}


	/**
	 * Returns the first day of the week.
	 *
	 * @return first day of week
	 */
	public int getFirstDayOfWeek() {
		if (firstDayOfWeek != 0) {
			return firstDayOfWeek;
		}
		return defaultFirstDayOfWeek;
	}

	/**
	 * Returns must have day of the 1st week.
	 *
	 * @return must have day of the first week
	 */
	public int getMustHaveDayOfFirstWeek() {
		if (mustHaveDayOfFirstWeek != 0) {
			return mustHaveDayOfFirstWeek;
		}
		return defaultMustHaveDayOfFirstWeek;
	}


	// ---------------------------------------------------------------- week definitions (alt)

	private static int defaultMinimalDaysInFirstWeek = 4;

	private int minimalDaysInFirstWeek = 0;

	/**
	 * Returns minimal number of days of the first week. It is calculated from
	 * must have day of the first week.
	 *
	 * @return minimal number of days of the first week
	 */
	public int getMinimalDaysInFirstWeek() {
		if (minimalDaysInFirstWeek != 0) {
			return minimalDaysInFirstWeek;
		}
		return defaultMinimalDaysInFirstWeek;
	}


	/**
	 * Defines default week alternatively.
	 *
	 * @param start  first day in week
	 * @param min    minimal days of week
	 */
	public static void setDefaultWeekDefinitionAlt(int start, int min) {
		if ((start >= 1) && (start <= 7)) {
			defaultFirstDayOfWeek = start;
		}
		if ((min >= 1) && (min <= 7)) {
			defaultMustHaveDayOfFirstWeek = convertMin2Must(defaultFirstDayOfWeek, min);
			defaultMinimalDaysInFirstWeek = min;
		}
	}

	/**
	 * Defines week alternatively.
	 *
	 * @param start  first day in week
	 * @param min    minimal days of week
	 */

	public void setWeekDefinitionAlt(int start, int min) {
		if ((start >= 1) && (start <= 7)) {
			firstDayOfWeek = start;
		}
		if ((min >= 1) && (min <= 7)) {
			mustHaveDayOfFirstWeek = convertMin2Must(getFirstDayOfWeek(), min);
			minimalDaysInFirstWeek = min;
		}
	}

	/**
	 * Converts minimal day of week to must have day of week.
	 * Method is symmetrical.
	 *
	 * @param start  first day of week
	 * @param min    minimal day of week
	 *
	 * @return must have day of week
	 */
	private static int convertMin2Must(int start, int min) {
		int must = 8 - min + (start - 1);
		if (must > 7) {
			must -= 7;
		}
		return must;
	}

	// ---------------------------------------------------------------- isValid


	/**
	 * Checks if some string represents a valid date. It uses
	 * <code>JDateTime</code>'s default format template.
	 *
	 * @param s      string
	 *
	 * @return true if date is valid, otherwise false
	 */
	public static boolean isValid(String s) {
		return isValid(s, defaultFormatTemplate);
	}

	/**
	 * Checks if some string represents a valid date.
	 *
	 * @param s        string
	 * @param template template
	 *
	 * @return true if date is valid, otherwise false
	 */
	public static boolean isValid(String s, String template) {
		DateTimeStamp dtsOriginal = formatter.set(s, template);
		if (dtsOriginal == null) {
			return false;
		}
		return TimeUtil.isValidDateTime(dtsOriginal);
		//JDateTime jdt = new JDateTime(dtsOriginal);
		//DateTimeStamp dtsMatch = jdt.getDateTimeStamp();
		//return (dtsOriginal.compareTo(dtsMatch) == 0);
	}

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

	/**
	 * Returns date time string in YYYY-MM-DD hh:mm:s.m format.
	 *
	 * @return date time string in default format
	 * @see #get
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer(25);
		sb.append(time.year).append('-');
		if (time.month < 10) sb.append('0');
		sb.append(time.month).append('-');
		if (time.day < 10) sb.append('0');
		sb.append(time.day).append(' ');
		if (time.hour < 10) sb.append('0');
		sb.append(time.hour).append(':');
		if (time.minute < 10) sb.append('0');
		sb.append(time.minute).append(':');
		if (time.second < 10) sb.append('0');
		sb.append((int)time.second).append('.');
		int milis = getMillisecond();
		if (milis < 10) sb.append('0');
		if (milis < 100) sb.append('0');
		sb.append(milis);
		return sb.toString();
	}

}

⌨️ 快捷键说明

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