现在的位置: 首页 > 综合 > 正文

java转换日期的工具类

2014年02月08日 ⁄ 综合 ⁄ 共 3015字 ⁄ 字号 评论关闭
package com.lingan.common;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtils {
	private static Log log = LogFactory.getLog(DateUtils.class);
	public final static SimpleDateFormat dateMillTimeFormat = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");
	public final static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss.SSS");
	public final static java.util.regex.Pattern pattern = Pattern
			.compile("^[0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2}( +[0-9]{2}(:[0-9]{2}(:[0-9]{2}\\.?[0-9]{0,3})?)?)?$");

	public synchronized static Date toDate(String date) {
		Date d = null;
		// 20110201==>yyyyMMdd
		// 2011/01/01==>yyyy/MM/dd
		// 2011-01-01==>yyyy-MM-dd
		try {
			if (!pattern.matcher(date).matches()) {
				throw new Exception(date+" is not a date format.");
			}
			String rdate = date.replaceAll("[/ :.-]", "");
			if (rdate.length() < 8) {
				throw new Exception();
			} else {
				while (rdate.length() < 17) {
					rdate += "0";
				}
			}
			d = dateMillTimeFormat.parse(rdate);
		} catch (Exception e) {
			log.error(date + " 日期格式錯誤.");
		}
		return d;
	}

	public static Date toDate(Timestamp date) {
		return date;
	}

	public static Date toDate(Object date) {
		Date d = null;
		if (date instanceof Date) {
			d = (Date) date;
		} else if (date instanceof String) {
			d = toDate((String) date);
		}
		return d;
	}
	public static Timestamp toTimeStamp(Object obj){
		if (obj==null || StringUtils.isBlank(obj.toString())){
			return null;
		}
		Date date = toDate(obj);
		return new Timestamp(date.getTime());
	}
	public synchronized static String format(Date date) {
		return dateTimeFormat.format(date);
	}

	public synchronized static String format(Timestamp date) {
		return dateTimeFormat.format(date);
	}
	public static String format(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public synchronized static String format(Object date) {
		String formatDate = null;
		if (date instanceof Date) {
			formatDate = dateTimeFormat.format((Date) date);
		}
		return formatDate;
	}

	public static void main(String[] args) {
		System.out.println("1:" + DateUtils.toDate("2011"));
		System.out.println("2:" + DateUtils.toDate("2011/12/20"));
		System.out.println("3:" + DateUtils.toDate("2011/12/20 17:20:30"));
		System.out.println("4:" + DateUtils.toDate("2011/12/20 17:20"));
		System.out.println("5:"
				+ DateUtils.toDate(new Timestamp(Calendar.getInstance()
						.getTime().getTime())));
		System.out.println("6:"
				+ DateUtils.format(new Timestamp(Calendar.getInstance()
						.getTime().getTime())));

		System.out.println("10:" + DateUtils.toDate("2010/12/01"));
		System.out.println("11:" + DateUtils.toDate("2011-01-01"));
		System.out.println("12:" + DateUtils.toDate("2010-10-11 15:20:15"));
		System.out.println("13:" + DateUtils.toDate("2010-10-11 15:20"));
		System.out.println("14:" + DateUtils.toDate("2010-10-11 15"));
		System.out.println("15:" + DateUtils.toDate("2010-10-11 15:20:15.235"));
		System.out.println("16:" + DateUtils.toDate("2010-10-11 15:20:15"));
		System.out.println("17:" + DateUtils.toDate("2010-10-11 15:20:15"));
		System.out.println("18:" + DateUtils.toDate("15:12:30"));
		System.out.println("19:" + DateUtils.toDate("15:12:30.563"));
	}
}

这个还是从项目中拿过来,自己保存以后备用。

抱歉!评论已关闭.