123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package com.szwl.model.utils;
- 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 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));
- }
- /** @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();
- }
- /**
- * 获取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;
- }
- }
|