张晨的个人博客

Java处理Date时间格式的各种场景工具类(二)

张晨的个人博客2014-05-30Java技术 2624 0A+A-

public class DateUtil {
public enum DateType {
	YEAR, MONTH, DAY, HH, MI, SS, YYYY_MM_DD, YYYYMMDD
}



/**
 * 将精确到秒的字符串转换为日期类型
 * <p>
 * 字符串格式需要为yyyy-MM-dd hh:mm:ss
 * 
 * @param date
 *            将精确到秒的字符串
 * @return Timestamp对象
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static Date string2Timestamp(String date) {
	SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	try {
		clsFormat.parse(date);
		return new Timestamp(clsFormat.parse(date).getTime());
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将精确到秒的字符串转换为日期类型
 * <p>
 * 字符串格式需要为yyyy-MM-dd hh:mm:ss
 * 
 * @param date
 *            将精确到秒的字符串
 * @return Timestamp对象
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static Timestamp str2Timestamp(String date, String pattern) {
	SimpleDateFormat clsFormat = new SimpleDateFormat(pattern);
	try {
		clsFormat.parse(date);
		return new Timestamp(clsFormat.parse(date).getTime());
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将精确到天的字符串转换为日期类型
 * <p>
 * 字符串格式需要为yyyy-MM-dd
 * 
 * @param date
 *            将精确到天的字符串
 * @return java.sql.Date对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.sql.Date string2Date(String date) {
	SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd");
	try {
		if(date==""||"".equals(date)){
			return null;
		}else{
			return new java.sql.Date(clsFormat.parse(date).getTime());
		}
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将日期类型字符串转换为日期类型
 * <p>
 * 
 * @param date  日期类型字符串
 * @return java.sql.Date对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.sql.Date string2Date(Object date) {
	if (date instanceof java.util.Date)
		return new java.sql.Date(((java.util.Date) date).getTime());
	String strDate = date.toString();
	if (StringUtil.isNull(strDate) || (strDate.length() < 5))
		throw new RuntimeException("非日期类型");
	String dateSpace = "";
	if (strDate.contains("-") || strDate.contains("/")) {
		dateSpace = String.valueOf(strDate.charAt(4));
	}
	String format[];
	if (!StringUtil.isNull(dateSpace)) {
		format = new String[6];
		format[0] = "yyyy" + dateSpace + "MM" + dateSpace + "dd";
		format[1] = "yyyy" + dateSpace + "MM";
		format[2] = "yyyy" + dateSpace + "MM" + dateSpace + "d";
		format[3] = "yyyy" + dateSpace + "M" + dateSpace + "dd";
		format[4] = "yyyy" + dateSpace + "M" + dateSpace + "d";
		format[5] = "yyyy" + dateSpace + "M";
	} else {
		format = new String[5];
		format[0] = "yyyyMMdd";
		format[1] = "yyyyMM";
		format[2] = "yyyyMdd";
		format[3] = "yyyyMd";
		format[4] = "yyyyM";
	}
	SimpleDateFormat formatter;
	String aFormat = "", tmp = "";
	;
	Date frmDate = null;
	for (int i = 0; i < format.length; i++) {
		aFormat = format[i];
		formatter = new SimpleDateFormat(aFormat);
		formatter.setLenient(false);

		try {
			frmDate = formatter.parse(strDate);
			tmp = formatter.format(frmDate);
		} catch (ParseException e) {
		}
		if (strDate.equals(tmp)) {
			return new java.sql.Date(frmDate.getTime());
		}
	}
	throw new RuntimeException("非日期类型");
}

/**
 * 将精确到月的字符串转换为日期类型
 * <p>
 * 字符串格式需要为yyyy-MM
 * 
 * @param date
 *            将精确到月的字符串
 * @return java.sql.Date对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.sql.Date string4Date(String date) {
	SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM");
	try {
		if(date==""||"".equals(date)){
			return null;
		}else{
		return new java.sql.Date(clsFormat.parse(date).getTime());
		}
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将精确到天的字符串转换为日期类型
 * <p>
 * 字符串格式需要为yyyy-MM-dd HH:mm
 * 
 * @param date
 *            将精确到分钟的的字符串
 * @return java.util.Date对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.util.Date string3Date(String date) {
SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	try {
		if(date==""||"".equals(date)){
			return null;
		}else{
			return  clsFormat.parse(date);
		}
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将字符型时间转换为Time型
 * <p>
 * 字符串格式需要为HH:mm:ss
 * 
 * @param date
 *            将精确到天的字符串
 * @return Timestamp对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.sql.Time string2Time(String time) {
	SimpleDateFormat clsFormat = new SimpleDateFormat("HH:mm:ss");
	try {
		return new java.sql.Time(clsFormat.parse(time).getTime());
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

// 将精确到天的字符串对象转换为日期类型
public static java.sql.Date object2Date(Object date) {
	return string2Date((String) date);
}

/**
 * 将字符串转为指定格式的日期
 * <p>
 * 日期字符串与字符串格式需要匹配
 * 
 * @param date
 *            日期字符串
 * @param frm
 *            字符串格式
 * @return Timestamp对象
 * @throws ParseException
 * @throws RuntimeException
 *             日期类型转换错误
 */
public static java.sql.Date string2Date(String date, String frm) {
	SimpleDateFormat clsFormat = new SimpleDateFormat(frm);
	try {
		clsFormat.parse(date);
		return new java.sql.Date(clsFormat.parse(date).getTime());
	} catch (ParseException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	}
}

/**
 * 将日期类型转换为精确到天的字符串
 * <p>
 * 字符串格式为yyyy-MM-dd
 * 
 * @param date
 *            日期类型
 * @return String 日期
 * 
 */
public static String date2String(Date date) {
	SimpleDateFormat clsFormat = new SimpleDateFormat("yyyy-MM-dd");
	return clsFormat.format(date);
}

/**
 * 将日期类型转换为指定格式的字符串
 * 
 * 
 * @param date
 *            日期类型
 * @param frm
 *            需要转换的格式,具体格式参加java说明
 * @return String 日期
 * 
 */
public static String date2String(Date date, String frm) {
	SimpleDateFormat clsFormat = new SimpleDateFormat(frm);
	return clsFormat.format(date);
}

/** 
* 将日期类型转换为指定格式的日期类型
* @param date
* 			日期类型
* @param frm
* 			 需要转换的格式,具体格式参加java说明
* @return Date
* @description:
*/
public static java.sql.Date date2date(Date date,String frm){
	return string2Date(date2String(date), frm);
}

/** 
* 将日期类型转换为yyyy-MM-dd格式的日期类型
* @param date
* 			日期类型
* @return Date
* @description:
*/
public static java.sql.Date date2date(Date date){
	return string2Date(date2String(date));
}

}

 

文章关键词
Java
Date
Date时间
发表评论