|
|
@ -332,211 +332,211 @@ public class DateUtil { |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的年数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的年数 |
|
|
|
*/ |
|
|
|
public static long yearBetween(Date d1,Date d2){ |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.YEARS.between(ld2,ld1); |
|
|
|
public static long yearsBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的年数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @return 两个日期相差的年数 |
|
|
|
*/ |
|
|
|
public static long yearBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.YEARS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的月数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的月数 |
|
|
|
*/ |
|
|
|
public static long monthBetween(Date d1,Date d2){ |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.MONTHS.between(ld2,ld1); |
|
|
|
return ChronoUnit.YEARS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的月数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的月数 |
|
|
|
*/ |
|
|
|
public static long monthBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.MONTHS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
public static long monthsBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的周数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的周数 |
|
|
|
*/ |
|
|
|
public static long weeksBetween(Date d1,Date d2){ |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.WEEKS.between(ld2,ld1); |
|
|
|
return ChronoUnit.MONTHS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的周数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的周数 |
|
|
|
*/ |
|
|
|
public static long weeksBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.SECONDS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
public static long weeksBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的天数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的天数 |
|
|
|
*/ |
|
|
|
public static long dayBetween(Date d1,Date d2){ |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.DAYS.between(ld2,ld1); |
|
|
|
return ChronoUnit.WEEKS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的天数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的天数 |
|
|
|
*/ |
|
|
|
public static long dayBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.DAYS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
public static long daysBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的小时数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的小时数 |
|
|
|
*/ |
|
|
|
public static long hoursBetween(Date d1,Date d2){ |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.HOURS.between(ld2,ld1); |
|
|
|
return ChronoUnit.MONTHS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的小时数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的小时数 |
|
|
|
*/ |
|
|
|
public static long hoursBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.HOURS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
public static long hoursBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的分钟数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的分钟数 |
|
|
|
*/ |
|
|
|
public static long minutesBetween(Date d1,Date d2){ |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.MINUTES.between(ld2,ld1); |
|
|
|
return ChronoUnit.MONTHS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的分钟数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的分钟数 |
|
|
|
*/ |
|
|
|
public static long minutesBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.HOURS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
public static long minutesBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的秒数 |
|
|
|
* @param d1 日期1 |
|
|
|
* @param d2 日期2 |
|
|
|
* @return 两个日期相差的秒数 |
|
|
|
*/ |
|
|
|
public static long secondsBetween(Date d1,Date d2){ |
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.SECONDS.between(ld2,ld1); |
|
|
|
return ChronoUnit.MONTHS.between(ld1,ld2); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算两个日期相差的秒数 |
|
|
|
* @param d1Str 日期字符串1 |
|
|
|
* @param d2Str 日期字符串2 |
|
|
|
* @param o1 日期对象1 |
|
|
|
* @param o2 日期对象2 |
|
|
|
* @return 两个日期相差的秒数 |
|
|
|
*/ |
|
|
|
public static long secondsBetween(String d1Str,String d2Str){ |
|
|
|
try { |
|
|
|
Date d1 = tryParseDate(d1Str); |
|
|
|
Date d2 = tryParseDate(d2Str); |
|
|
|
LocalDateTime ld1 = toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 = toLocalDateTime(d2); |
|
|
|
return ChronoUnit.SECONDS.between(ld2, ld1); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new RuntimeException(e); |
|
|
|
public static long secondsBetween(Object o1,Object o2){ |
|
|
|
if(o1==null || o2==null) throw new RuntimeException("parameter can NOT be null"); |
|
|
|
Date d1 =null; |
|
|
|
Date d2 =null; |
|
|
|
if(o1 instanceof String){ |
|
|
|
try { d1 = tryParseDate(o1.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o1 instanceof Date){ |
|
|
|
d1 =(Date)o1; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
|
|
|
|
if(o2 instanceof String){ |
|
|
|
try { d2 = tryParseDate(o2.toString()); }catch (Exception e){ throw new RuntimeException(e); } |
|
|
|
}else if(o2 instanceof Date){ |
|
|
|
d1 =(Date)o2; |
|
|
|
}else{ |
|
|
|
throw new RuntimeException("parameter must be String or Date type."); |
|
|
|
} |
|
|
|
LocalDateTime ld1 =toLocalDateTime(d1); |
|
|
|
LocalDateTime ld2 =toLocalDateTime(d2); |
|
|
|
return ChronoUnit.MONTHS.between(ld1,ld2); |
|
|
|
} |
|
|
|
} |
|
|
|