|
@@ -20,7 +20,7 @@ import java.util.*;
|
|
*/
|
|
*/
|
|
public class DateUtils extends PropertyEditorSupport {
|
|
public class DateUtils extends PropertyEditorSupport {
|
|
|
|
|
|
- private static Logger logger= LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
|
|
/**
|
|
/**
|
|
* 以毫秒表示的时间
|
|
* 以毫秒表示的时间
|
|
@@ -892,7 +892,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
try {
|
|
try {
|
|
date = dateFormat.parse(nowTime);
|
|
date = dateFormat.parse(nowTime);
|
|
} catch (ParseException e) {
|
|
} catch (ParseException e) {
|
|
- logger.error(e.getMessage(),e);
|
|
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
|
|
//得到日历
|
|
//得到日历
|
|
@@ -1217,7 +1217,7 @@ 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");
|
|
|
|
|
|
@@ -1233,7 +1233,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
}
|
|
}
|
|
|
|
|
|
//得到相差的天数 betweenDate
|
|
//得到相差的天数 betweenDate
|
|
- long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);
|
|
|
|
|
|
+ long betweenDate = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
|
|
|
|
|
|
//打印控制台相差的天数
|
|
//打印控制台相差的天数
|
|
System.out.println(betweenDate);
|
|
System.out.println(betweenDate);
|
|
@@ -1243,20 +1243,20 @@ public class DateUtils extends PropertyEditorSupport {
|
|
/***
|
|
/***
|
|
* 两个日期之间间隔的月数(不考虑日期的情况)
|
|
* 两个日期之间间隔的月数(不考虑日期的情况)
|
|
*/
|
|
*/
|
|
- 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) {
|
|
@@ -1273,11 +1273,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 {
|
|
@@ -1285,7 +1286,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));
|
|
@@ -1297,8 +1298,33 @@ public class DateUtils extends PropertyEditorSupport {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public static int dateDiff(String start, String end) {
|
|
|
|
+ //设置转换的日期格式
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ //开始时间
|
|
|
|
+ Date startDate = null;
|
|
|
|
+ Date endDate = null;
|
|
|
|
+ try {
|
|
|
|
+ startDate = sdf.parse(start);
|
|
|
|
+ endDate = sdf.parse(end);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //得到相差的天数 betweenDate
|
|
|
|
+ long betweenDate = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
|
|
|
|
+ return (int) betweenDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Date addMonth(Date date) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();//日历对象
|
|
|
|
+ calendar.add(Calendar.MONTH, -1);//月份减一
|
|
|
|
+ return calendar.getTime();
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void main(String[] args) throws ParseException {
|
|
public static void main(String[] args) throws ParseException {
|
|
- System.out.println(calDiffMonth(DateUtils.parseDate("2019-04-01","yyyy-MM-dd"),new Date()));
|
|
|
|
|
|
+ System.out.println(calDiffMonth(DateUtils.parseDate("2019-04-01", "yyyy-MM-dd"), new Date()));
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|