package com.shawn.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; 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 Map mapChDate = new HashMap(); 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)); } /** @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(); } 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; } public int getValue() { return this.value; } } /** * * 毛龙飞 * @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(); } }