时间工具类 #未整理
/**
* All copyright reserved by NewHeight LTD.
*
*/
package com.yihaodian.gos.create.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
/**
* DateTime 工具类
*
* @version 0.1, 2012-7-24
* @since JDK1.6
*/
public class DateUtils
{
private static final Logger log = Logger.getLogger(DateUtils.class);
public static final int NEXT_DAY = 5; //added by bill 2010-07-20,@see #getNextDay,取代硬编码5天
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String PERIOD_DATE_FORMAT = "yyyyMMddHHmm";
public static final String DO_DATE_FORMAT = "yyyyMMdd";
/**
* 获取InvokeTimePeriod
* @return
*/
public static Long getInvokeTimePeriod(Date date){
return Long.parseLong(new SimpleDateFormat(PERIOD_DATE_FORMAT).format(date));
}
/**
*
* @Title: subDays
* @Description: 返回date1和date2之间相差的天数
* @param date1
* @param date2
* @return int 返回类型
*/
public static int subDays(Date date1, Date date2){
long d1 = DateUtils.toDate(DateUtils.getStringDate(date1), DEFAULT_DATE_FORMAT).getTime();
long d2 = DateUtils.toDate(DateUtils.getStringDate(date2), DEFAULT_DATE_FORMAT).getTime();
return (int) (d1 - d2) / (1000 * 60 * 60 * 24);
}
/**
*
* @Title: getDateByDateString
* @Description: 将dateString型的字符串转换为date类型对象
* @param dateString
* @return Date 返回类型
*/
public static Date getDateByDateString(String dateString){
SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
try {
return df.parse(dateString);
} catch (ParseException e) {
return new Date();
}
}
/**
*
* @Title: getDateByDateString
* @Description: 将dateString型的字符串转换为date类型对象
* @param dateString
* @return Date 返回类型
* @throws ParseException
*/
public static Date getDateByDateStr(String dateString) throws ParseException{
SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return df.parse(dateString);
}
/**
*
* @Title: addDays
* @Description: 返回指定日期(date)加上指定天数(days)后的日期
* @param date
* @param days
* @return Date 返回类型
*/
public static Date addDays(Date date, int days){
Calendar cal = Calendar.getInstance();
try {
cal.setTime(date);
cal.add(Calendar.DATE, days);
} catch (Exception e) {
log.error("addDays error" , e);
}
return cal.getTime();
}
/**
* @Title: addHour
* @author guoyang
* @Description: 返回指定日期(date)加上指定小时数(hours)后的日期
* @param date
* @param hour
* @return
*/
public static Date addHour(Date date, int hours){
Calendar cal = Calendar.getInstance();
try {
cal.setTime(date);
cal.add(Calendar.HOUR, hours);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return cal.getTime();
}
/**
*
* @Title: getStringDate
* @Description: 以“yyyy-MM-dd”形式返回当前的年、月、日
* @param d
* @return String 返回类型
*/
public static String getStringDate(Date d){
Date date = d;
SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return df.format(date);
}
/**
* @Title: getDoStringDate
* @param d 日期时间对象
* @return 格式化的时间日期字符串
*/
public static String getDoStringDate(Date d) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(DO_DATE_FORMAT);
return simpledateformat.format(d);
}
/**
*
* @Title: getStringDate
* @Description: 以“yyyy-MM-dd HH:mm:ss”形式返回当前的年、月、日
* @param d
* @return String 返回类型
*/
public static String getStringDateTime(Date d){
Date date = d;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
/**
*
* @Title: toDate
* @Description: 将dateStr型的字符串转换为format格式的date类型对象
* @param dateStr
* @param format
* @return Date 返回类型
*/
public static Date toDate(String dateStr, String format){
SimpleDateFormat df = new SimpleDateFormat(format);
try {
return df.parse(dateStr);
} catch (ParseException e) {
return new Date();
}
}
/**
*
* @Title: getFromatStringDateTime
* @description 将String类型的时间转化为format格式的日期字符串
* @param dateStr
* @param format
* @return String 转化后的时间
*/
public static String getFromatStringDateTime(String dateStr, String format) {
Date date = toDate(dateStr, format);
return getStringDate(date);
}
/**
*
* @Title: getStringTime
* @Description: 以"HH:mm:ss"形式返回传入的时间
* @param d
* @return String 返回类型
*/
public static String getStringTime(Date d){
Date date = d;
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(date);
}
/**
*
* @Title: getNextDay
* @Description: 得到日期后几天的日期时间
* @param date
* @return Date 返回类型
*/
public static Date getNextDay(Date date){
if(date == null) {
date = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,NEXT_DAY);
return cal.getTime();
}
/**
* 设定比较日期的Calendar 时分秒
* @param calendar
* @param compareDateStr yyyy-MM-dd字符格式
* @return Calendar 比较日期 (完整的年月日、时分秒)
*/
public static Calendar getCompareCalendar(Calendar calendar,String compareDateStr){
if(StringUtils.isEmpty(compareDateStr) || calendar ==null){
return null;
}
try{
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c.setTime(sdf.parse(compareDateStr));
c.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
c.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
return c;
}catch(Exception e){
log.error(e);
return null;
}
}
/**
* 取当前日期
* @return
*/
public static Date getNowDate(){
String nowStr = getStringDate(new Date());
return getDateByDateString(nowStr);
}
public static String getDate8Str(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}
public static String getDate8Str() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(new Date());
}
public static Calendar getCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static Date add(Date date, int field, int diff) {
Calendar c = getCalendar(date);
c.add(field, diff);
return c.getTime();
}
/**
* 得到与当前时间相差 diff 天的时间
*/
public static Date nextDays(Date date, int diff) {
return add(date, Calendar.DATE, diff);
}
/**
* 计算 d1 - d2 的毫秒差
*/
public static long diffMillis(Date d1, Date d2) {
return d1.getTime() - d2.getTime();
}
}