package com.jbx.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; public class DateUtil { /** * @param date * 日期类型 * @return String yyyy-MM-dd 日期字符串 * */ static public String getDateStr(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(date); } /** * @param date * 字符串日期类型 * @return Date yyyyMMdd 日期类型 * */ static public Date getDateFromYYYYMMDD(String date) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); return format.parse(date); } /** * @param date * 日期类型 * @return String MM-dd 日期字符串 * */ static public String getDateStrMMDD(Date date) { SimpleDateFormat format = new SimpleDateFormat("MM-dd"); return format.format(date); } /** * @param date * 日期类型 * @return String HH:mm:ss 日期字符串(24小时制) * */ static public String getDateStrForTime(Date date) { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); return format.format(date); } /** * @param date * 日期类型 * @return String yyyy 日期字符串 * */ static public String getYear(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy"); return format.format(date); } /** * @param date * 日期类型 * @return String yyyy年MM月dd日 日期字符串 * */ static public String getDateStrC(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); return format.format(date); } /** * @param date * 日期类型 * @return String yyyyMMdd 日期字符串 * */ static public String getDateStrCompact(Date date) { if (date == null) return ""; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String str = format.format(date); return str; } /** * @param date * 日期类型 * @return String HHmmss 日期字符串(24小时制) * */ static public String getDateStrCompact2(Date date) { if (date == null) return ""; SimpleDateFormat format = new SimpleDateFormat("HHmmss"); String str = format.format(date); return str; } /** * 格式化时间 * * @param date:Date类型的数据 * @return YYYYMMDDhhmm格式的字符串 */ public static String getFormatDate(Date date){ if(null==date){ return "-"; } else{ java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); return format.format(date); } } /** * 格式化时间 * * @param date:Date类型的数据 * @return MM月dd日格式的字符串 */ public static String getFormatMMddDate(Date date){ if(null==date){ return "-"; } else{ java.text.DateFormat format = new java.text.SimpleDateFormat("MM月dd日"); return format.format(date); } } /** * @param date * 日期类型 * @return String yyyy-MM-dd HH:mm:ss 日期字符串(24小时制) * */ static public String getDateTimeStr(Date date) { if (date == null) { return ""; } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); } /** * @param date * 日期类型 * @return String yyyy年MM月dd日 HH时mm分ss秒 日期字符串(24小时制) * */ static public String getDateTimeStrC(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); return format.format(date); } /** * @param pattern * 日期格式化类型 如:yyyy-MM-dd yyyyMMdd等 * @return String 日期字符串(当前时间 24小时制) * */ public static String getCurDateStr(String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(new Date()); } /** * @param s * 字符串日期类型 yyyy-MM-dd格式 * @return date 日期类型 * */ static public Date parseDate(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(s); } /** * @param s * 字符串日期类型 yyyy-MM-dd格式 * @return date 日期类型 * */ static public Date parseDateC(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(s); } /** * @param s * 字符串日期类型 yyyy-MM-dd HH:mm:ss格式 * @return date 日期类型 * */ static public Date parseDateTime(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.parse(s); } /** * @param s * 字符串日期类型 yyyy-MM-dd HH:mm格式 * @return date 日期类型 * */ static public Date parseDateTime2(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return format.parse(s); } /** * @param s * 字符串日期类型 yyyy年MM月dd日 HH时mm分ss秒格式 * @return date 日期类型 * */ static public Date parseDateTimeC(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); return format.parse(s); } /** * @param s * 字符串日期类型 HH:mm:ss格式 * @return date 日期类型 * */ static public Date parseTime(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); return format.parse(s); } /** * @param s * 字符串日期类型 HH时mm分ss秒格式 * @return date 日期类型 * */ static public Date parseTimeC(String s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("HH时mm分ss秒"); return format.parse(s); } /** * @param date 日期类型 * */ static public int yearOfDate(Date date) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String d = format.format(date); return Integer.parseInt(d.substring(0, 4)); } static public int monthOfDate(Date s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String d = format.format(s); return Integer.parseInt(d.substring(5, 7)); } static public int dayOfDate(Date s) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String d = format.format(s); return Integer.parseInt(d.substring(8, 10)); } static public String getDateTimeStr(java.sql.Date date, double time) { int year = date.getYear() + 1900; int month = date.getMonth() + 1; int day = date.getDate(); String dateStr = year + "-" + month + "-" + day; Double d = new Double(time); String timeStr = String.valueOf(d.intValue()) + ":00:00"; return dateStr + " " + timeStr; } /** * Get the total month from two date. * * @param sd * the start date * @param ed * the end date * @return int month form the start to end date * @throws ParseException */ static public int diffDateM(Date sd, Date ed) throws ParseException { return (ed.getYear() - sd.getYear()) * 12 + ed.getMonth() - sd.getMonth() + 1; } static public int diffDateD(Date sd, Date ed) throws ParseException { return Math.round((ed.getTime() - sd.getTime()) / 86400000) + 1; } static public int diffDateM(int sym, int eym) throws ParseException { return (Math.round(eym / 100) - Math.round(sym / 100)) * 12 + (eym % 100 - sym % 100) + 1; } static public java.sql.Date getNextMonthFirstDate(java.sql.Date date) throws ParseException { Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.MONTH, 1); scalendar.set(Calendar.DATE, 1); return new java.sql.Date(scalendar.getTime().getTime()); } static public java.sql.Date getFrontDateByDayCount(java.sql.Date date, int dayCount) throws ParseException { Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.DATE, -dayCount); return new java.sql.Date(scalendar.getTime().getTime()); } /** * Get first day of the month. * * @param year * the year * @param month * the month * @return Date first day of the month. * @throws ParseException */ static public Date getFirstDay(String year, String month) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(year + "-" + month + "-1"); } static public Date getFirstDay(int year, int month) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(year + "-" + month + "-1"); } static public Date getLastDay(String year, String month) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse(year + "-" + month + "-1"); Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.MONTH, 1); scalendar.add(Calendar.DATE, -1); date = scalendar.getTime(); return date; } static public Date getLastDay(int year, int month) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse(year + "-" + month + "-1"); Calendar scalendar = new GregorianCalendar(); scalendar.setTime(date); scalendar.add(Calendar.MONTH, 1); scalendar.add(Calendar.DATE, -1); date = scalendar.getTime(); return date; } /** * getToday get todat string with format YYYY-MM-DD from a Date object * * @param date * date * @return String */ static public String getTodayStr() throws ParseException { Calendar calendar = Calendar.getInstance(); return getDateStr(calendar.getTime()); } static public Date getToday() throws ParseException { return new Date(System.currentTimeMillis()); } static public String getTodayAndTime() { return new Timestamp(System.currentTimeMillis()).toString(); } static public String getTodayC() throws ParseException { Calendar calendar = Calendar.getInstance(); return getDateStrC(calendar.getTime()); } static public int getThisYearMonth() throws ParseException { Date today = Calendar.getInstance().getTime(); return (today.getYear() + 1900) * 100 + today.getMonth() + 1; } static public int getYearMonth(Date date) throws ParseException { return (date.getYear() + 1900) * 100 + date.getMonth() + 1; } // 获取相隔月数 static public long getDistinceMonth(String beforedate, String afterdate) throws ParseException { SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd"); long monthCount = 0; try { java.util.Date d1 = d.parse(beforedate); java.util.Date d2 = d.parse(afterdate); monthCount = (d2.getYear() - d1.getYear()) * 12 + d2.getMonth() - d1.getMonth(); // dayCount = (d2.getTime()-d1.getTime())/(30*24*60*60*1000); } catch (ParseException e) { System.out.println("Date parse error!"); // throw e; } return monthCount; } // 获取相隔天数 static public long getDistinceDay(String beforedate, String afterdate) throws ParseException { SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd"); long dayCount = 0; try { java.util.Date d1 = d.parse(beforedate); java.util.Date d2 = d.parse(afterdate); dayCount = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000); } catch (ParseException e) { System.out.println("Date parse error!"); // throw e; } return dayCount; } // 获取相隔天数 static public long getDistinceDay(Date beforedate, Date afterdate) throws ParseException { long dayCount = 0; try { dayCount = (afterdate.getTime() - beforedate.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { // System.out.println("Date parse error!"); // // throw e; } return dayCount; } static public long getDistinceDay(java.sql.Date beforedate, java.sql.Date afterdate) throws ParseException { long dayCount = 0; try { dayCount = (afterdate.getTime() - beforedate.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { // System.out.println("Date parse error!"); // // throw e; } return dayCount; } // 获取相隔天数 static public long getDistinceDay(String beforedate) throws ParseException { return getDistinceDay(beforedate, getTodayStr()); } // 获取相隔小时数 static public long getDistinceTime(String beforeDateTime, String afterDateTime) throws ParseException { SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long timeCount = 0; try { java.util.Date d1 = d.parse(beforeDateTime); java.util.Date d2 = d.parse(afterDateTime); timeCount = (d2.getTime() - d1.getTime()) / (60 * 60 * 1000); } catch (ParseException e) { System.out.println("Date parse error!"); throw e; } return timeCount; } /** * 获取两个日期的相隔小时数 * @param beforeDateTime 起始日期 * @param afterDateTime 结束日期 * @return * @throws ParseException * @author cuiqc 2018-01-15 */ static public long getDistinceTime(Date beforeDateTime, Date afterDateTime) throws ParseException { //afterDateTime该日期在beforeDateTime日期之后,也就是大于 if(null != beforeDateTime && null != afterDateTime && afterDateTime.after(beforeDateTime)){ long timeCount = afterDateTime.getTime() - beforeDateTime.getTime(); long hour = timeCount / (60 * 60 * 1000); return hour; } return 0; } // 获取相隔时间中文描述 static public String getDistinceTimeC(Date beforeDateTime, Date afterDateTime){ long timeCountH = (afterDateTime.getTime() - beforeDateTime.getTime()) / (60 * 60 * 1000); long timeCountM = ((afterDateTime.getTime() - beforeDateTime.getTime()) % (60 * 60 * 1000)) / (60 * 1000); if(0==timeCountM){ return timeCountH + "小时"; }else{ return timeCountH + "小时" + timeCountM + "分钟"; } } // 获取相隔时间中文描述 static public String getDistinceTimeByLan(Date beforeDateTime, Date afterDateTime,String lanType){ long timeCountH = (afterDateTime.getTime() - beforeDateTime.getTime()) / (60 * 60 * 1000); long timeCountM = ((afterDateTime.getTime() - beforeDateTime.getTime()) % (60 * 60 * 1000)) / (60 * 1000); if ("zh_TW".equals(lanType)) { return timeCountH + "小時" + timeCountM + "分鍾"; }else if ("en_US".equals(lanType)){ return timeCountH + "hours" + timeCountM + "minutes"; }else{ return timeCountH + "小时" + timeCountM + "分钟"; } } // 获取相隔时间中文描述 static public String getDistinceTimeTW(Date beforeDateTime, Date afterDateTime){ long timeCountH = (afterDateTime.getTime() - beforeDateTime.getTime()) / (60 * 60 * 1000); long timeCountM = ((afterDateTime.getTime() - beforeDateTime.getTime()) % (60 * 60 * 1000)) / (60 * 1000); return timeCountH + "小時" + timeCountM + "分鍾"; } /** * 获取时间中文描述,飞行时间格式:03:20 ,字符串格式 * @param time 飞行时间 * @param lanType 语言 * @return */ static public String getDistinceTimeByLan(String time,String lanType){ String times[] = time.split(":"); String timeCountH = times[0]; String timeCountM = times[1]; if ("zh_TW".equals(lanType)) { return timeCountH + "小時" + timeCountM + "分鍾"; }else if ("en_US".equals(lanType)){ return timeCountH + "hours" + timeCountM + "minutes"; }else{ return timeCountH + "小时" + timeCountM + "分钟"; } } /** * @param date * @author Uncle_Dallas 主要用于航班查询模块 * @date 2011-03-14 * @return HH:MM:SS的字符串 * */ public static String getCommonTimeFormat(Date date) { return getDateTimeStr(date).split(" ")[1]; } /** * @param date * @author Uncle_Dallas * @date 2011-03-16 * @return HHmm 的字符串 * */ static public String getDateStrToTime(Date date) { if(null == date){ return "-"; } SimpleDateFormat format = new SimpleDateFormat("HH:mm"); return format.format(date); } /** * @param date * @author Uncle_Dallas 主要用于航班查询模块 * @date 2011-03-14 * @return 15MAR11的字符串 * */ public static String getCommonDateFormat(String date) { String[] n = date.split("-"); return n[2] + getMonth(n[1], "MMM") + n[0].substring(2); } public static String getCommonFormateDate(String str) { String date = str.substring(0, 2); String month = getMonth(str.substring(2, 5), "MM"); String year = str.substring(5, 7); StringBuffer sb = new StringBuffer(); sb.append("20").append(year).append("-").append(month).append("-"); sb.append(date); return sb.toString(); } /** * 获取普通格式的日期,20110101 * * @param str * 格式如:21APR11 * @return */ public static String getCommonDate(String str) { String date = str.substring(0, 2); String month = getMonth(str.substring(2, 5), "MM"); String year = str.substring(5, 7); StringBuffer sb = new StringBuffer(); sb.append("20").append(year).append(month).append(date); return sb.toString(); } public static String getMonth(String strMonth, String rtnType) { String tempstrMonth = ""; if (strMonth.length() <= 2 && rtnType.equalsIgnoreCase("MM") || strMonth.length() == 3 && rtnType.equalsIgnoreCase("MMM")) tempstrMonth = strMonth; else { if (strMonth.length() <= 2 && rtnType.equalsIgnoreCase("MMM")) { if (strMonth.equals("01") || strMonth.equals("1")) tempstrMonth = "Jan"; if (strMonth.equals("02") || strMonth.equals("2")) tempstrMonth = "Feb"; if (strMonth.equals("03") || strMonth.equals("3")) tempstrMonth = "Mar"; if (strMonth.equals("04") || strMonth.equals("4")) tempstrMonth = "Apr"; if (strMonth.equals("05") || strMonth.equals("5")) tempstrMonth = "May"; if (strMonth.equals("06") || strMonth.equals("6")) tempstrMonth = "Jun"; if (strMonth.equals("07") || strMonth.equals("7")) tempstrMonth = "Jul"; if (strMonth.equals("08") || strMonth.equals("8")) tempstrMonth = "Aug"; if (strMonth.equals("09") || strMonth.equals("9")) tempstrMonth = "Sep"; if (strMonth.equals("10")) tempstrMonth = "Oct"; if (strMonth.equals("11")) tempstrMonth = "Nov"; if (strMonth.equals("12")) tempstrMonth = "Dec"; } if (strMonth.length() == 3 && rtnType.equalsIgnoreCase("MM")) { if (strMonth.equalsIgnoreCase("Jan")) tempstrMonth = "01"; if (strMonth.equalsIgnoreCase("Feb")) tempstrMonth = "02"; if (strMonth.equalsIgnoreCase("Mar")) tempstrMonth = "03"; if (strMonth.equalsIgnoreCase("Apr")) tempstrMonth = "04"; if (strMonth.equalsIgnoreCase("May")) tempstrMonth = "05"; if (strMonth.equalsIgnoreCase("Jun")) tempstrMonth = "06"; if (strMonth.equalsIgnoreCase("Jul")) tempstrMonth = "07"; if (strMonth.equalsIgnoreCase("Aug")) tempstrMonth = "08"; if (strMonth.equalsIgnoreCase("Sep")) tempstrMonth = "09"; if (strMonth.equalsIgnoreCase("Oct")) tempstrMonth = "10"; if (strMonth.equalsIgnoreCase("Nov")) tempstrMonth = "11"; if (strMonth.equalsIgnoreCase("Dec")) tempstrMonth = "12"; } } return tempstrMonth.toUpperCase(); } // 获取相隔时间 static public long getDistinceTime(String beforeDateTime) throws ParseException { return getDistinceTime(beforeDateTime, new Timestamp(System.currentTimeMillis()).toLocaleString()); } /** * * @param date * 传入时间 * @param pattern * 时间格式 * @return */ /** 获得中文格式的时间字符串 */ public static String getDateFormatCnStr(Date date, String pattern) { if (date == null) return ""; String result = null; try { SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE); result = sdf.format(date); } catch (Exception e) { System.out.println("时间转换出错:Date" + date.toString() + ",pattern" + pattern); e.printStackTrace(); } if (!isNull(result)) return result; return ""; } /** 判断字符串是否为null */ public static boolean isNull(String value) { return (value == null || value.trim().length() == 0); } public static final String dateToString(Date date, String s) { if (date == null || s == null) return ""; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendarToString(calendar, s); } public static final String calendarToString(Calendar calendar, String s) { if (s == null || calendar == null) return ""; String s1 = s.toUpperCase(); String as[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; int i = calendar.get(1); int j = calendar.get(2); int k = calendar.get(5); int l = calendar.get(11); int i1 = calendar.get(12); int j1 = calendar.get(13); int k1 = calendar.get(7) - 1; int l1 = s1.indexOf("YYYY"); if (l1 != -1) { s1 = s1.substring(0, l1) + i + s1.substring(l1 + 4); } else { l1 = s1.indexOf("YY"); if (l1 != -1) { // add by WYT // 修改年份为"00"是判断出错的情况 // int j2 = i <= 2000 ? i - 1900 : i - 2000; int j2 = i % 100; // end s1 = s1.substring(0, l1) + (j2 >= 10 ? String.valueOf(j2) : "0" + j2) + s1.substring(l1 + 2); } else { l1 = s1.indexOf("Y"); if (l1 != -1) { // add by WYT // 修改年份为"00"是判断出错的情况 // int j2 = i <= 2000 ? i - 1900 : i - 2000; int j2 = i % 10; // end s1 = s1.substring(0, l1) + j2 + s1.substring(l1 + 1); } } } l1 = s1.indexOf("HH"); if (l1 != -1) { s1 = s1.substring(0, l1) + (l >= 10 ? String.valueOf(l) : "0" + l) + s1.substring(l1 + 2); } else { l1 = s1.indexOf("H"); if (l1 != -1) s1 = s1.substring(0, l1) + l + s1.substring(l1 + 1); } l1 = s1.indexOf("MI"); if (l1 != -1) s1 = s1.substring(0, l1) + (i1 >= 10 ? String.valueOf(i1) : "0" + i1) + s1.substring(l1 + 2); l1 = s1.indexOf("SS"); if (l1 != -1) s1 = s1.substring(0, l1) + (j1 >= 10 ? String.valueOf(j1) : "0" + j1) + s1.substring(l1 + 2); l1 = s1.indexOf("DD"); if (l1 != -1) { s1 = s1.substring(0, l1) + (k >= 10 ? String.valueOf(k) : "0" + k) + s1.substring(l1 + 2); } else { l1 = s1.indexOf("D"); if (l1 != -1) s1 = s1.substring(0, l1) + k + s1.substring(l1 + 1); } l1 = s1.indexOf("MMM"); if (l1 != -1) { s1 = s1.substring(0, l1) + encodeMonth(j) + s1.substring(l1 + 3); } else { l1 = s1.indexOf("MM"); if (l1 != -1) { s1 = s1.substring(0, l1) + (j >= 9 ? String.valueOf(j + 1) : "0" + (j + 1)) + s1.substring(l1 + 2); } else { l1 = s1.indexOf("M"); if (l1 != -1) s1 = s1.substring(0, l1) + (j + 1) + s1.substring(l1 + 1); } } l1 = s1.indexOf("WWW"); if (l1 != -1) { s1 = s1.substring(0, l1) + as[k1].substring(0, 3) + s1.substring(l1 + 3); } else { int i2 = s1.indexOf("WW"); if (i2 != -1) s1 = s1.substring(0, i2) + as[k1].substring(0, 2) + s1.substring(i2 + 2); } return s1; } private static final String encodeMonth(int i) { String as[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; if (i >= 0 && i < 12) return as[i]; else return ""; } /** * 根据主机日期字符串格式转化成Date格式的日期 * * @param hostdate * 21APR11 * @return Date * @throws ParseException */ public static Date hostDateStr2Date(String hostdate) throws ParseException { return parseDate(getCommonFormateDate(hostdate)); } /** * 获得date的前days的日期 * * @param date * Date * @param days * int 天数 * @return String yyyy-MM-dd */ public static Date getBeforeDate(Date date, int days) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days); return calendar.getTime(); } /** * 获得date的后days的日期 * * @param date * Date * @param days * int 天数 * @return String yyyy-MM-dd */ public static Date getAfterDate(Date date, int days) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days); return calendar.getTime(); } /** * 获得date的前days的日期 * * @param date * String yyyy-MM-dd形式的字符串 * @param days * int 天数 * @return String yyyy-MM-dd */ public static String getBeforeDate(String date, int days) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date d = df.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days); return df.format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); return ""; } } /** * 获得date的前days的日期 * * @param date * String yyyyMMdd形式的字符串 * @param days * int 天数 * @return String yyyy-MM-dd */ public static String getBeforeDateStr(String date, int days) { SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); try { Date d = df.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days); return df.format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); return ""; } } /** * 获得date的后days的日期 * * @param date * String yyyy-MM-dd形式的字符串 * @param days * int 天数 * @return String yyyy-MM-dd */ public static String getAfterDate(String date, int days) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date d = df.parse(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days); return df.format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); return ""; } } /** * 通过DATE获得Week * @param datDate Date * @return */ public static String getDayOfWeek(Date datDate,Locale mylocale){ Calendar calendar = Calendar.getInstance(); calendar.setTime(datDate); int iWeek = calendar.get(Calendar.DAY_OF_WEEK)-1; if(mylocale == null) { mylocale = Locale.SIMPLIFIED_CHINESE; } String strWeek = ""; if(mylocale.getCountry().equalsIgnoreCase("CN")){ String[] sWeek = {"日","一","二","三","四","五","六"}; strWeek = sWeek[iWeek]; }else if(mylocale.getCountry().equalsIgnoreCase("TW")){ String[] sWeek = {"日","壹","二","三","四","五","六"}; strWeek = sWeek[iWeek]; }else{ String as[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; strWeek = as[iWeek]; } return strWeek; } /** * 获取两个日期之间相差多少天 * @param c1 日期1 * @param c2 日期2 * @return 天数,日期1和日期2的顺序不影响结果,结果始终为整数 */ public static int getDays(Calendar c1, Calendar c2) { if (c1.after(c2)) { Calendar swap = c1; c1 = c2; c2 = swap; } int days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR); int y2 = c2.get(Calendar.YEAR); while (c1.get(Calendar.YEAR) != y2) { days = days + c1.getActualMaximum(Calendar.DAY_OF_YEAR); c1.add(Calendar.YEAR, 1); } return days; } }
DateUtil
记录一下常用的工具类,方便使用的时候可以获取。
猜你喜欢
转载自lovemojienv.iteye.com/blog/2413241
今日推荐
周排行