All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.core.util.TimeUtil Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
/*
 * Created on 2005-7-13
 * 
 * 时间工具类.
 */
package com.rt.core.util;

import com.rt.core.constant.RTConst;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 */
class TimeUtil {
	/**
	 * SimpleDateFormat到格式的日期
	 * 
	 * @param formatString
	 * @return SimpleDateFormat
	 */
	private static SimpleDateFormat getDateFormatter(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		// workaround for bug
		formatter.setTimeZone(java.util.TimeZone.getDefault());
		// don't alllow things like 1/35/99
		formatter.setLenient(false);
		return formatter;
	}

	/**
	 * 获得当前时间Calendar格式
	 * 
	 * @param date
	 *            Date
	 * @return Calendar
	 */
	public static Calendar getCalendar(Date date) {
		if (date == null) {
			return null;
		}
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		return now;
	}

	/**
	 * 获得当前时间Calendar格式
	 * 
	 * @param longDate
	 *            longDate
	 * @return Calendar
	 */
	public static Calendar getCalendar(long longDate) {
		Calendar now = Calendar.getInstance();
		now.setTimeInMillis(longDate);
		return now;
	}

	/**
	 * 得到当前时间
	 * 
	 * @return Date
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * 得到指定格式的日期字符串
	 * 
	 * @param format
	 * @return nowDate String.
	 */
	public static String getDateToString(String format) {
		return getDateFormatter(format).format(getDate());
	}

	/**
	 * 得到当前时间的毫秒部分
	 * 
	 * @return String SSS
	 */
	public static String getDateToSecond() {
		return getDateToString(RTConst.SECOND_CODE);
	}

	/**
	 * 转换现在的时间为Long格式 以毫秒为单位计算,从1970年1月1日0时0分0秒0000毫秒算起.
	 * 
	 * @return longDate long
	 */
	public static long getDateTolong() {
		return getDate().getTime();
	}

	/**
	 * @see dateTolong
	 * 
	 * @return longDate Long
	 */
	public static Long getDateToLong() {
		return new Long(getDate().getTime());
	}

	/**
	 * this.dateTolong
	 * 
	 * @return longDate string
	 */
	public static String getDateToString() {
		return String.valueOf(getDate().getTime());
	}

	/**
	 * long转换为日期
	 * 
	 * @param longDate
	 * @return Date
	 */
	public static Date toDate(String longDate) {
		return new Date(RTUtil.tolong(longDate));
	}

	/**
	 * long转换为日期
	 * 
	 * @param longDate
	 * @return Date
	 */
	public static Date toDate(long longDate) {
		return new Date(longDate);
	}

	/**
	 * long转换为日期
	 * 
	 * @param longDate
	 * @return Date
	 */
	public static Date toDate(Long longDate) {
		if (longDate == null) {
			return null;
		}
		return new Date(longDate.longValue());
	}

	/**
	 * String到Date格式的转换
	 * 
	 * @param dateString
	 *            like 1970/01/01
	 * @param formatString
	 * @return date Date
	 */
	public static Date toDate(String date, String format) {
		try {
			return getDateFormatter(format).parse(date);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * date到String格式的转换
	 * 
	 * @param date
	 * @param format
	 * @return date String
	 */
	public static String toStringDate(Date date, String format) {
		return getDateFormatter(format).format(date);
	}

	/**
	 * stringDate转为long型
	 * 
	 * @param stringDate
	 * @param format
	 * @return long
	 */
	public static long tolongDate(String stringDate, String format) {
		Date d = toDate(stringDate, format);
		return d == null ? 0 : d.getTime();
	}

	/**
	 * stringDate转为long型
	 * 
	 * @param stringDate
	 * @param format
	 * @return Long
	 */
	public static Long toLongDate(String stringDate, String format) {
		Date d = toDate(stringDate, format);
		return d == null ? null : new Long(d.getTime());
	}

	/**
	 * long or integer型转换成date string
	 * 
	 * @param longDate
	 * @param format
	 * @return StringDate
	 */
	public static String toStringDate(String longDate, String format) {
		return toStringDate(toDate(RTUtil.tolong(longDate)), format);
	}

	/**
	 * long or integer型转换成date string
	 * 
	 * @param longDate
	 * @param format
	 * @return StringDate
	 */
	public static String toStringDate(long longDate, String format) {
		return toStringDate(toDate(longDate), format);
	}

	/**
	 * long or integer型转换成date string
	 * 
	 * @param longDate
	 * @param format
	 * @return StringDate
	 */
	public static String toStringDate(Long longDate, String format) {
		if (longDate == null) {
			return null;
		}
		return toStringDate(toDate(longDate), format);
	}

	/**
	 * 取得某月最后一天的日期整数值
	 * 
	 * @param year
	 *            年(yyyy)
	 * @param month
	 *            月(mm或m)
	 * @return int 日期整数值
	 */
	public static int getMaxDayOfMonth(String year, String month) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(year));
		c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 按生日转换成年龄
	 * 
	 * @param birthday
	 * @return int
	 */
	public static int getAgeOfBirthday(String birthday) {
		if (RTUtil.isEmpty(birthday)) {
			return 0;
		}
		return getAgeOfBirthday(toDate(birthday, "yyyy-MM-dd"));
	}

	/**
	 * 按生日转换成年龄
	 * 
	 * @param birthday
	 * @return int
	 */
	public static int getAgeOfBirthday(Date birthday) {
		Calendar brith = getCalendar(birthday);
		Calendar now = getCalendar(getDate());
		int age = now.get(Calendar.YEAR) - brith.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) - brith.get(Calendar.MONTH);
		int day = now.get(Calendar.DATE) - brith.get(Calendar.DATE);
		if (day < 0) {
			month--;
		}
		if (month < 0) {
			age--;
		}
		return age;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy