日期转换工具集

package com.sinotrans.filesystem.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtil {
	private static java.text.SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	public static String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 格式化时间 返回yyyy-MM-dd HH:mm:ss
	 */
	public static String parseDateToString(Date date) {
		try {
			return dateformat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获得系统时间
	 * 
	 * @return
	 */
	public static String getSysDateTime() {
		return dateformat.format(new Date());
	}

	/**
	 * "yyyy-MM-dd"格式的字符串转化为时间
	 * 
	 * @param str
	 * @return
	 */
	public static Calendar str2Date(String str) {
		java.util.Calendar ca = java.util.Calendar.getInstance();
		int year = Integer.parseInt(str.substring(0, 4));
		int month = Integer.parseInt(str.substring(5, 7));
		int day = Integer.parseInt(str.substring(8, 10));
		ca.set(year, month - 1, day);
		return ca;
	}

	/**
	 * @description 将格式为yyyy-MM-dd HH:mm:ss的字符串转为Date
	 * @param dateTimeString
	 * @return Date
	 * @throws ParseException
	 */
	public static Date str2DateTime(String dateTimeString)
			throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = format.parse(dateTimeString);
		return date;
	}

	/** 获取系统当前时间 **/
	public static long getTime() {
		java.util.Date dt = new java.util.Date();
		return dt.getTime();
	}

	/** 获取系统当前日期 得到的日期格式如:2004-10-09 **/
	public static java.sql.Date getSqlDate() {
		return new java.sql.Date(getTime());
	}

	/**
	 * 获得指定时间是星期几
	 * 
	 * @param dt
	 * @return
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 当前时间推后几天
	 * 
	 * @param day
	 * @return
	 */
	public static String addDay(int day) {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int date = cal.get(Calendar.DATE);

		cal.set(year, month, date + day);
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String result = sdf.format(cal.getTime());
		return result;

	}

	/**
	 * 两个Str的时间比较先后
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean before(String date1, String date2) {
		Calendar datef = str2Date(date1);
		Calendar dates = str2Date(date2);
		return datef.before(dates);
	}

	/**
	 * 把yyyy-MM-dd格式的字符串转换成Timestamp
	 * 
	 * @param dateStr
	 * @return Timestamp
	 */
	public final static Timestamp getTimeOfDateStr(String dateStr) {
		DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.sql.Timestamp time = null;
		try {
			java.util.Date da = df.parse(dateStr);
			time = new java.sql.Timestamp(da.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;

	}

	public final static String dateToString(Date date) {
		if (date == null) {
			date = new Date();
		}
		DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmm");
		try {
			return df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";

	}

	public final static String dateToyyyyMMddHHmmss(Date date) {
		if (date == null) {
			date = new Date();
		}
		DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
		try {
			return df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";

	}

	/**
	 * @Title 时间字符串[格式必须 为: yyyy-mm-dd hh:mm:ss]转 Timestamp
	 * @Description 时间字符串[格式必须 为: yyyy-mm-dd hh:mm:ss]转 Timestamp
	 * @param datestr
	 *            时间字符串
	 * @param date_format
	 *            时间字符串格式
	 * @return Timestamp 返回TimeStamp类型的时间
	 */
	public static Timestamp dateStrToTime(String datestr) {
		return Timestamp.valueOf(datestr);
	}

	/**
	 * @Title Date转 Timestamp
	 * @Description Date转 Timestamp
	 * @param date
	 *            Date 时间
	 * @return Timestamp 返回TimeStamp类型的时间
	 */
	public static Timestamp dateToTime(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * @Title Timestamp转 String
	 * @Description Timestamp转 String
	 * @param timestamp
	 *            Timestamp 时间
	 * @return Timestamp 返回String类型的时间 [格式为 yyyy-mm-dd hh:mm:ss]
	 */
	public static String timestampToDateStr(Timestamp timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_YMDHMS);
		return sdf.format(timestamp);
	}

	/**
	 * @Title Timestamp转 Date
	 * @Description Timestamp转 Date
	 * @param date
	 *            Date 时间
	 * @return Timestamp 返回Date类型的时间
	 */
	public static Date timeStampToDate(Timestamp timestamp) {
		Date date = new Date();
		date = timestamp;
		return date;
	}

	/**
	 * 
	 * 
	 * @Title 获得当前的系统时间
	 * @Description
	 * @param
	 * @return
	 * @throws
	 */
	public static String getSystemTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_YMDHMS);
		return sdf.format(new Date());
	}

	/**
	 * 获取当前时间+或-的时间
	 * 
	 * @author wzc([email protected])
	 * @date Mar 6, 2015 7:49:44 PM
	 * @param scale
	 * @param unit
	 * @return Date
	 */
	public static Date setDate(Calendar c, int scale, int unit) {
		c.add(unit, scale);
		return c.getTime();
	}

	public static Date setDate(int scale, int unit) {
		Calendar c = Calendar.getInstance();
		return setDate(c, scale, unit);
	}

	/**
	 * @description 格式化时间 返回yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @return String
	 */
	public static String dateToStr(Date date) {
		try {
			return timestampToDateStr(dateToTime(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 增加或減少天数
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static Date addDay(Date date, int num) {
		Calendar startDate = Calendar.getInstance();
		startDate.setTime(date);
		startDate.add(Calendar.DAY_OF_MONTH, num);
		return startDate.getTime();
	}

}

猜你喜欢

转载自blog.csdn.net/u014505277/article/details/50629740