|
@@ -23,21 +23,64 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
|
|
- /**
|
|
|
- * 以毫秒表示的时间
|
|
|
- */
|
|
|
+ public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyyMMdd");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> yyyymmddhhmmss = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> short_time_sdf = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("HH:mm");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() {
|
|
|
+ @Override
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 以毫秒表示的时间
|
|
|
private static final long DAY_IN_MILLIS = 24 * 3600 * 1000L;
|
|
|
private static final long HOUR_IN_MILLIS = 3600 * 1000L;
|
|
|
private static final long MINUTE_IN_MILLIS = 60 * 1000L;
|
|
|
private static final long SECOND_IN_MILLIS = 1000L;
|
|
|
|
|
|
+ public static String getNowHour(Date date) {
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
|
|
|
+ return simpleDateFormat.format(date);
|
|
|
+ }
|
|
|
|
|
|
public static Date getNowDate() throws ParseException {
|
|
|
SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
String nowDate = getNowDate("yyyy-MM-dd");
|
|
|
- Date parse = dsdf.parse(nowDate);
|
|
|
- return parse;
|
|
|
-
|
|
|
+ return dsdf.parse(nowDate);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -47,28 +90,14 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
//将字符串形式的时间转化为Date类型的时间
|
|
|
Date a = sdf.parse(beginDate);
|
|
|
Date b = sdf.parse(nowDate);
|
|
|
- //Date类的一个方法,如果a早于b返回true,否则返回false
|
|
|
- if (a.before(b)) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- * 如果你不喜欢用上面这个太流氓的方法,也可以根据将Date转换成毫秒
|
|
|
- if(a.getTime()-b.getTime()<0)
|
|
|
- return true;
|
|
|
- else
|
|
|
- return false;
|
|
|
- */
|
|
|
+ return a.before(b);
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String getDateByDateStr(String dateStr) throws ParseException {
|
|
|
SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
Date parse = dsdf.parse(dateStr);
|
|
|
- String day = dsdf.format(parse);
|
|
|
- return day;
|
|
|
+ return dsdf.format(parse);
|
|
|
}
|
|
|
|
|
|
public static Integer getHour(String dateStr) throws ParseException {
|
|
@@ -76,10 +105,27 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
Date date = sdf.parse(dateStr);
|
|
|
cal.setTime(date);
|
|
|
- Integer hour = cal.get(Calendar.HOUR_OF_DAY);
|
|
|
- return hour;
|
|
|
+ return cal.get(Calendar.HOUR_OF_DAY);
|
|
|
+ }
|
|
|
|
|
|
+ public static String getEndTime(String time) throws ParseException {
|
|
|
+ SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date parse = smf.parse(time);
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(parse);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.add(Calendar.SECOND, -1);
|
|
|
+ Date end = calendar.getTime();
|
|
|
+ String format = smf.format(end);
|
|
|
+ return format;
|
|
|
+ }
|
|
|
|
|
|
+ // 指定模式的时间格式
|
|
|
+ private static SimpleDateFormat getSDFormat(String pattern) {
|
|
|
+ return new SimpleDateFormat(pattern);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -93,6 +139,41 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 日期转换为字符串
|
|
|
+ *
|
|
|
+ * @return 字符串
|
|
|
+ */
|
|
|
+ public static String date2Str() {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return format.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
+ String splitDate = DateUtils.addDay(endDate, -1);
|
|
|
+ splitDate = DateUtils.addDay(splitDate, 1);
|
|
|
+ if (DateUtils.compareDate(startDate, splitDate) >= 0) {
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ map.put("startDate", startDate);
|
|
|
+ map.put("endDate", endDate);
|
|
|
+ list.add(map);
|
|
|
+ } else {
|
|
|
+ while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("startDate", splitDate);
|
|
|
+ map.put("endDate", endDate);
|
|
|
+ list.add(map);
|
|
|
+ splitDate = DateUtils.addDay(splitDate, -1);
|
|
|
+ endDate = DateUtils.addDay(endDate, -1);
|
|
|
+ if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
+ splitDate = startDate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 当前日历,这里用中国时间表示
|
|
|
*
|
|
|
* @return 以当地时区表示的系统当前日历
|
|
@@ -100,7 +181,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
public static Calendar getCalendar() {
|
|
|
return Calendar.getInstance();
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 指定毫秒数表示的日历
|
|
|
*
|
|
@@ -113,20 +193,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
return cal;
|
|
|
}
|
|
|
|
|
|
- // ////////////////////////////////////////////////////////////////////////////
|
|
|
- // getDate
|
|
|
- // 各种方式获取的Date
|
|
|
- // ////////////////////////////////////////////////////////////////////////////
|
|
|
-
|
|
|
- /**
|
|
|
- * 当前日期
|
|
|
- *
|
|
|
- * @return 系统当前时间
|
|
|
- */
|
|
|
- public static Date getDate() {
|
|
|
- return new Date();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 字符串转换成日期
|
|
|
*
|
|
@@ -147,17 +213,31 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 当前日期
|
|
|
+ *
|
|
|
+ * @return 系统当前时间
|
|
|
+ * @param l
|
|
|
+ */
|
|
|
+ public static Date getDate(long l) {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
/**
|
|
|
* 日期转换为字符串
|
|
|
*
|
|
|
+ * @param date_sdf 日期格式
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String date2Str() {
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- return format.format(new Date());
|
|
|
+ public static String date2Str(SimpleDateFormat date_sdf) {
|
|
|
+ Date date = getDate();
|
|
|
+ if (null == date) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return date_sdf.format(date);
|
|
|
+ }
|
|
|
+ public static Date getDate() {
|
|
|
+ return new Date();
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 格式化时间
|
|
|
*
|
|
@@ -223,8 +303,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 当前时间的标准形式字符串
|
|
|
*/
|
|
|
public static String now() {
|
|
|
- SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- return datetimeFormat.format(getCalendar().getTime());
|
|
|
+ return datetimeFormat.get().format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -251,8 +330,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
Date dt = new Date();
|
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
String nowTime = df.format(dt);
|
|
|
- Timestamp buydate = Timestamp.valueOf(nowTime);
|
|
|
- return buydate;
|
|
|
+ return Timestamp.valueOf(nowTime);
|
|
|
}
|
|
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////
|
|
@@ -310,8 +388,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 默认日期按“年-月-日“格式显示
|
|
|
*/
|
|
|
public static String formatDate() {
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- return format.format(getCalendar().getTime());
|
|
|
+ return date_sdf.get().format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -320,8 +397,14 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
|
|
|
*/
|
|
|
public static String formatDateTime() {
|
|
|
- SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- return datetimeFormat.format(getCalendar().getTime());
|
|
|
+ return datetimeFormat.get().format(getCalendar().getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间字符串
|
|
|
+ */
|
|
|
+ public static String getDataString(SimpleDateFormat formatstr) {
|
|
|
+ return formatstr.format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -331,8 +414,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“年-月-日“格式显示
|
|
|
*/
|
|
|
public static String formatDate(Calendar cal) {
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- return format.format(cal.getTime());
|
|
|
+ return date_sdf.get().format(cal.getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -342,8 +424,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“年-月-日“格式显示
|
|
|
*/
|
|
|
public static String formatDate(Date date) {
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- return format.format(date);
|
|
|
+ return date_sdf.get().format(date);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -353,8 +434,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定毫秒数表示日期按“年-月-日“格式显示
|
|
|
*/
|
|
|
public static String formatDate(long millis) {
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- return format.format(new Date(millis));
|
|
|
+ return date_sdf.get().format(new Date(millis));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -364,7 +444,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 默认日期按指定格式显示
|
|
|
*/
|
|
|
public static String formatDate(String pattern) {
|
|
|
- return getDateFormat(pattern).format(getCalendar().getTime());
|
|
|
+ return getSDFormat(pattern).format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -375,7 +455,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按指定格式显示
|
|
|
*/
|
|
|
public static String formatDate(Calendar cal, String pattern) {
|
|
|
- return getDateFormat(pattern).format(cal.getTime());
|
|
|
+ return getSDFormat(pattern).format(cal.getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -386,7 +466,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按指定格式显示
|
|
|
*/
|
|
|
public static String formatDate(Date date, String pattern) {
|
|
|
- return getDateFormat(pattern).format(date);
|
|
|
+ return getSDFormat(pattern).format(date);
|
|
|
}
|
|
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////
|
|
@@ -400,8 +480,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 默认日期按“年-月-日 时:分“格式显示
|
|
|
*/
|
|
|
public static String formatTime() {
|
|
|
- SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
- return timeSdf.format(getCalendar().getTime());
|
|
|
+ return time_sdf.get().format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -411,8 +490,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
|
|
|
*/
|
|
|
public static String formatTime(long millis) {
|
|
|
- SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
- return timeSdf.format(new Date(millis));
|
|
|
+ return time_sdf.get().format(new Date(millis));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -422,8 +500,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“年-月-日 时:分“格式显示
|
|
|
*/
|
|
|
public static String formatTime(Calendar cal) {
|
|
|
- SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
- return timeSdf.format(cal.getTime());
|
|
|
+ return time_sdf.get().format(cal.getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -433,8 +510,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“年-月-日 时:分“格式显示
|
|
|
*/
|
|
|
public static String formatTime(Date date) {
|
|
|
- SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
- return timeSdf.format(date);
|
|
|
+ return time_sdf.get().format(date);
|
|
|
}
|
|
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////
|
|
@@ -448,8 +524,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 默认日期按“时:分“格式显示
|
|
|
*/
|
|
|
public static String formatShortTime() {
|
|
|
- SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
|
|
|
- return shortTimeSdf.format(getCalendar().getTime());
|
|
|
+ return short_time_sdf.get().format(getCalendar().getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -459,8 +534,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定毫秒数表示日期按“时:分“格式显示
|
|
|
*/
|
|
|
public static String formatShortTime(long millis) {
|
|
|
- SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
|
|
|
- return shortTimeSdf.format(new Date(millis));
|
|
|
+ return short_time_sdf.get().format(new Date(millis));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -516,10 +590,9 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @param src 将要转换的原始字符窜
|
|
|
* @param pattern 转换的匹配格式
|
|
|
* @return 如果转换成功则返回转换后的日期
|
|
|
- * @throws ParseException
|
|
|
* @throws
|
|
|
*/
|
|
|
- public static Calendar parseCalendar(String src, String pattern) throws ParseException {
|
|
|
+ public static Calendar parseCalendar(String src, String pattern){
|
|
|
|
|
|
Date date = parseDate(src, pattern);
|
|
|
Calendar cal = Calendar.getInstance();
|
|
@@ -527,7 +600,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
return cal;
|
|
|
}
|
|
|
|
|
|
- public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
|
|
|
+ public static String formatAddDate(String src, String pattern, int amount) {
|
|
|
Calendar cal;
|
|
|
cal = parseCalendar(src, pattern);
|
|
|
cal.add(Calendar.DATE, amount);
|
|
@@ -543,7 +616,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @throws ParseException
|
|
|
* @throws
|
|
|
*/
|
|
|
- public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
|
|
|
+ public static Timestamp parseTimestamp(String src, String pattern) {
|
|
|
Date date = parseDate(src, pattern);
|
|
|
return new Timestamp(date.getTime());
|
|
|
}
|
|
@@ -593,13 +666,10 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
Date date = sdf1.parse(createTime);
|
|
|
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- String str = sdf2.format(date);
|
|
|
-
|
|
|
- return str;
|
|
|
+ return sdf2.format(date);
|
|
|
}
|
|
|
|
|
|
public static Map<String, Object> getStartEndTime(String createTime) throws ParseException {
|
|
|
-
|
|
|
SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
Date parse = smf.parse(createTime);
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
@@ -646,7 +716,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* HH:mm:ss“ * @param text String类型的时间值
|
|
|
*/
|
|
|
@Override
|
|
|
- public void setAsText(String text) throws IllegalArgumentException {
|
|
|
+ public void setAsText(String text) {
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
if (StringUtils.hasText(text)) {
|
|
@@ -687,43 +757,18 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
|
|
|
- List<Map<String, String>> list = new ArrayList<>();
|
|
|
- String splitDate = DateUtils.addDay(endDate, -1);
|
|
|
- splitDate = DateUtils.addDay(splitDate, 1);
|
|
|
- if (DateUtils.compareDate(startDate, splitDate) >= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
- map.put("startDate", startDate);
|
|
|
- map.put("endDate", endDate);
|
|
|
- list.add(map);
|
|
|
- } else {
|
|
|
- while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
- map.put("startDate", splitDate);
|
|
|
- map.put("endDate", endDate);
|
|
|
- list.add(map);
|
|
|
- splitDate = DateUtils.addDay(splitDate, -1);
|
|
|
- endDate = DateUtils.addDay(endDate, -1);
|
|
|
- if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
- splitDate = startDate;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
|
|
|
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
String splitDate = DateUtils.addWeek(endDate, -1);
|
|
|
splitDate = DateUtils.addDay(splitDate, 1);
|
|
|
if (DateUtils.compareDate(startDate, splitDate) >= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
map.put("startDate", startDate);
|
|
|
map.put("endDate", endDate);
|
|
|
list.add(map);
|
|
|
} else {
|
|
|
while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
map.put("startDate", splitDate);
|
|
|
map.put("endDate", endDate);
|
|
|
list.add(map);
|
|
@@ -738,17 +783,17 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
}
|
|
|
|
|
|
public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
|
|
|
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
String splitDate = DateUtils.addMonth(endDate, -1);
|
|
|
splitDate = DateUtils.addDay(splitDate, 1);
|
|
|
if (DateUtils.compareDate(startDate, splitDate) >= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
map.put("startDate", startDate);
|
|
|
map.put("endDate", endDate);
|
|
|
list.add(map);
|
|
|
} else {
|
|
|
while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
map.put("startDate", splitDate);
|
|
|
map.put("endDate", endDate);
|
|
|
list.add(map);
|
|
@@ -844,11 +889,10 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
|
|
|
public static Map<String, Object> getBeforeDate() {
|
|
|
Date dNow = new Date();
|
|
|
- Date dBefore = new Date();
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.setTime(dNow);
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
- dBefore = calendar.getTime();
|
|
|
+ Date dBefore = calendar.getTime();
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
String defaultStartDate = sdf.format(dBefore);
|
|
|
defaultStartDate = defaultStartDate + " 00:00:00";
|
|
@@ -865,7 +909,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @param date 传入的初始日期
|
|
|
* @param num 天数
|
|
|
* @return
|
|
|
- * @throws ParseException
|
|
|
*/
|
|
|
public static String getAnotherDay(String format, String date, Integer num) {
|
|
|
|
|
@@ -914,8 +957,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
}
|
|
|
|
|
|
public static Map<String, Object> compareDate(String format, String date1, String date2) {
|
|
|
-
|
|
|
-
|
|
|
DateFormat df = new SimpleDateFormat(format);
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
try {
|
|
@@ -945,11 +986,9 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @throws ParseException
|
|
|
*/
|
|
|
public static String getYear(String format, String date) throws ParseException {
|
|
|
-
|
|
|
SimpleDateFormat df = new SimpleDateFormat(format);
|
|
|
Date parse = df.parse(date);
|
|
|
return String.format("%tY", parse);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -975,7 +1014,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @throws ParseException
|
|
|
*/
|
|
|
public static String getDay(String format, String date) throws ParseException {
|
|
|
-
|
|
|
SimpleDateFormat df = new SimpleDateFormat(format);
|
|
|
Date parse = df.parse(date);
|
|
|
return String.format("%td", parse);
|
|
@@ -1016,7 +1054,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
calendar.setTime(date);
|
|
|
calendar.setFirstDayOfWeek(Calendar.FRIDAY);
|
|
|
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
|
|
|
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
return calendar.getTime();
|
|
|
}
|
|
|
|
|
@@ -1026,13 +1063,12 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
*/
|
|
|
public static Map<String, Integer> getYearQuarter(Date date) {
|
|
|
Map<String, Integer> dateMap = new HashMap<>();
|
|
|
-
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
calendar.setTime(date);
|
|
|
int originYear = calendar.get(Calendar.YEAR);
|
|
|
int originMonth = calendar.get(Calendar.MONTH);
|
|
|
- int originQuarter = DateUtils.getQuarter(date); //本月所在季度(按照日期来的)
|
|
|
-
|
|
|
+ //本月所在季度(按照日期来的)
|
|
|
+ int originQuarter = DateUtils.getQuarter(date);
|
|
|
Calendar calendar1 = new GregorianCalendar();
|
|
|
calendar1.setTime(date);
|
|
|
calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
|
|
@@ -1054,7 +1090,8 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
//如果本周第一天(周五)和当前天不在一个月内,说明周跨月了
|
|
|
if (fridayMonth == originMonth && thursdayMonth == originMonth) {
|
|
|
dateMap.put("year", originYear);
|
|
|
- dateMap.put("month", originMonth + 1); //最后一个月是11,应该+1
|
|
|
+ //最后一个月是11,应该+1
|
|
|
+ dateMap.put("month", originMonth + 1);
|
|
|
dateMap.put("quarter", originQuarter);
|
|
|
return dateMap;
|
|
|
} else if (fridayYear < originYear || (fridayYear == originYear && fridayMonth < originMonth)) {
|
|
@@ -1248,14 +1285,10 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
startCalendar.setTime(start);
|
|
|
GregorianCalendar endCalendar = new GregorianCalendar();
|
|
|
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);
|
|
|
-
|
|
|
return (endYear - startYear) * 12 + (endMonth - startMonth);
|
|
|
}
|
|
|
|
|
@@ -1317,30 +1350,24 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
return (int) betweenDate;
|
|
|
}
|
|
|
|
|
|
- public static Date addMonth(Date date) {
|
|
|
- Calendar calendar = Calendar.getInstance();//日历对象
|
|
|
- calendar.add(Calendar.MONTH, -1);//月份减一
|
|
|
- return calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 判断两个日期间隔是否大于半年
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean isMoreSixMonth(String startDate, String endDate) {
|
|
|
+ public static boolean isMoreSixMonth(String startDate, String endDate) throws ParseException {
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
c.setTime(Objects.requireNonNull(parseDate(startDate, SystemDateConstant.yyyy_MM_dd)));
|
|
|
long time1 = c.getTimeInMillis();
|
|
|
c.setTime(Objects.requireNonNull(parseDate(endDate, SystemDateConstant.yyyy_MM_dd)));
|
|
|
long time2 = c.getTimeInMillis();
|
|
|
- long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
- int day = Integer.parseInt(String.valueOf(between_days));
|
|
|
+ long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
+ int day = Integer.parseInt(String.valueOf(betweenDays));
|
|
|
return day > 180;
|
|
|
}
|
|
|
|
|
|
public static List<Date> findDates(Date dBegin, Date dEnd) {
|
|
|
- List<Date> lDate = new ArrayList<Date>();
|
|
|
+ List<Date> lDate = new ArrayList<>();
|
|
|
lDate.add(dBegin);
|
|
|
Calendar calBegin = Calendar.getInstance();
|
|
|
// 使用给定的 Date 设置此 Calendar 的时间
|