import com.mysql.jdbc.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 时间处理类
* <p>Version: 1.0
*/
public class DateHelper {
private static Logger logger = LoggerFactory.getLogger(DateHelper.class);
public static final String getCurrentDate(String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern , Locale.CHINA);
return df.format(new Date());
}
/**
* 获取满格式日期
* @param dateString
* @return
*/
public static final Date getFullFormatDate(String dateString) {
return getFormatDate(dateString, "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取指定日期格式的日期
* @param dateString
* @return
*/
public static final Date getFormatDate(String dateString, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format , Locale.CHINA);
Date returnDate;
try {
returnDate = sdf.parse(dateString);
} catch (ParseException e) {
String errMsg = "日期字符串" + dateString + "解析异常";
logger.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
return returnDate;
}
public static final String getFormatString(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format , Locale.CHINA);
return sdf.format(date);
}
public static final String getFormatString(String dateString, String format) {
if ( StringUtils.isNullOrEmpty( dateString )) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat(format , Locale.CHINA);
Date returnDate;
String returnString;
try {
returnDate = sdf.parse(dateString);
} catch (ParseException e) {
String errMsg = "日期字符串" + dateString + "解析异常";
logger.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
returnString=sdf.format(returnDate);
return returnString;
}
/**
* 求两个日期之间相差天数 不足一天算一天
* @Title: getTwoDay
* @return long 返回类型
* @throws
*/
public static final int getTwoDay(Date StartDate, Date endDate){
if(endDate.getTime()>=StartDate.getTime()){
int day = (int)((endDate.getTime() - StartDate.getTime()) / (24 * 60 * 60 * 1000));
if((endDate.getTime() - StartDate.getTime()) % (24 * 60 * 60 * 1000)!=0)
day+=1;
return day;
}else{
return -1;
}
}
/**
* 计算时间差 精确到分
* <p>User: fengmingyue
* <p>Date: 2017年5月5日 下午8:38:35
* <p>Version: 1.0
* @param StartDate
* @param endDate
* @return
*/
public static String getTime(Date StartDate, Date endDate) {
long diff = endDate.getTime() - StartDate.getTime();
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
return days+"天"+hours+"小时"+minutes+"分";
}
/**
* 计算时间差 精确到分
* @return
*/
public static String getTimeStr (long diff ) {
long days = diff / ( 1000 * 60 * 60 * 24 );
long hours = ( diff - days * ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 );
long minutes = ( diff - days * ( 1000 * 60 * 60 * 24 ) - hours * ( 1000 * 60 * 60 ) ) / ( 1000 * 60 );
return days + "天" + hours + "小时" + minutes + "分钟";
}
/**
* 比较两个日期大小 当date1大于date2 时返回true
* @Title: compareTwoDate
* @Description: 比较两个日期大小 当date1大于date2 时返回true
* @return boolean 返回类型
*/
public static boolean compareTwoDate(Date date1, Date date2){
boolean flag = false;
try {
if (date1.getTime() > date2.getTime()) {
flag = true;
} else if (date1.getTime() < date2.getTime()) {
flag = false;
} else {
flag = true;
}
} catch (Exception e) {
logger.error("日期比较过程错误:" + e.getMessage(),e);
}
return flag;
}
/**
*
* @Title: getTimeSecond
* @Description: (时间差)
* @param @param reportDate
* @param @param contrastDate
* @param @return 设定文件
* @return Map 返回类型
* @throws
*/
public static String[] getTimeSecond(Date reportDate, Date contrastDate){
try {
long diff = contrastDate.getTime() - reportDate.getTime();
long day = diff / (24 * 60 * 60 * 1000);
long hour = (diff / (60 * 60 * 1000) - day * 24);
long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String[] time = {String.valueOf(day), String.valueOf(hour), String.valueOf(min), String.valueOf(s)};
return time;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new String[]{"0","0","0","0"};
}
}
/**
*
* @Title: getDate
* @Description: (根据样式返回当时date数据格式)
* @return String 返回类型
* @throws
*/
public static String getDate(Object currdate, String foramte) {
if (null == currdate) {
return "";
}
SimpleDateFormat date = new SimpleDateFormat(foramte);
return date.format(currdate);
}
/**
* <p>Desc:获得指定日期的前一天的日期
* @param date 指定日期
* @return
*/
public static String getLastDayDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(date);//把指定时间赋给日历
calendar.add(Calendar.DATE, -1); //设置为前一天
Date dBefore = calendar.getTime(); //得到前一天的时间
return sdf.format(dBefore);
}
/**
* <p>Desc:获得指定日期的前一个月的日期
* @param date 指定日期
* @return
*/
public static String getLastMonthDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance(); //得到日历
calendar.setTime(date);//把指定时间赋给日历
calendar.add(Calendar.MONTH, -1); //设置为前一个月
Date dBefore = calendar.getTime(); //得到前一个月
return sdf.format(dBefore);
}
/**
* 根据生日计算年龄
* @Title getAgeByBirth
* @param birthday
* @return
* @since V1.0
*/
public static int getAgeByBirth(Date birthday) {
int age = 0;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date());// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
if (birth.after(now)) {//如果传入的时间,在当前时间的后面,返回0岁
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {//兼容性更强,异常后返回数据
return 0;
}
}
/**
*
* @Title getNowNextTomorrowSeconds
* @Description 获取当前时间距离第二天零时多少秒数
* @return
*/
public static long getNowNextTomorrowSeconds(){
Calendar cal = Calendar.getInstance ();
cal.add ( Calendar.DAY_OF_YEAR, 1 );
cal.set ( Calendar.HOUR_OF_DAY, 0 );
cal.set ( Calendar.MINUTE, 0 );
cal.set ( Calendar.SECOND, 0 );
cal.set ( Calendar.MILLISECOND, 0 );
return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
}
/**
* @Param [根据时间戳(精确到毫秒)转换时间]
* @return java.util.Date
**/
public static final Date getFormatDateStamp(String dateString, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format , Locale.CHINA);
Date returnDate;
try {
long dateLong = Long.parseLong(dateString);
String ft = sdf.format(dateLong);
returnDate = sdf.parse(ft);
} catch (ParseException e) {
String errMsg = "日期字符串" + dateString + "解析异常";
logger.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
return returnDate;
}
}