You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.1 KiB
158 lines
5.1 KiB
package irbs.defaultManager.utils;
|
|
|
|
import irbs.defaultManager.enums.IntervalType;
|
|
import liquibase.util.StringUtil;
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
|
|
public class DateUtil {
|
|
|
|
public static Date toDate(String dtStr) {
|
|
if(dtStr != null){
|
|
dtStr = dtStr.replaceAll("[-./年月日]","");
|
|
try {
|
|
return io.sc.platform.core.util.DateUtil.parseDate(dtStr, "yyyyMMdd");
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Date toDateHLine(String dtStr) {
|
|
if(dtStr != null){
|
|
dtStr = dtStr.replaceAll("[-./年月日]","-");
|
|
if(dtStr.length()==8){//如果时YYYYMMDD格式
|
|
dtStr = dtStr.substring(0,4) + "-" + dtStr.substring(4,6) + "-" + dtStr.substring(6);
|
|
}
|
|
try {
|
|
return io.sc.platform.core.util.DateUtil.parseDate(dtStr, "yyyy-MM-dd");
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Date toDateTimeHLine(String dtStr) {
|
|
if(dtStr != null){
|
|
dtStr = dtStr.replaceAll("[-./年月日]","-");
|
|
if(dtStr.substring(0, dtStr.indexOf(" ")).length() == 8){//如果时YYYYMMDD HH:mm:ss格式
|
|
dtStr = dtStr.substring(0,4) + "-" + dtStr.substring(4,6) + "-" + dtStr.substring(6);
|
|
}
|
|
try {
|
|
return io.sc.platform.core.util.DateUtil.parseDate(dtStr, "yyyy-MM-dd HH:mm:ss");
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String toString(Date dt, String format){
|
|
if(dt != null){
|
|
return io.sc.platform.core.util.DateUtil.formatDate(dt, format);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String getCurrentDateString(){
|
|
Date currentDate = new Date();
|
|
return toString(currentDate, "yyyyMMdd");
|
|
}
|
|
|
|
public static String getCurrentDateHLineString(){
|
|
Date currentDate = new Date();
|
|
return toString(currentDate, "yyyy-MM-dd");
|
|
}
|
|
|
|
public static Date getCurrentDate(){
|
|
Calendar calendar = Calendar.getInstance();
|
|
return calendar.getTime();
|
|
}
|
|
|
|
public static String getLastMonthEndString(){
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.set(Calendar.DAY_OF_MONTH, 0);
|
|
return toString(calendar.getTime(), "yyyyMMdd");
|
|
}
|
|
|
|
public static Date addDays(int day){
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + day);
|
|
return calendar.getTime();
|
|
}
|
|
|
|
public static Date addDays(Date dt, int day){
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(dt);
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + day);
|
|
return calendar.getTime();
|
|
}
|
|
|
|
public static int compareDate(String dt1, String dt2){
|
|
int r = 0;
|
|
if(dt1 != null && dt2 != null){
|
|
dt1 = dt1.replaceAll("[-./年月日]","");
|
|
dt2 = dt2.replaceAll("[-./年月日]","");
|
|
Double d1 = Double.valueOf(dt1);
|
|
Double d2 = Double.valueOf(dt2);
|
|
if(d1.doubleValue() == d2.doubleValue()){
|
|
r = 0;
|
|
}else if(d1.doubleValue() > d2.doubleValue()){
|
|
r = 1;
|
|
}else{
|
|
r = -1;
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
public static int compareDate(Date dt1, Date dt2){
|
|
int r = 0;
|
|
if(dt1 != null && dt2 != null){
|
|
long dif = dt2.getTime() - dt1.getTime();
|
|
if(dif == 0){
|
|
r = 0;
|
|
}else if(dif < 0){
|
|
r = 1;
|
|
}else{
|
|
r = -1;
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* 计算日期差
|
|
* @param dt1 第一个日期
|
|
* @param dt2 第二个日期
|
|
* @param interval 日期差类型
|
|
* @return
|
|
*/
|
|
public static Double dateDiff(String dt1, String dt2, String interval) throws Exception {
|
|
if(StringUtil.isNotEmpty(dt1) && StringUtil.isNotEmpty(dt2)){
|
|
dt1 = dt1.replaceAll("[-./年月日]","");
|
|
dt2 = dt2.replaceAll("[-./年月日]","");
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
Date d1 = format.parse(dt1);
|
|
Date d2 = format.parse(dt2);
|
|
long dif = d2.getTime() - d1.getTime();
|
|
Long ret = null;
|
|
if(IntervalType.年.code().equalsIgnoreCase(interval)){
|
|
ret = dif/3600/1000/24/365;
|
|
}else if(IntervalType.月.code().equalsIgnoreCase(interval)){
|
|
ret = dif/3600/1000/24/30;
|
|
}else if(IntervalType.日.code().equalsIgnoreCase(interval)){
|
|
ret = dif/3600/1000/24;
|
|
}else{
|
|
ret = dif/3600/1000/24/365;
|
|
}
|
|
return ret.doubleValue();
|
|
}
|
|
return 0d;
|
|
}
|
|
}
|
|
|