Java的日期工具类

package com.tech.jin.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import org.apache.log4j.Logger; public class DateUtil { private static Logger logger = Logger.getLogger(DateUtil.class); private static Date now = new Date(); /**
* 获取日期格式对象
* @param format
* @return
*/
private static DateFormat getFormatInstance(String format){
DateFormat sdf = new SimpleDateFormat(format);
return sdf;
} /**
* date对象转字符串
* @param date
* @param format
* @return
*/
public static String dateToString(Date date,String format){
if(date==null){
return "";
} DateFormat df = getFormatInstance(format);
String dateStr = df.format(date);
logger.info("dateToString:"+dateStr);
return dateStr;
} /**
* 字符串转date对象
* @param dateStr
* @param format
* @return
*/
public static Date stringToDate(String dateStr,String format){
if(dateStr==null||"".equals(dateStr.trim())){
logger.info("要转换的date字符串为空...");
return null;
}
logger.info("stringToDate:"+dateStr);
DateFormat df = getFormatInstance(format);
Date date = null;
try {
date = df.parse(dateStr);
} catch (ParseException e) {
logger.error("stringToDate转换错误:"+e);
} return date;
} /**
* date字符串更换格式
* @param dateStr
* @param oldFormat
* @param newFormat
* @return
*/
public static String stringToString(String dateStr,String oldFormat,String newFormat){
Date date = stringToDate(dateStr, oldFormat);
dateStr = dateToString(date, newFormat); return dateStr;
} /**
* 获取当前时间/日期
* @return
*/
public static Date getNow(){
return now;
} /**
* 获取当前时间/日期字符串
* @param format
* @return
*/
public static String getNowStr(String format){
String nowStr = dateToString(now, format);
logger.info("获取当前时间字符串:"+nowStr);
return nowStr;
} /**
* 比较两个时间字符串
* @param dateStr1
* @param dateStr2
* @param format
* @return -2:比较时间异常
* -1: (dateStr1 < dateStr2)
* 0: (dateStr1 = dateStr2)
* 1: (dateStr1 > dateStr2)
*
*/
public static int compareDateStr(String dateStr1,String dateStr2,String format){ logger.info("dateStr1:"+dateStr1+" dateStr2:"+dateStr2); Date date1 = stringToDate(dateStr1, format);
Date date2 = stringToDate(dateStr2, format); int result = -2;//初始化状态 result = compareDate(date1, date2); String resultStr = "对字符串进行("+format+")格式化后,";
if(result==0){
resultStr += dateStr1+" = "+dateStr2;
}else if(result==1){
resultStr += dateStr1+" > "+dateStr2;
}else if(result==-1){
resultStr += dateStr1+" < "+dateStr2;
}else{
resultStr += "比较日期异常...";
}
logger.info("比较时间结果:"+result +" "+resultStr); return result; } /**
* 比较两个date
* @param date1
* @param date2
* @return -2:比较时间异常
* -1: (date1 < date2)
* 0: (date1 = date2)
* 1: (date1 > date2)
*/
public static int compareDate(Date date1,Date date2){
int result = -2;//初始化状态
if(date1==null||date2==null){
return -2;
} result = date1.compareTo(date2);
logger.info("compareDate:"+result); return result;
} /**
* 获取传入date的几天前或几天后日期
* @param date
* @param daysCount 天数(正数:daysCount天后;负数:daysCount天前)
* @return
*/
public static Date getDayBeforeOrAfter(Date date,int daysCount){
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, daysCount); return c.getTime();
} /**
* 获取传入dateStr的几天前或几天后日期
* @param dateStr
* @param format 传入传出的日期字符串格式
* @param daysCount 天数(正数:daysCount天后;负数:daysCount天前)
* @return
*/
public static String getDayBeforeOrAfter(String dateStr,String format,int daysCount){
if(dateStr==null||"".equals(dateStr.trim())){
return "";
}
logger.info("dateStr:"+dateStr+" format:"+format +" daysCount:"+daysCount);
Date date = stringToDate(dateStr, format);
date = getDayBeforeOrAfter(date, daysCount);
dateStr = dateToString(date, format);
logger.info("dateStr:"+dateStr);
return dateStr;
} /**
* 获取date这个月的最后一天
* @param date
* @return
*/
public static Date getDayMonthEnd(Date date){
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, 1);//得到下个月
c.set(Calendar.DATE, 1);//设置下个月1号初始化
c.add(Calendar.DATE, -1);//下个月1号减一天,即date当月的最后一天
return c.getTime();
} /**
* 取两个日期的差值
* @param startDate
* @param endDate
* @return int[]{days,hours,minutes,seconds}
*/
public static int[] getDateDiff(Date startDate,Date endDate) { if(startDate==null||endDate==null){
logger.error("getDaysDiff:传入的日期参数异常...");
return null;
} long time = (endDate.getTime() - startDate.getTime());//单位:毫秒 time = Math.abs(time); time = time / 1000;//转成秒 long days = time / (24 * 60 * 60); long hours = time % (24 * 60 * 60) / (60 * 60); long minutes = time % (60 * 60) / 60; long seconds = time % 60; logger.info("日期差值:"+days+"天 "+hours+"小时 "+minutes+"分钟 "+seconds+"秒"); int[] diff = new int[]{(int)days,(int)hours,(int)minutes,(int)seconds}; return diff;
} /**
* 得到date的daysCount天前/天后是星期几
* @param date
* @param daysCount
* @return
*/
public static String getAfterDayWeek(Date date,int daysCount) { date = getDayBeforeOrAfter(date, daysCount);
String dateStr = dateToString(date, "E"); logger.info("getAfterDayWeek:" + dateStr); return dateStr;
} public static void main(String[] args) {
Date a = stringToDate("20170106 23:57:59", "yyyyMMdd HH:mm:ss");
Date b = stringToDate("20170107 23:56:59", "yyyyMMdd HH:mm:ss"); getDateDiff(a, b); } }
上一篇:jq如何获取服务器的时间


下一篇:LightOJ1021 Painful Bases(状压DP)