|
@@ -20,6 +20,7 @@ import java.util.*;
|
|
|
* @Version 1.0
|
|
|
*/
|
|
|
public class DateUtils extends PropertyEditorSupport {
|
|
|
+
|
|
|
private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
|
|
|
|
|
|
public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
|
|
@@ -66,10 +67,10 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
};
|
|
|
|
|
|
// 以毫秒表示的时间
|
|
|
- private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
|
|
|
- private static final long HOUR_IN_MILLIS = 3600 * 1000;
|
|
|
- private static final long MINUTE_IN_MILLIS = 60 * 1000;
|
|
|
- private static final long SECOND_IN_MILLIS = 1000;
|
|
|
+ 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;
|
|
|
|
|
|
// 指定模式的时间格式
|
|
|
private static SimpleDateFormat getSDFormat(String pattern) {
|
|
@@ -80,9 +81,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -95,8 +94,7 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
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 {
|
|
@@ -104,10 +102,7 @@ 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -155,58 +150,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
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>>();
|
|
|
- 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.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.addWeek(splitDate, -1);
|
|
|
- endDate = DateUtils.addWeek(endDate, -1);
|
|
|
- if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
- splitDate = startDate;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
|
|
|
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
|
|
|
- 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.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.addMonth(splitDate, -1);
|
|
|
- endDate = DateUtils.addMonth(endDate, -1);
|
|
|
- if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
- splitDate = startDate;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 当前日历,这里用中国时间表示
|
|
|
*
|
|
@@ -319,9 +262,6 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
*/
|
|
|
public static String getDate(String format) {
|
|
|
Date date = new Date();
|
|
|
- if (null == date) {
|
|
|
- return null;
|
|
|
- }
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
return sdf.format(date);
|
|
|
}
|
|
@@ -388,8 +328,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);
|
|
|
- java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
|
|
|
- return buydate;
|
|
|
+ return Timestamp.valueOf(nowTime);
|
|
|
}
|
|
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////
|
|
@@ -603,7 +542,8 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“时:分“格式显示
|
|
|
*/
|
|
|
public static String formatShortTime(Calendar cal) {
|
|
|
- return short_time_sdf.get().format(cal.getTime());
|
|
|
+ SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
|
|
|
+ return shortTimeSdf.format(cal.getTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -613,7 +553,8 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @return 指定日期按“时:分“格式显示
|
|
|
*/
|
|
|
public static String formatShortTime(Date date) {
|
|
|
- return short_time_sdf.get().format(date);
|
|
|
+ SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
|
|
|
+ return shortTimeSdf.format(date);
|
|
|
}
|
|
|
|
|
|
// ////////////////////////////////////////////////////////////////////////////
|
|
@@ -630,10 +571,15 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @param pattern 转换的匹配格式
|
|
|
* @return 如果转换成功则返回转换后的日期
|
|
|
* @throws ParseException
|
|
|
+ * @throws
|
|
|
*/
|
|
|
- public static Date parseDate(String src, String pattern) throws ParseException {
|
|
|
- return getSDFormat(pattern).parse(src);
|
|
|
-
|
|
|
+ public static Date parseDate(String src, String pattern) {
|
|
|
+ try {
|
|
|
+ return getDateFormat(pattern).parse(src);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -642,9 +588,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();
|
|
@@ -652,7 +598,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);
|
|
@@ -666,8 +612,9 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
* @param pattern 转换的匹配格式
|
|
|
* @return 如果转换成功则返回转换后的时间戳
|
|
|
* @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());
|
|
|
}
|
|
@@ -767,13 +714,15 @@ 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)) {
|
|
|
try {
|
|
|
if (text.indexOf(":") == -1 && text.length() == 10) {
|
|
|
- setValue(DateUtils.date_sdf.get().parse(text));
|
|
|
+ setValue(format.parse(text));
|
|
|
} else if (text.indexOf(":") > 0 && text.length() == 19) {
|
|
|
- setValue(DateUtils.datetimeFormat.get().parse(text));
|
|
|
+ setValue(datetimeFormat.parse(text));
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("Could not parse date, date format is error ");
|
|
|
}
|
|
@@ -805,6 +754,57 @@ public class DateUtils extends PropertyEditorSupport {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
|
|
|
+ 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<>();
|
|
|
+ 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.addWeek(splitDate, -1);
|
|
|
+ endDate = DateUtils.addWeek(endDate, -1);
|
|
|
+ if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
+ splitDate = startDate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
|
|
|
+ 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<>();
|
|
|
+ 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.addMonth(splitDate, -1);
|
|
|
+ endDate = DateUtils.addMonth(endDate, -1);
|
|
|
+ if (DateUtils.compareDate(startDate, splitDate) > 0) {
|
|
|
+ splitDate = startDate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
public static String addMonth(String dateString, int month) throws ParseException {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
Date date = sdf.parse(dateString);
|