|
@@ -7,260 +7,262 @@ import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-public class DateUtils
|
|
|
-{
|
|
|
- public static final String PATTERN_MMdd = "MMdd";
|
|
|
- public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
|
|
|
- public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
|
|
|
- public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
|
|
|
- public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
|
|
|
- public static final String PATTERN_yyyy_M_d = "yyyy-M-d";
|
|
|
- public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
|
|
|
- public static final String PATTERN_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
|
|
|
- public static final String PATTERN_HH_mm_ss = "HH:mm:ss";
|
|
|
- public static final String PATTERN_HHmmss = "HHmmss";
|
|
|
- public static final String PATTERN_HH = "HH";
|
|
|
- public static final String PATTERN_MMdd2 = "MM月dd";
|
|
|
- public static final String PATTERN_MM = "MM";
|
|
|
- public static final Map<String, String> mapChDate = new HashMap<String, String>();
|
|
|
-
|
|
|
- public static Date getNextday(Date now)
|
|
|
- {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(now);
|
|
|
- calendar.add(5, 1);
|
|
|
- return calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static Date getPreday(Date now)
|
|
|
- {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(now);
|
|
|
- calendar.add(5, -1);
|
|
|
- return calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatDate_ch(Date date)
|
|
|
- {
|
|
|
- if(date==null){
|
|
|
- throw new IllegalArgumentException("需要转换的对象为空");
|
|
|
+public class DateUtils {
|
|
|
+ public static final String PATTERN_MMdd = "MMdd";
|
|
|
+ public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
|
|
|
+ public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
|
|
|
+ public static final String PATTERN_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
|
|
|
+ public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
|
|
|
+ public static final String PATTERN_yyyy_M_d = "yyyy-M-d";
|
|
|
+ public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ public static final String PATTERN_yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
|
|
|
+ public static final String PATTERN_HH_mm_ss = "HH:mm:ss";
|
|
|
+ public static final String PATTERN_HHmmss = "HHmmss";
|
|
|
+ public static final String PATTERN_HH = "HH";
|
|
|
+ public static final String PATTERN_MMdd2 = "MM月dd";
|
|
|
+ public static final String PATTERN_MM = "MM";
|
|
|
+ public static final Map<String, String> mapChDate = new HashMap<String, String>();
|
|
|
+
|
|
|
+ public static Date getNextday(Date now) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(now);
|
|
|
+ calendar.add(5, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getPreday(Date now) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(now);
|
|
|
+ calendar.add(5, -1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDate_ch(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ throw new IllegalArgumentException("需要转换的对象为空");
|
|
|
+ }
|
|
|
+ String str = formatDate(date, "MMdd");
|
|
|
+ str = (String) mapChDate.get(str.substring(0, 2)) + str.substring(2);
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDate(Date date, String pattern) {
|
|
|
+ if (date == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = null;
|
|
|
+ if (pattern == null)
|
|
|
+ sdf = new SimpleDateFormat();
|
|
|
+ else {
|
|
|
+ sdf = new SimpleDateFormat(pattern);
|
|
|
+ }
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDate(Date date) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String source, String pattern) throws ParseException {
|
|
|
+ SimpleDateFormat sdf = null;
|
|
|
+ if (pattern == null)
|
|
|
+ sdf = new SimpleDateFormat();
|
|
|
+ else {
|
|
|
+ sdf = new SimpleDateFormat(pattern);
|
|
|
+ }
|
|
|
+ return sdf.parse(source);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String source) throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
+ return sdf.parse(source);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String source, Date defaultDate) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
+ return sdf.parse(source);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ }
|
|
|
+ return defaultDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String source, String pattern, Date defaultDate) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = null;
|
|
|
+ if (pattern == null)
|
|
|
+ sdf = new SimpleDateFormat();
|
|
|
+ else {
|
|
|
+ sdf = new SimpleDateFormat(pattern);
|
|
|
+ }
|
|
|
+ return sdf.parse(source);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ }
|
|
|
+ return defaultDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean tryParse(String source) throws ParseException {
|
|
|
+ try {
|
|
|
+ Integer.parseInt(source);
|
|
|
+ return true;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date changeDate(Date date, TimeType timeType, int amount) {
|
|
|
+ if ((date == null) || (timeType == null) || (amount == 0)) {
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(timeType.getValue(), amount);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isSameDay(Date date1, Date date2) {
|
|
|
+ if ((date1 == null) || (date2 == null)) {
|
|
|
+ throw new IllegalArgumentException("The date must not be null");
|
|
|
+ }
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(date1);
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(date2);
|
|
|
+ return isSameDay(cal1, cal2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isSameDay(Calendar cal1, Calendar cal2) {
|
|
|
+ if ((cal1 == null) || (cal2 == null)) {
|
|
|
+ throw new IllegalArgumentException("The date must not be null");
|
|
|
+ }
|
|
|
+ return (cal1.get(0) == cal2.get(0)) && (cal1.get(1) == cal2.get(1)) && (cal1.get(6) == cal2.get(6));
|
|
|
}
|
|
|
- String str = formatDate(date, "MMdd");
|
|
|
- str = (String)mapChDate.get(str.substring(0, 2)) + str.substring(2);
|
|
|
- return str;
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatDate(Date date, String pattern)
|
|
|
- {
|
|
|
- if(date==null){
|
|
|
- return "";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @deprecated
|
|
|
+ */
|
|
|
+ public static Date add(Date date, int calendarField, int amount) {
|
|
|
+ if (date == null) {
|
|
|
+ throw new IllegalArgumentException("The date must not be null");
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(calendarField, amount);
|
|
|
+ return c.getTime();
|
|
|
}
|
|
|
- SimpleDateFormat sdf = null;
|
|
|
- if (pattern == null)
|
|
|
- sdf = new SimpleDateFormat();
|
|
|
- else {
|
|
|
- sdf = new SimpleDateFormat(pattern);
|
|
|
+
|
|
|
+ public static Date addYears(Date date, int amount) {
|
|
|
+ return add(date, 1, amount);
|
|
|
}
|
|
|
- return sdf.format(date);
|
|
|
- }
|
|
|
-
|
|
|
- public static String formatDate(Date date) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
- return sdf.format(date);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date parseDate(String source, String pattern) throws ParseException
|
|
|
- {
|
|
|
- SimpleDateFormat sdf = null;
|
|
|
- if (pattern == null)
|
|
|
- sdf = new SimpleDateFormat();
|
|
|
- else {
|
|
|
- sdf = new SimpleDateFormat(pattern);
|
|
|
+
|
|
|
+ public static Date addMonths(Date date, int amount) {
|
|
|
+ return add(date, 2, amount);
|
|
|
}
|
|
|
- return sdf.parse(source);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date parseDate(String source) throws ParseException {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
- return sdf.parse(source);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date parseDate(String source, Date defaultDate) {
|
|
|
- try {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
- return sdf.parse(source);
|
|
|
+
|
|
|
+ public static Date addWeeks(Date date, int amount) {
|
|
|
+ return add(date, 3, amount);
|
|
|
}
|
|
|
- catch (ParseException e)
|
|
|
- {
|
|
|
+
|
|
|
+ public static Date addDays(Date date, int amount) {
|
|
|
+ return add(date, 5, amount);
|
|
|
}
|
|
|
- return defaultDate;
|
|
|
- }
|
|
|
-
|
|
|
- public static Date parseDate(String source, String pattern, Date defaultDate) {
|
|
|
- try {
|
|
|
- SimpleDateFormat sdf = null;
|
|
|
- if (pattern == null)
|
|
|
- sdf = new SimpleDateFormat();
|
|
|
- else {
|
|
|
- sdf = new SimpleDateFormat(pattern);
|
|
|
- }
|
|
|
- return sdf.parse(source);
|
|
|
+
|
|
|
+ public static Date addHours(Date date, int amount) {
|
|
|
+ return add(date, 11, amount);
|
|
|
}
|
|
|
- catch (ParseException e) {
|
|
|
+
|
|
|
+ public static Date addMinutes(Date date, int amount) {
|
|
|
+ return add(date, 12, amount);
|
|
|
}
|
|
|
- return defaultDate;
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean tryParse(String source) throws ParseException {
|
|
|
- try {
|
|
|
- Integer.parseInt(source);
|
|
|
- return true;
|
|
|
- } catch (NumberFormatException e) {
|
|
|
+
|
|
|
+ public static Date addSeconds(Date date, int amount) {
|
|
|
+ return add(date, 13, amount);
|
|
|
}
|
|
|
- return false;
|
|
|
- }
|
|
|
|
|
|
- public static Date changeDate(Date date, TimeType timeType, int amount)
|
|
|
- {
|
|
|
- if ((date == null) || (timeType == null) || (amount == 0)) {
|
|
|
- return date;
|
|
|
+ public static Date addMilliseconds(Date date, int amount) {
|
|
|
+ return add(date, 14, amount);
|
|
|
}
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTime(date);
|
|
|
- c.add(timeType.getValue(), amount);
|
|
|
- return c.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean isSameDay(Date date1, Date date2)
|
|
|
- {
|
|
|
- if ((date1 == null) || (date2 == null)) {
|
|
|
- throw new IllegalArgumentException("The date must not be null");
|
|
|
+
|
|
|
+ static {
|
|
|
+ mapChDate.put("01", "一月");
|
|
|
+ mapChDate.put("02", "二月");
|
|
|
+ mapChDate.put("03", "三月");
|
|
|
+ mapChDate.put("04", "四月");
|
|
|
+ mapChDate.put("05", "五月");
|
|
|
+ mapChDate.put("06", "六月");
|
|
|
+ mapChDate.put("07", "七月");
|
|
|
+ mapChDate.put("08", "八月");
|
|
|
+ mapChDate.put("09", "九月");
|
|
|
+ mapChDate.put("10", "十月");
|
|
|
+ mapChDate.put("11", "十一月");
|
|
|
+ mapChDate.put("12", "十二月");
|
|
|
}
|
|
|
- Calendar cal1 = Calendar.getInstance();
|
|
|
- cal1.setTime(date1);
|
|
|
- Calendar cal2 = Calendar.getInstance();
|
|
|
- cal2.setTime(date2);
|
|
|
- return isSameDay(cal1, cal2);
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean isSameDay(Calendar cal1, Calendar cal2)
|
|
|
- {
|
|
|
- if ((cal1 == null) || (cal2 == null)) {
|
|
|
- throw new IllegalArgumentException("The date must not be null");
|
|
|
+
|
|
|
+ public static enum TimeType {
|
|
|
+ SECOND(13), MINUTE(12), HOUR(10), DAY(5),
|
|
|
+ WEEK(4), MONTH(2),
|
|
|
+ YEAR(1);
|
|
|
+
|
|
|
+ private int value;
|
|
|
+
|
|
|
+ private TimeType(int value) {
|
|
|
+ this.value = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue() {
|
|
|
+ return this.value;
|
|
|
+ }
|
|
|
}
|
|
|
- return (cal1.get(0) == cal2.get(0)) && (cal1.get(1) == cal2.get(1)) && (cal1.get(6) == cal2.get(6));
|
|
|
- }
|
|
|
-
|
|
|
- /** @deprecated */
|
|
|
- public static Date add(Date date, int calendarField, int amount)
|
|
|
- {
|
|
|
- if (date == null) {
|
|
|
- throw new IllegalArgumentException("The date must not be null");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 毛龙飞
|
|
|
+ *
|
|
|
+ * @param d
|
|
|
+ * @param day
|
|
|
+ * @return
|
|
|
+ * @Description 获取日期的后几天
|
|
|
+ */
|
|
|
+ public static Date getDateAfter(Date d, int day) {
|
|
|
+ Calendar now = Calendar.getInstance();
|
|
|
+ now.setTime(d);
|
|
|
+ now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
|
|
|
+ return now.getTime();
|
|
|
}
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTime(date);
|
|
|
- c.add(calendarField, amount);
|
|
|
- return c.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addYears(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 1, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addMonths(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 2, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addWeeks(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 3, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addDays(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 5, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addHours(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 11, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addMinutes(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 12, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addSeconds(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 13, amount);
|
|
|
- }
|
|
|
-
|
|
|
- public static Date addMilliseconds(Date date, int amount)
|
|
|
- {
|
|
|
- return add(date, 14, amount);
|
|
|
- }
|
|
|
-
|
|
|
- static
|
|
|
- {
|
|
|
- mapChDate.put("01", "一月");
|
|
|
- mapChDate.put("02", "二月");
|
|
|
- mapChDate.put("03", "三月");
|
|
|
- mapChDate.put("04", "四月");
|
|
|
- mapChDate.put("05", "五月");
|
|
|
- mapChDate.put("06", "六月");
|
|
|
- mapChDate.put("07", "七月");
|
|
|
- mapChDate.put("08", "八月");
|
|
|
- mapChDate.put("09", "九月");
|
|
|
- mapChDate.put("10", "十月");
|
|
|
- mapChDate.put("11", "十一月");
|
|
|
- mapChDate.put("12", "十二月");
|
|
|
- }
|
|
|
-
|
|
|
- public static enum TimeType
|
|
|
- {
|
|
|
- SECOND(13), MINUTE(12), HOUR(10), DAY(5),
|
|
|
- WEEK(4), MONTH(2),
|
|
|
- YEAR(1);
|
|
|
-
|
|
|
- private int value;
|
|
|
-
|
|
|
- private TimeType(int value) {
|
|
|
- this.value = value;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取week 0是周日
|
|
|
+ *
|
|
|
+ * @param day
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getWeek(Date day) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(day);
|
|
|
+ int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (w < 0)
|
|
|
+ w = 0;
|
|
|
+ return w;
|
|
|
}
|
|
|
|
|
|
- public int getValue() {
|
|
|
- return this.value;
|
|
|
+ /**
|
|
|
+ * 判断给定日期是否是当月的最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isLastDayOfMonth(Date date) {
|
|
|
+ //1、创建日历类
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ //2、设置当前传递的时间,不设就是当前系统日期
|
|
|
+ calendar.setTime(date);
|
|
|
+ //3、data的日期是N,那么N+1【假设当月是30天,30+1=31,如果当月只有30天,那么最终结果为1,也就是下月的1号】
|
|
|
+ calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
|
|
|
+ //4、判断是否是当月最后一天【1==1那么就表明当天是当月的最后一天返回true】
|
|
|
+ if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- /**
|
|
|
- *
|
|
|
- * 毛龙飞
|
|
|
- * @Description 获取日期的后几天
|
|
|
- * @param d
|
|
|
- * @param day
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Date getDateAfter(Date d, int day) {
|
|
|
- Calendar now = Calendar.getInstance();
|
|
|
- now.setTime(d);
|
|
|
- now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
|
|
|
- return now.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取week 0是周日
|
|
|
- * @param day
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static int getWeek(Date day) {
|
|
|
- Calendar cal = Calendar.getInstance();
|
|
|
- cal.setTime(day);
|
|
|
- int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
- if (w < 0)
|
|
|
- w = 0;
|
|
|
- return w;
|
|
|
- }
|
|
|
}
|