1. 程式人生 > >日期工具類 DateUtils(繼承org.apache.commons.lang.time.DateUtils類)

日期工具類 DateUtils(繼承org.apache.commons.lang.time.DateUtils類)

/**
 *
 */
package com.dsj.gdbd.utils.web;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;

/**
 * 日期工具類, 繼承org.apache.commons.lang.time.DateUtils類
 *
 * @author
ThinkGem * @version 2014-4-15 */ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
"yyyy.MM.dd HH:mm", "yyyy.MM"}; /** * 日期列舉常量 */ public enum Pattern { year, month, day, minutes, second, millisecond, hour } ; /** * 得到當前日期字串 格式(yyyy-MM-dd) */ public static String getDate() { return getDate("yyyy-MM-dd"); }
/** * 得到當前日期字串 格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */ public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); } /** * 得到日期字串 預設格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */ public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * 得到日期時間字串,轉換格式(yyyy-MM-dd HH:mm:ss) */ public static String formatDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前時間字串 格式(HH:mm:ss) */ public static String getTime() { return formatDate(new Date(), "HH:mm:ss"); } /** * 得到當前日期和時間字串 格式(yyyy-MM-dd HH:mm:ss) */ public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前年份字串 格式(yyyy) */ public static String getYear() { return formatDate(new Date(), "yyyy"); } /** * 得到當前月份字串 格式(MM) */ public static String getMonth() { return formatDate(new Date(), "MM"); } public static String getYearMonth(Date date) { return formatDate(date, "yyyy-MM"); } /** * 得到當天字串 格式(dd) */ public static String getDay() { return formatDate(new Date(), "dd"); } /** * 得到當前星期字串 格式(E)星期幾 */ public static String getWeek() { return formatDate(new Date(), "E"); } /** * 日期型字串轉化為日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd", * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" } */ public static Date parseDate(Object str) { if (str == null) { return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * 獲取過去的天數 * * @param date * @return */ public static long pastDays(Date date) { long t = new Date().getTime() - date.getTime(); return t / (24 * 60 * 60 * 1000); } /** * 獲取過去的小時 * * @param date * @return */ public static long pastHour(Date date) { long t = new Date().getTime() - date.getTime(); return t / (60 * 60 * 1000); } /** * 獲取過去的分鐘 * * @param date * @return */ public static long pastMinutes(Date date) { long t = new Date().getTime() - date.getTime(); return t / (60 * 1000); } /** * 轉換為時間(天,時:分:秒.毫秒) * * @param timeMillis * @return */ public static String formatDateTime(long timeMillis) { long day = timeMillis / (24 * 60 * 60 * 1000); long hour = (timeMillis / (60 * 60 * 1000) - day * 24); long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss; } /** * 獲取兩個日期之間的天數 * * @param before * @param after * @return */ public static long getDistanceOfTwoDate(Date before, Date after) { long beforeTime = before.getTime(); long afterTime = after.getTime(); return (afterTime - beforeTime) / (1000 * 60 * 60 * 24); } /** * 獲取上個月第一天 */ public static String getLastMonthFristDay() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } /** * 加月 */ public static String getMonthFristDay(Date date, Integer n) { Calendar calendar = DateToCalendar(date); calendar.add(Calendar.MONTH, -n); calendar.set(Calendar.DAY_OF_MONTH, 1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } /** * 加月 */ public static Date getMonthFristDayMonth(Date date, Integer n) { Calendar calendar = DateToCalendar(date); calendar.add(Calendar.MONTH, -n); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTime(); } public static String getMonthFristDay(String yyyyMM) { Calendar calendar = DateToCalendar(parseDate(yyyyMM + "-01")); calendar.add(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } public static Date getLastMonthFristDay(Date date) { Calendar calendar = DateToCalendar(date); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTime(); } /** * 獲取上個月最後一天 */ public static String getLastMonthLastDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } public static String nextMonthFirstDate(String yyyyMM) { Calendar calendar = DateToCalendar(parseDate(yyyyMM + "-01")); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } public static String nextMonthFirstDate() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); return DateUtils.formatDate(calendar.getTime(), "yyyy-MM-dd"); } public static Date strToDate(String dstr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小寫的mm表示的是分鐘 try { return sdf.parse(dstr); } catch (ParseException e) { return null; } } public static Long getStrToDateTime(String dstr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小寫的mm表示的是分鐘 try { return sdf.parse(dstr).getTime() / 1000; } catch (ParseException e) { e.printStackTrace(); return null; } } public static Long getStrToDateTimeAddOne(String dstr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小寫的mm表示的是分鐘 try { Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(dstr)); cal.add(Calendar.DATE, 1); return cal.getTimeInMillis() / 1000; } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 計算兩個日期間的差值 * * @param date1 * 日期1 * @param date2 * 日期2 * @param p * 日期常量 * @return date1-date2 的整型值 */ /** * Java日期轉Unix 時間 */ public static long toUnixTime(Date date) { return date.getTime() / 1000; } /** * Unix 時間轉 Java日期 */ public static Date toDate(long unixTime) { Long timestamp = unixTime * 1000; return new Date(timestamp); } /** * 判斷一個時間是否是兩個時間的區間 * * @return */ public static boolean isTwoDateSection(Date beforeDate, Date afterDate, Date currentDate) { if (currentDate.after(beforeDate) && currentDate.before(afterDate)) { return true; } else { return false; } } /** * 日期加dayNum天 */ public static Date addDay(Date date, int dayNum) { if (null == date) { return date; } Calendar c = Calendar.getInstance(); c.setTime(date); // 設定當前日期 c.add(Calendar.DATE, dayNum); // 日期加dayNum天 date = c.getTime(); return date; } public static int dateDiff(Date date1, Date date2, Pattern p) { int diff = 0; Calendar cal1 = Calendar.getInstance(); cal1.clear(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.clear(); cal2.setTime(date2); switch (p) { case year: { int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); diff = year1 - year2; break; } case month: { int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); int month1 = cal1.get(Calendar.MONTH); int month2 = cal2.get(Calendar.MONTH); System.out.println(month1 + " " + month2); diff = month1 + (year1 - year2) * 12 - month2; break; } case minutes: { int minute1 = cal1.get(Calendar.MINUTE); int minute2 = cal2.get(Calendar.MINUTE); diff = minute1 - minute2; break; } case second: { int second1 = cal1.get(Calendar.SECOND); int second2 = cal2.get(Calendar.SECOND); diff = second1 - second2; break; } case millisecond: { int mil1 = cal1.get(Calendar.MILLISECOND); int mil2 = cal2.get(Calendar.MILLISECOND); diff = mil1 - mil2; break; } default: break; } return diff; } /** * 根據當前所過時間的毫秒數返回日期實體 * * @param milliseconds 毫秒數 * @return */ public static Date getDateByMillSec(long milliseconds) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(milliseconds * 1000); return c.getTime(); } /** * 根據當前所過時間的秒數返回日期實體 * * @param milliseconds 毫秒數 * @return */ public static Date getDateBysecondSec(long second) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(second); return c.getTime(); } /** * 把String型別的日期轉換成Date型別 */ public static Date parseDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 當前時間減去12個月得到的時間 * * @return */ public static String getDateDiff() { Calendar c = Calendar.getInstance(); String now = formatDate(c.getTime(), "yyyy-MM-dd"); c.add(Calendar.MONTH, -11); String lnow = formatDate(c.getTime(), "yyyy-MM-dd"); return now + "@" + lnow; } /** * 得到系統當前時間 */ public static String getNowDate() { Calendar c = Calendar.getInstance(); String now = formatDate(c.getTime(), "yyyy-MM-dd"); return now; } public static String getNowDate(String formatDate) { Calendar c = Calendar.getInstance(); String now = formatDate(c.getTime(), formatDate); return now; } /** * 得到當前時間減去12個月的時間 * * @return */ public static String getNowMonthsubtraction12() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -11); String snow = formatDate(c.getTime(), "yyyy-MM-dd"); return snow; } public static String getNextDateStr() { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, +1); String snow = formatDate(c.getTime(), "yyyy-MM-dd"); return snow; } /** * 得到開始日期和結束日期之間的日期 * * * @param startDate 開始日期 * @param endDate 結束日期 * @param noDay 是否不顯示日 * @return */ public static List<String> getDateMonth(String startDate, String endDate, boolean noDay) { Date date1 = parseDate(startDate); Date date2 = parseDate(endDate); List<String> list = new ArrayList<String>(); int numMonth = dateDiff(date1, date2, Pattern.month); System.out.println(numMonth); for (int i = 0; i <= numMonth; i++) { Calendar c1 = new GregorianCalendar(); c1.setTime(date1); c1.add(Calendar.MONTH, -i); if (noDay == true) { list.add(formatDate(c1.getTime(), "yyyy-MM")); } else { list.add(formatDate(c1.getTime(), "yyyy-MM-dd")); } } Collections.reverse(list); return list; } /** * 得到一個月中的最後一天 */ public static Integer getMonthLastDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.get(Calendar.DATE); } /** * 日期轉日曆 * * @param date * @return Calendar */ public static Calendar DateToCalendar(Date date) { Calendar startdate = Calendar.getInstance(); startdate.setTime(date); return startdate; } public static String getMDate(Date date) { String date1 = DateUtils.formatDate(date, "yyyy年MM月"); return date1; } public static String getYMDDate(Date date) { String date1 = DateUtils.formatDate(date, "yyyy年MM月dd日"); return date1; } // 得到當前時間的下個月。 public static Date getNextDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, +1); return calendar.getTime(); } /** * 得到當前時間的上個月 * * @param date * @return * @author zxr */ public static Date getPreDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, -1); return calendar.getTime(); } /** * 得到時間的前一天。 */ public static Date getUpDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); } public static Date getUpDate(Date date, Integer day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, day); return calendar.getTime(); } /** * 得到時間的前7天。 */ public static Date getUpDate7(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -7); return calendar.getTime(); } /** * 得到時間的下一天。 */ public static Date getDownDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, +1); return calendar.getTime(); } /** * 得到時間的下七天。 */ public static Date getDownDate7(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, +7); return calendar.getTime(); } /** * 獲取當期時間 author gzb * * @return */ public static String getNowTime() { Date dates = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); String now = sdf.format(dates); return now; } /** * 得打本季度 * * @return */ public static Integer getQuarter() { Calendar calendar = Calendar.getInstance(); int quarter = calendar.get(Calendar.MONTH); if (quarter <= 12 && quarter >= 10) return 10; if (quarter <= 9 && quarter >= 7) return 7; if (quarter <= 6 && quarter >= 4) return 4; if (quarter <= 3 && quarter >= 1) return 1; return 0; } /** * 得到當前時間減去12個月的時間 * * @return */ public static Date minusHour(Date date, int hour) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.HOUR, hour); return c.getTime(); } public static String getYesterday() { Calendar d = Calendar.getInstance(); d.add(Calendar.DATE, -1); return DateUtils.formatDate(d.getTime(), "yyyy-MM-dd"); } /** * 得到指定日期的開始 如2010-01-10 00:00 * * @param d * @author zdw */ public static Date getNowdayBegin(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); return c.getTime(); } public static Date getNowdayBeginMILLISECOND(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } public static Date getNowdayEndMILLISECOND(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); c.set(Calendar.MILLISECOND, 59); return c.getTime(); } /** * 得到指定日期的結束 如2010-01-10 23:59 * * @param d * @author zdw */ public static Date getNowdayEnd(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); return c.getTime(); } /** * 得到當前時間減去3個月的時間 * * @return */ public static String getNowMonthsubtraction3() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -2); return formatDate(c.getTime(), "yyyy-MM-dd"); } /** * 得到指定時間的開始時間 如:2010-10-10 00:00 * * @return */ public static Date getBegin(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0); return c.getTime(); } /** * 得到指定時間的結束時間 如:2010-10-10 23:59 * * @return */ public static Date getEnd(Date d) { Calendar c = Calendar.getInstance(); c.setTime(d); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59); return c.getTime(); } /** * 將指定格式日期形式的字串轉換成日期型別 * <p> * <pre> * 常用日期格式有:精確到秒的形式 &quot;yyyy-MM-dd HH:mm:ss&quot;,精確到日的形式 &quot;yyyy-MM-dd&quot; * * 例如,將字串&quot;2009-12-24 12:09:35&quot;轉換成日期型別,則需要將引數strFormat置為 * &quot;yyyy-MM-dd HH:mm:ss&quot;形式,這樣就能將其轉換為日期型別的了。 * </pre> * * @param strDate - 需要轉換的日期(字串) * @param strFormat - 需要格式化日期(字串)的格式 * @return - 日期 */ public static Date string2Date(String strDate, String strFormat) { try { SimpleDateFormat sdf = new SimpleDateFormat(strFormat); if (strDate.length() == 16) { strDate += ":00"; } else if (strDate.length() == 10) { strDate += " 00:00:00"; } Date date = sdf.parse(strDate); return date; } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 將"yyyy-MM-dd"格式的日期字串轉換為日期型別 * * @param strDate - 需要轉換的日期字串 * @return - 日期型別 */ public static Date string2Date(String strDate) { return string2Date(strDate, "yyyy-MM-dd"); } /** * 將日期轉換成指定格式的字串型別. 常用日期格式有:精確到秒的形式 "yyyy-MM-dd HH:mm:ss",精確到日的形式 * "yyyy-MM-dd" * * @param date - 需要轉換的日期 * @param format - 日期格式 * @return - 轉換成的字串 */ public static final String date2String(Date date, String format) { if (date == null || format == null) return ""; SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * 將日期轉換成指定格式( "yyyy-MM-dd")的字串型別. * * @param date - 需要轉換的日期 * @return - 轉換成的字串 */ public static final String date2String(Date date) { return date2String(date, "yyyy-MM-dd"); } /** * 計算兩個日期之間的秒差 * * @param a * @param b * @return */ public static int getBetweenMin(Date a, Date b) { long dayNumber = 0; // 1小時=60分鐘=3600秒=3600000 long mins = 1000L; // long day= 24L * 60L * 60L * 1000L;計算天數之差 try { dayNumber = (a.getTime() - b.getTime()) / mins; } catch (Exception e) { } if (dayNumber < 0) { return 300; } return (int) dayNumber; } public static Long getBetweenSub(Date a, Date b) { long dayNumber = 0; // 1小時=60分鐘=3600秒=3600000 long mins = 1000L; // long day= 24L * 60L * 60L * 1000L;計算天數之差 dayNumber = (a.getTime() - b.getTime()) / mins; return dayNumber; } public static boolean getBetweenBig(Date a, Date b) { long dayNumber = 0; // 1小時=60分鐘=3600秒=3600000 long mins = 1000L; // long day= 24L * 60L * 60L * 1000L;計算天數之差 try { dayNumber = (a.getTime() - b.getTime()) / mins; } catch (Exception e) { e.printStackTrace(); } if (dayNumber < 0) { return false; } return true; } public static final String DEFAULT_PATTERN = "yyyy-MM-dd"; public static final String DEFAULT_TIME = "yyyy-MM-dd HH:mm:ss"; public static final String DEFAULT_TIME_NAME = "yyyy-MM-ddHHmmss"; private static final String DATE_EL = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))" + "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" + "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|" + "([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?" + "((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])" + "|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])" + "|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])" + "((\\s)|(\\:([0-5]?[0-9])))))?$"; /** * 日期轉換常用格式 * * @author sunhaiyang */ public enum Format { YYYYMMDD("yyyyMMdd"), YYYY_MM_DD("yyyy-MM-dd"), YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm"), YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss"), YYYYMMDDHHMMSS("yyyyMMddHHmmss"), DDMMYYYY("ddMMMyy"); private final String value; Format(String value) { this.value = value; } public String value() { return value; } } /** * 把String型別的日期轉換成Date型別 */ public static Date parseDate(String date, String pattern) { final SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 日期轉換的方法 * * @param date * @param format * @return String */ public static String formatDate(Date date, String fmt) { if (null != date) { final SimpleDateFormat sdf = new SimpleDateFormat(fmt); return sdf.format(date); } return ""; } /** * <B>功能簡述</B><br> * EBE AV 日期查詢處理 * * @param dates * @return String * @author xiaojingze */ public static String dealDate(String dates) { String month = getMMM(parseDate(dates, Format.YYYY_MM_DD.value())); String day = dates.split("-")[2]; return day + month; } /** * 得到當前月的第一天 */ public static Date getMonthDay(String f) { final Calendar calendar = Calendar.getInstance(); if ("first".equals(f)) { calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); } else { calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return calendar.getTime(); } } /** * 得到去年的日期 * * @return */ public static Date getLastYear() { final Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.YEAR, -1); return calendar.getTime(); } /** * 得到制定時間的年,月,日 已int 型別返回 */ public static int getDatePattern(Date date, Pattern p) { final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); switch (p) { case year: return calendar.get(Calendar.YEAR); case month: return calendar.get(Calendar.MONTH) + 1; case day: return calendar.get(Calendar.DATE); case hour: return calendar.get(Calendar.HOUR); default: break; } return 0; } /** * 匹配英文表示的月份 * * @param date 日期 */ public static String getMMM(Date date) { final String mmm = ""; final int month = getDatePattern(date, Pattern.month); switch (month) { case 1: return "JAN"; case 2: return "FEB"; case 3: return "MAR"; case 4: return "APR"; case 5: return "MAY"; case 6: return "JUN"; case 7: return "JUL"; case 8: return "AUG"; case 9: return "SEP"; case 10: return "OCT"; case 11: return "NOV"; case 12: return "DEC"; default: break; } return mmm; } /** * <B>功能簡述</B><br> * 根據英文簡拼得到 月份 * * @param mmm 簡拼 * @return 月份 */ public static String getMonth(String mmm) { String m = null; if (mmm.equalsIgnoreCase("JAN")) { m = "01"; } else if (mmm.equalsIgnoreCase("FEB")) { m = "02"; } else if (mmm.equalsIgnoreCase("MAR")) { m = "03"; } else if (mmm.equalsIgnoreCase("APR")) { m = "04"; } else if (mmm.equalsIgnoreCase("MAY")) { m = "05"; } else if (mmm.equalsIgnoreCase("JUN")) { m = "06"; } else if (mmm.equalsIgnoreCase("JUL")) { m = "07"; } else if (mmm.equalsIgnoreCase("AUG")) { m = "08"; } else if (mmm.equalsIgnoreCase("SEP")) { m = "09"; } else if (mmm.equalsIgnoreCase("OCT")) { m = "10"; } else if (mmm.equalsIgnoreCase("NOV")) { m = "11"; } else if (mmm.equalsIgnoreCase("DEC")) { m = "12"; } return m; } /** * 判斷是否為時間型別 * * @param date * @return * @date Dec 14, 2011 4:00:46 PM * @comment */ public static boolean isDateType(String date) { final java.util.regex.Pattern p = java.util.regex.Pattern.compile(DATE_EL); final Matcher m = p.matcher(date); return m.matches(); } /** * 獲取給定日期所在周的第一天 * * @param date * @return */ public static Date getTheFirstDayOfWeek(Date date) { final Calendar cal = Calendar.getInstance(); cal.setTime(date); return getADayOfWeek(cal, Calendar.MONDAY).getTime(); } /** * 獲取給定日期所在周的最後一天 * * @param date * @return */ public static Date getTheLastDayOfWeek(Date date) { final Calendar cal = Calendar.getInstance(); cal.setTime(date); return getADayOfWeek(cal, Calendar.SUNDAY).getTime(); } /** * 獲取給定日期上一週的第一天 * * @param date * @return */ public static Date getTheFirstDayOfPreWeek(Date date) { final Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, -Calendar.DAY_OF_WEEK); return getADayOfWeek(cal, Calendar.MONDAY).getTime(); } /** * 獲取給定日期上一週的最後一天 * * @param date * @return */ public static Date getTheLastDayOfPreWeek(Date date) { final Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, -Calendar.DAY_OF_WEEK); return getADayOfWeek(cal, Calendar.SUNDAY).getTime(); } /** * 獲取給定日期所在周的某一天 * * @param day * @param dayOfWeek * @return */ private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) { final int currDayOfWeek = day.get(Calendar.DAY_OF_WEEK); final int week = 7; if (currDayOfWeek == dayOfWeek) { return day; } int diffDay = dayOfWeek - currDayOfWeek; if (currDayOfWeek == Calendar.SUNDAY) { diffDay -= week; } else if (dayOfWeek == Calendar.SUNDAY) { diffDay += week; } day.add(Calendar.DATE, diffDay); return day; } /** * 獲取當前時間的小時 * * @return */ public static Integer getCurrentHour() { final Calendar cal = Calendar.getInstance(); return cal.get(Calendar.HOUR_OF_DAY); } public static int getWeekOfDate(Date date) { final Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return (calendar.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** * <B>功能簡述</B><br> * 根據給定日期和需要減少天數,獲取減少後的日期 * * @param date 修改時間 * @param day 減少天數(正整數) * @return [返回型別說明] * @author */ public static Date getBeforeDate(Date date, long day) { if (date != null) { final Calendar calendar = Calendar.getInstance(); long time = date.getTime() - (day * 24 * 60 * 60 * 1000); calendar.setTimeInMillis(time); return calendar.getTime(); } return null; } public static Date getBeforeDateM(Date date, int month) { if (date != null) { final Calendar calendar = Calendar.getInstance(); calendar.add(calendar.MONTH, month); return calendar.getTime(); } return null; } /** * <B>功能簡述</B><br> * 根據給定日期和需要增加天數,獲取增加後的日期 * * @param date 修改時間 * @param day 增加天數(正整數) * @return [返回型別說明] * @author */ public static Date getAfterDate(Date date, long day) { if (date != null) { final Calendar calendar = Calendar.getInstance(); long time = date.getTime() + (day * 24 * 60 * 60 * 1000); calendar.setTimeInMillis(time); return calendar.getTime(); } return null; } public static String getWeeByDate(Date dt) { String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } public static List<String> getSevenDate() { List<String> dateList = new ArrayList<String>(); Date date = DateUtils.getAfterDate(new Date(), -6); for (int i = 0; i < 7; i++) { dateList.add(DateUtils.date2String(DateUtils.getAfterDate(date, i))); } return dateList; } /** * 計算兩個日期之間相差的天數 * * @param smdate 較小的時間 * @param bdate 較大的時間 * @return 相差天數 * @throws ParseException */ public static int daysBetween(Date smdate, Date bdate) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); smdate = sdf.parse(sdf.format(smdate)); bdate = sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } catch (Exception e) { return -1; } } public static String getRepTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); return sdf.format(new Date()); } public static final Date dateFormat(Date date) { return string2Date(date2String(date, "yyyy-MM-dd")); } public static void main(String[] args) { System.out.println(formatDateTHH(new Date())); } public static String formatDateTHH(Date date) { String formatDate = getRepTime(); return formatDate.substring(0, 8) + "T" + formatDate.substring(7, formatDate.length() - 1); } public static String formatDateyMdHm(Date date, String form) { SimpleDateFormat formatter = new SimpleDateFormat(form); return formatter.format(date); } }