|
|
|
@ -2,14 +2,19 @@ |
|
|
|
|
|
|
|
package com.heai.common.utils; |
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
import org.joda.time.DateTime; |
|
|
|
import org.joda.time.LocalDate; |
|
|
|
import org.joda.time.format.DateTimeFormat; |
|
|
|
import org.joda.time.format.DateTimeFormatter; |
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.logging.Level; |
|
|
|
import java.util.logging.Logger; |
|
|
|
|
|
|
|
/** |
|
|
|
* 日期处理 |
|
|
|
@ -86,6 +91,81 @@ public class DateUtils { |
|
|
|
return dateTime.plusSeconds(seconds).toDate(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getStringDate(Date date) { |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
return sdf.format(date); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getStringNow() { |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN); |
|
|
|
return sdf.format(new Date()); |
|
|
|
} |
|
|
|
|
|
|
|
public static Date getStringToDate(String time,String format) { |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format); |
|
|
|
try { |
|
|
|
return sdf.parse(time); |
|
|
|
} catch (ParseException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static int differentDays (Date date1, Date date2) { |
|
|
|
Calendar cal1 = Calendar.getInstance(); |
|
|
|
cal1.setTime(date1); |
|
|
|
Calendar cal2 = Calendar.getInstance(); |
|
|
|
cal2.setTime(date2); |
|
|
|
int day1= cal1.get(Calendar.DAY_OF_YEAR); |
|
|
|
int day2 = cal2.get(Calendar.DAY_OF_YEAR); |
|
|
|
|
|
|
|
int year1 = cal1.get(Calendar.YEAR); |
|
|
|
int year2 = cal2.get(Calendar.YEAR); |
|
|
|
if(year1 != year2) { // 同一年 |
|
|
|
int timeDistance = 0 ; |
|
|
|
for(int i = year1; i < year2; i ++) { |
|
|
|
if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年 |
|
|
|
timeDistance += 366; |
|
|
|
} else { // 不是闰年 |
|
|
|
timeDistance += 365; |
|
|
|
} |
|
|
|
} |
|
|
|
return timeDistance + (day2-day1); |
|
|
|
} else { // 不同年 |
|
|
|
return day2 - day1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 比较两个日期的大小: |
|
|
|
* 2 发生异常 |
|
|
|
* 1 firstDate>secondDate |
|
|
|
* 0 firstDate==secondDate |
|
|
|
* -1 firstDate<secondDate |
|
|
|
* @param format |
|
|
|
* @param firstDate |
|
|
|
* @param secondDate |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static int compareDate(String format,String firstDate, String secondDate){ |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format); |
|
|
|
int result = 2; |
|
|
|
try { |
|
|
|
Date fDate = sdf.parse(firstDate); |
|
|
|
Date sDate = sdf.parse(secondDate); |
|
|
|
|
|
|
|
if(fDate.getTime()>sDate.getTime()){ |
|
|
|
result = 1; |
|
|
|
}else if(fDate.getTime()==sDate.getTime()){ |
|
|
|
result = 0; |
|
|
|
}else{ |
|
|
|
result = -1; |
|
|
|
} |
|
|
|
} catch (ParseException ex) { |
|
|
|
Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 对日期的【分钟】进行加/减 |
|
|
|
* |
|
|
|
|