- 浏览: 87355 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (148)
- 全文检索 (1)
- java (29)
- xml (2)
- json (2)
- redis springmvc (1)
- Redis (5)
- 开发常识 (1)
- tomcat (2)
- 单元测试Junit (2)
- 设计模式 (2)
- spring (10)
- jvm (2)
- eclipse (4)
- echart (1)
- mybatis (1)
- mysql (3)
- web (1)
- js (2)
- PL/SQL (2)
- 其他 (1)
- 人生 (1)
- 安全 (2)
- jsp (2)
- 硬件电脑 (1)
- linux (3)
- git (10)
- oracle (8)
- ant (1)
- maven (2)
- 正则表达式 (2)
- chrome (1)
- 面试 (6)
- 多线程 (19)
- bug (11)
- java工具类 (3)
- 算法 (1)
- bug,git (1)
- shell (2)
- springmvc (2)
- Java8 (1)
- 消息队列-rocketmq (1)
- es (1)
- dubbo (0)
- spring cloud (0)
- hashmap (0)
- springboot (1)
- velocity (0)
记录一下常用的工具类,方便使用的时候可以获取。
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; } }
发表评论
-
linux 文件 dos unix格式,unix和dos下文本文件得区别
2023-09-06 23:23 84问题: 当在window ... -
java 枚举values()方法
2019-07-30 20:41 380工作中,同事 ... -
newHashMapWithExpectedSize
2019-07-30 19:45 1183newHashMapWithExpectedSize VS ... -
java 8 stream应用
2019-07-15 11:43 01.生成map List<BusinessFacili ... -
技术知识点汇总
2019-02-12 15:47 01. LTS(light-task-scheduler) ... -
架构演进
2018-12-21 00:54 348一传统垂直mvc项目 垂直架构图:表示层->业务逻辑层- ... -
集群中session共享
2018-12-20 23:25 0当项目演进时,一个节点出错,如何保证域名能访问到别的节点。 一 ... -
父类子类静态代码块执行
2018-08-22 15:23 0先看一个基础面试题: package com.jbx.te ... -
数组元素的初始化
2018-07-10 17:16 462直接上代码 ,char的初始化值是'\u0000' p ... -
统计一个字符串中每个字符串出现的次数
2018-07-03 17:43 582经常遇到这个问题:总结一下 思路和代码 import ja ... -
WebTool
2018-03-15 21:32 351package com.jbx.util; im ... -
MD5
2018-03-15 21:25 318/**************************** ... -
indexOf判断一个字符串是否包含另一个字符串
2018-02-26 08:58 480jdk中的表述如下 indexOf public int in ... -
eclipse 添加反编译
2018-01-25 08:57 299为eclipse中*.clsas/*.class withou ... -
Java读取配置文件
2018-01-24 16:00 288Java读取配置文件test.properties 文件tes ... -
HTTP请求头
2017-12-29 16:49 0Request Headers 当访问一个action时,在谷 ... -
阿里巴巴Java开发手册(终极版)
2017-11-13 16:37 467前言 《阿里巴巴 Java 开发手册》是阿里巴巴集团技术团队 ... -
JavaEE 基础实用教程(二)------Jsp应用基础
2017-11-03 17:22 626习题 1.画出HTML文件的基 ... -
JavaEE 基础实用教程(一)---简述
2017-11-03 14:55 563第一部分 实用教程 第 ... -
实现普通用户登录
2017-11-03 11:09 0页面元素: 用户名,密码,验证码,登录 1.创建登录页 ...
相关推荐
DateUtil 日期工具类
python源码安装包python-dateutil-2.8.0.tar,解压后 python setup.py install进行安装.
dateutil模块
python_dateutil-2.8.0-py2.py3-none-any.whl
资源来自pypi官网。 资源全名:types-python-dateutil-0.1.4.tar.gz
一个Java 日期时间操作函数集 DateUtil.java代码,用于时间和日期操作的工具类,DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法,格式化后的系统当前时间,如果有异常产生,返回...
python_dateutil-2.8.1-py2.py3-none-any.whl官网下载很慢,把自己下载的一些上传给大家下载,速度更快。
Java DateUtil.java日期时间操作类源代码,DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法。比如日期格式转换成时间戳、得到格式化后的系统当前日期、转化日期格式"MM/dd/YY、MM....
DateUtil(日期工具类)DateUtil(日期工具类)DateUtil(日期工具类)DateUtil(日期工具类)
DateUtil(通用日期工具类)
numpy+matplotlib+six+dateutil+pytz+pyparsing python2.7 的第三方函数库,win7 64位环境下的安装包,下载安装即可。
openerp7源码编译所需要的dateutil模块包,希望可以帮助大家
dateutil类 日期、时间、星期操作类.已打jar包
python-dateutil-2.2.win-amd64-py2.7
ArrayUtil DateUtil FileUtil ReguUtil StringUtil 5个工具包
python-dateutil-2.2.win32-py2.7.exe,python-dateutil-2.2.win32-py2.7.exe
python的工具包dateUtil和pyparsing,安装配置matplotlib包时可能会需要
快速日期格式化类 ,线程安全的 包括:获取 DateUtil.STYLE格式的日期 字符转日期 日期转字符 字符日期从src_format改为dest_format 返回当前系统日期时间等