datetimeutils.java

来自「本文论述了一个前台笔记本销售系统的开发过程」· Java 代码 · 共 783 行 · 第 1/2 页

JAVA
783
字号
	 */
	public static long NULL_TIME = (long) 18000000;

	/**
	 * Timestamp representing "January 1, 1970 00:00:00".
	 */
	public static Timestamp NULL_TIMESTAMP = new Timestamp(NULL_TIME);

	/**
	 * Timestamp representing "December 31, 2029, 23:59:59.9"
	 */
	public static Timestamp MAX_TIMESTAMP = Timestamp
			.valueOf("2029-12-31 23:59:59.999");

	/**
	 * Return the String representing "January 1, 1970 00:00:00".
	 */
	public static String getTimestampDisplayNull() {
		return NULL_TIMESTAMP_DISPLAY;
	}

	/**
	 * Return the String representing the current timestamp;
	 */
	public static String getTimestampDisplay() {
		return getTimestamp(new Timestamp(System.currentTimeMillis()));
	}

	/**
	 * @deprecated Use getTimestampDisplay.
	 */
	public static String getTimestampText() {
		return getTimestampDisplay();
	}

	/**
	 * Return null if timestamp is null or equals "January 1, 1970 00:00:00".
	 */
	public static boolean isNull(Timestamp timestamp) {
		return ((timestamp == null) || (timestamp.getTime() == NULL_TIME));
	}

	/**
	 * Factory method to return timestamp initialized to current system time.
	 * For timestamp as a String in the default format, use
	 * <code>getTimestamp().toString()</code>.
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * Convert timestamp to String for given locale in given style. A null
	 * locale will return the default locale.
	 */
	public static String getTimestamp(Timestamp timestamp, Locale locale,
			int style) {
		Date date = (Date) timestamp;
		if (locale == null) {
			return (DateFormat.getDateTimeInstance(style, style).format(date));
		}
		return (DateFormat.getDateTimeInstance(style, style, locale)
				.format(date));
	}

	/**
	 * Convert date to String for default locale in given style. A null locale
	 * will return the default locale.
	 */
	public static String getTimestamp(Timestamp timestamp, int style) {
		return getTimestamp(timestamp, (Locale) null, style);
	}

	/**
	 * Convert date to String for default locale in DEFAULT style. A null locale
	 * will return the default locale.
	 */
	public static String getTimestamp(Timestamp timestamp) {
		return getTimestamp(timestamp, (Locale) null, DEFAULT);
	}

	/**
	 * Return Timestamp value using a String. Null or conversion error returns
	 * null.
	 * 
	 * @param String
	 *            representing Timestamp
	 */
	public static Timestamp toTimestamp(String string) {
		if (string == null) {
			return null;
		} else {
			try {
				return Timestamp.valueOf(string);
			} catch (Throwable t) {
				return null;
			}
		}
	}

	/**
	 * Return a Timestamp based on the parameters. Any nulls or conversion error
	 * returns null.
	 * 
	 * @param year
	 *            The year
	 * @param month
	 *            The month
	 * @param day
	 *            The day
	 * @returns Timestamp for year-month-day
	 */
	public static Timestamp toTimestamp(String year, String month, String day) {

		if ((null == year) || (null == month) || (null == day)) {
			return null;
		}

		StringBuffer sb = new StringBuffer();
		// YEAR-MM-DD 00:00:00.0
		sb.append(year);
		sb.append(DATE_SEPARATOR);
		sb.append(month);
		sb.append(DATE_SEPARATOR);
		sb.append(day);

		sb.append(TIMESTAMP_TIME_ZERO);

		return toTimestamp(sb.toString());
	}

	/**
	 * Return String value representing Timestamp. Null returns null.
	 * 
	 * @param Timestamp
	 */
	public static String toString(Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		} else {
			return timestamp.toString();
		}
	}

	// 当前日期加减n天后的日期,返回String (yyyy-mm-dd)
	public static String nDaysAftertoday(int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add(Calendar.DAY_OF_MONTH, +n);
		return df.format(rightNow.getTime());
	}

	// 当前日期加减n天后的日期,返回Date
	public static Date nDaysAfterNowDate(int n) {
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add(Calendar.DAY_OF_MONTH, +n);
		return rightNow.getTime();
	}

	// 给定一个日期型字符串,返回加减n天后的日期型字符串
	public static String nDaysAfterOneDateString(String basicDate, int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date tmpDate = null;
		try {
			tmpDate = df.parse(basicDate);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}
		long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
				* (24 * 60 * 60 * 1000);
		tmpDate.setTime(nDay);

		return df.format(tmpDate);
	}

	// 给定一个日期,返回加减n天后的日期,返回Date
	public static Date nDaysAfterOneDate(Date basicDate, int n) {
		long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n)
				* (24 * 60 * 60 * 1000);
		basicDate.setTime(nDay);

		return basicDate;
	}

	// 当前日期加减n个月后的日期,返回String (yyyy-mm-dd)
	public static String nMonthsAftertoday(int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add(Calendar.MONTH, +n);
		return df.format(rightNow.getTime());
	}

	// 当前日期加减n个月后的日期,返回Date
	public static Date nMonthsAfterNowDate(int n) {
		Calendar rightNow = Calendar.getInstance();
		// rightNow.add(Calendar.DAY_OF_MONTH,-1);
		rightNow.add(Calendar.MONTH, +n);
		return rightNow.getTime();
	}

	// 当前日期加减n个月后的日期,返回Date
	public static Date nMonthsAfterOneDate(Date basicDate, int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(basicDate);
		rightNow.add(Calendar.MONTH, +n);
		return rightNow.getTime();
	}

	// 当前日期加减n个月后的日期,返回String
	public static String nMonthsAfterOneDateString(Date basicDate, int n) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(basicDate);
		rightNow.add(Calendar.MONTH, +n);
		return df.format(rightNow.getTime());
	}

	// 计算两个日期相隔的天数
	public static int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	// 计算两个日期相隔的天数
	public static int nDaysBetweenTwoDate(String firstString,
			String secondString) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date firstDate = null;
		Date secondDate = null;
		try {
			firstDate = df.parse(firstString);
			secondDate = df.parse(secondString);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}

		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	// 得到给定日期是星期几(从星期天开始,1-7)
	public static int getWeekNumOfDate(Date date) {
		GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
				.getInstance();
		calendar.setTime(date);
		return calendar.get(GregorianCalendar.DAY_OF_WEEK);
	}

	public static int getWeekNumOfDate(String date) {
		GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
				.getInstance();
		calendar.setTime(DateTimeUtils.toDate(date));
		return calendar.get(GregorianCalendar.DAY_OF_WEEK);
	}

	// 得到给定日期所在星期的某一天的日期
	// weekDay: from sunday to SATURDAY,1 to 7
	public static Date getWeekDayDate(int weekDay, Date date) {
		Calendar c = Calendar.getInstance();

		// modify on 2007-7-24 by zhangjun
		if (date != null) {
			c.setTime(date);
			if (weekDay >= Calendar.SUNDAY && weekDay <= Calendar.SATURDAY) {
				int delta = weekDay - c.get(Calendar.DAY_OF_WEEK);
				c.add(Calendar.DAY_OF_WEEK, delta);
			}
		}

		return new Date(c.getTimeInMillis());
	}

	public static GregorianCalendar getnDayDiff(GregorianCalendar cal, int n) {
		GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
		g1.add(Calendar.DAY_OF_MONTH, +n);
		return g1;
	}

	public static GregorianCalendar getnMonthDiff(GregorianCalendar cal, int n) {
		GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
		g1.add(Calendar.MONTH, +n);
		return g1;
	}

	public static GregorianCalendar getnYearDiff(GregorianCalendar cal, int n) {
		GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
		g1.add(Calendar.YEAR, +n);
		return g1;
	}

	public static String long2DateString(String longVal) {
		if (longVal.equals("")) {
			return "";
		}
		if (Long.parseLong(longVal) <= 0) {
			return "";
		}
		return dateToShortString(long2Date(longVal));
	}

	public static String long2DateString(String longVal, String format) {
		if (Long.parseLong(longVal) <= 0) {
			return "";
		}
		return dateToShortString(long2Date(longVal), format);
	}

	/**
	 * 取得间隔的天数
	 * 
	 * @param begin
	 *            long
	 * @param end
	 *            long
	 * @return int
	 */
	public static int getDayDiff(long begin, long end) {
		long interval = end - begin;
		if (interval > 0) {
			return (int) (interval / 3600 / 1000 / 24);
		}
		return 0;
	}

	public static int getDayDiff(String beginStr, String endStr) {
		try {
			long begin = Long.parseLong(beginStr);
			long end = Long.parseLong(endStr);
			return getDayDiff(begin, end);
		} catch (Exception e) {

		}
		return 0;
	}

	/**
	 * getMonthDiff 返回相差的月数(每月以30天计)
	 * 
	 * @param begin
	 *            long
	 * @param end
	 *            long
	 * @return int
	 */
	public static int getMonthDiff(long begin, long end) {
		long dec = end - begin;
		if (dec > 0) {
			return (int) (dec / (30 * 24 * 3600 * 1000L));
		}
		return 0;
	}

	/**
	 * getMonthDiff 返回相差的月数(每月以30天计)
	 * 
	 * @param begin
	 *            String
	 * @param end
	 *            String
	 * @return int
	 */
	public static int getMonthDiff(String begin, String end) {
		try {
			long beginLong = Long.parseLong(begin);
			long endLong = Long.parseLong(end);
			return getMonthDiff(beginLong, endLong);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static String long2DateLangString(String longVal) {
		if (longVal.equals("")) {
			return "";
		}
		if (Long.parseLong(longVal) <= 0) {
			return "";
		}
		return dateToLangString(long2Date(longVal));
	}

}

⌨️ 快捷键说明

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