|
@@ -1,5 +1,7 @@
|
|
package org.jeecg.common.util;
|
|
package org.jeecg.common.util;
|
|
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.beans.PropertyEditorSupport;
|
|
import java.beans.PropertyEditorSupport;
|
|
@@ -17,6 +19,9 @@ import java.util.*;
|
|
* @Version 1.0
|
|
* @Version 1.0
|
|
*/
|
|
*/
|
|
public class DateUtils extends PropertyEditorSupport {
|
|
public class DateUtils extends PropertyEditorSupport {
|
|
|
|
+
|
|
|
|
+ private static Logger logger= LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 以毫秒表示的时间
|
|
* 以毫秒表示的时间
|
|
*/
|
|
*/
|
|
@@ -236,7 +241,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
Date dt = new Date();
|
|
Date dt = new Date();
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
String nowTime = df.format(dt);
|
|
String nowTime = df.format(dt);
|
|
- java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
|
|
|
|
|
|
+ Timestamp buydate = Timestamp.valueOf(nowTime);
|
|
return buydate;
|
|
return buydate;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -779,10 +784,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
return sdf.format(calendar.getTime());
|
|
return sdf.format(calendar.getTime());
|
|
}
|
|
}
|
|
|
|
|
|
- //public static void main(String[] args) throws ParseException {
|
|
|
|
- // System.out.println(DateUtils.addDay( "2020-01-03", 7));
|
|
|
|
- //}
|
|
|
|
-
|
|
|
|
public static Date addDay(Date date, int day) {
|
|
public static Date addDay(Date date, int day) {
|
|
Calendar calendar = Calendar.getInstance();
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(date);
|
|
calendar.setTime(date);
|
|
@@ -880,10 +881,15 @@ public class DateUtils extends PropertyEditorSupport {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static String getMonthBefore(String format, String nowTime, int amount) throws ParseException {
|
|
|
|
|
|
+ public static String getMonthBefore(String format, String nowTime, int amount) {
|
|
// 获取当前时间
|
|
// 获取当前时间
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
- Date date = dateFormat.parse(nowTime);
|
|
|
|
|
|
+ Date date = null;
|
|
|
|
+ try {
|
|
|
|
+ date = dateFormat.parse(nowTime);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
|
|
//得到日历
|
|
//得到日历
|
|
Calendar calendar = Calendar.getInstance();
|
|
Calendar calendar = Calendar.getInstance();
|
|
@@ -1207,10 +1213,9 @@ public class DateUtils extends PropertyEditorSupport {
|
|
* @param dateEnd
|
|
* @param dateEnd
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static long getDiscrepantDays(String dateStart, String dateEnd){
|
|
|
|
|
|
+ public static long getDiscrepantDays(String dateStart, String dateEnd) {
|
|
//设置转换的日期格式
|
|
//设置转换的日期格式
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
-
|
|
|
|
//结束时间
|
|
//结束时间
|
|
Date startDate = null;
|
|
Date startDate = null;
|
|
Date endDate = null;
|
|
Date endDate = null;
|
|
@@ -1221,32 +1226,27 @@ public class DateUtils extends PropertyEditorSupport {
|
|
} catch (ParseException e) {
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
-
|
|
|
|
//得到相差的天数 betweenDate
|
|
//得到相差的天数 betweenDate
|
|
- long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
|
|
|
|
-
|
|
|
|
- //打印控制台相差的天数
|
|
|
|
- System.out.println(betweenDate);
|
|
|
|
- return betweenDate;
|
|
|
|
|
|
+ return (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
|
|
}
|
|
}
|
|
|
|
|
|
/***
|
|
/***
|
|
* 两个日期之间间隔的月数(不考虑日期的情况)
|
|
* 两个日期之间间隔的月数(不考虑日期的情况)
|
|
*/
|
|
*/
|
|
- public static int calDiffMonth(Date start, Date end){
|
|
|
|
|
|
+ public static int calDiffMonth(Date start, Date end) {
|
|
GregorianCalendar startCalendar = new GregorianCalendar();
|
|
GregorianCalendar startCalendar = new GregorianCalendar();
|
|
startCalendar.setTime(start);
|
|
startCalendar.setTime(start);
|
|
GregorianCalendar endCalendar = new GregorianCalendar();
|
|
GregorianCalendar endCalendar = new GregorianCalendar();
|
|
endCalendar.setTime(end);
|
|
endCalendar.setTime(end);
|
|
|
|
|
|
- int startYear=startCalendar.get(Calendar.YEAR);
|
|
|
|
- int startMonth=startCalendar.get(Calendar.MONTH)+1;
|
|
|
|
- int startDay=startCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
- int endYear=endCalendar.get(Calendar.YEAR);
|
|
|
|
- int endMonth=endCalendar.get(Calendar.MONTH)+1;
|
|
|
|
- int endDay=endCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
+ int startYear = startCalendar.get(Calendar.YEAR);
|
|
|
|
+ int startMonth = startCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
+ int startDay = startCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
+ int endYear = endCalendar.get(Calendar.YEAR);
|
|
|
|
+ int endMonth = endCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
+ int endDay = endCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
- return (endYear-startYear)*12 + (endMonth - startMonth);
|
|
|
|
|
|
+ return (endYear - startYear) * 12 + (endMonth - startMonth);
|
|
}
|
|
}
|
|
|
|
|
|
public static int getDaysOfMonth(int year, int month) {
|
|
public static int getDaysOfMonth(int year, int month) {
|
|
@@ -1263,11 +1263,12 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取指定日期下个月的第一天
|
|
* 获取指定日期下个月的第一天
|
|
|
|
+ *
|
|
* @param dateStr
|
|
* @param dateStr
|
|
* @param format
|
|
* @param format
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static Map<String, String> getMaxMinDaysOfAddMonth(String dateStr,String format, int addMonth){
|
|
|
|
|
|
+ public static Map<String, String> getMaxMinDaysOfAddMonth(String dateStr, String format, int addMonth) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
Map<String, String> dateMap = new HashMap<>();
|
|
Map<String, String> dateMap = new HashMap<>();
|
|
try {
|
|
try {
|
|
@@ -1275,7 +1276,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
Calendar calendar = Calendar.getInstance();
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(date);
|
|
calendar.setTime(date);
|
|
calendar.add(Calendar.MONTH, addMonth);
|
|
calendar.add(Calendar.MONTH, addMonth);
|
|
- calendar.set(Calendar.DAY_OF_MONTH,1);
|
|
|
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
dateMap.put("startDate", sdf.format(calendar.getTime()));
|
|
dateMap.put("startDate", sdf.format(calendar.getTime()));
|
|
|
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
|
|
@@ -1287,7 +1288,8 @@ public class DateUtils extends PropertyEditorSupport {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public static int dateDiff(String start, String end){
|
|
|
|
|
|
+
|
|
|
|
+ public static int dateDiff(String start, String end) {
|
|
//设置转换的日期格式
|
|
//设置转换的日期格式
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
//开始时间
|
|
//开始时间
|
|
@@ -1301,16 +1303,20 @@ public class DateUtils extends PropertyEditorSupport {
|
|
}
|
|
}
|
|
|
|
|
|
//得到相差的天数 betweenDate
|
|
//得到相差的天数 betweenDate
|
|
- long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
|
|
|
|
- return (int)betweenDate;
|
|
|
|
|
|
+ long betweenDate = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
|
|
|
|
+ return (int) betweenDate;
|
|
}
|
|
}
|
|
|
|
|
|
- public static Date addMonth(Date date){
|
|
|
|
|
|
+ public static Date addMonth(Date date) {
|
|
Calendar calendar = Calendar.getInstance();//日历对象
|
|
Calendar calendar = Calendar.getInstance();//日历对象
|
|
calendar.add(Calendar.MONTH, -1);//月份减一
|
|
calendar.add(Calendar.MONTH, -1);//月份减一
|
|
return calendar.getTime();
|
|
return calendar.getTime();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void main(String[] args) throws ParseException {
|
|
|
|
+ System.out.println(calDiffMonth(DateUtils.parseDate("2019-04-01", "yyyy-MM-dd"), new Date()));
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|