java 日期工具类(二)

2014-11-24 03:00:24 · 作者: · 浏览: 5
lse { days = String.valueOf(day); } String firstDay = "" + year + "-" + months + "-01"; String[] lastMonth = new String[2]; lastMonth[0] = firstDay; try { return sp.parse(firstDay); } catch (ParseException e) { } return null; } // 昨天日期 public String getYesterday() { Date today = new Date(); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, -1); return sp.format(ca.getTime()); } // 7天前日期 public String getSevenDayAgo() { Date today = new Date(); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, -7); return sp.format(ca.getTime()); } // 3天前日期 public String getThreeDayAgo() { Date today = new Date(); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, -3); return sp.format(ca.getTime()); } // 一月前日期 public String getOneMonthAgo() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); // 得到前一个月 long date = cal.getTimeInMillis(); Date kk = new Date(date); return sp.format(kk); } // 3月前日期 public String getThreeMonthAgo() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -3); long date = cal.getTimeInMillis(); Date kk = new Date(date); return sp.format(kk); } /** * getDate
* 格式:yyyy-MM-dd 00:00:00 eg.2013-08-19 00:00:00
* @param type -1表示昨天;-2表示前天;以此类推-N表示前N天 * @return java.util.Date */ public Date getDate(int type) { Date resultDate = null; Date today = new Date(); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, type); // 将时间转换成00:00:00 try { resul
tDate = sp.parse(sp.format(ca.getTime())); return resultDate; } catch (ParseException e) { e.printStackTrace(); } return resultDate; } /** * N天前的具体时间 * * @param n 0 表示今天,-1代表昨天 * @param type yyyyMMddHHmmss、yyyy-MM-dd HH:mm:ss * @return */ public String getSomeDaysAgoString(int n, String type) { Date today = new Date(); SimpleDateFormat sp = new SimpleDateFormat(); sp.applyPattern(type); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, n); return sp.format(ca.getTime()); } /** * N天前的具体时间 * * @param n 0 表示今天,-1代表昨天 * @return */ public Date getSomeDaysAgoDate(int n) { Date today = new Date(); Calendar ca = Calendar.getInstance(); ca.setTime(today); ca.add(Calendar.DAY_OF_YEAR, n); return ca.getTime(); } /** * 格式化某一时间 * * @param date * @param type * @return */ public String getString(Date date, String type) { SimpleDateFormat sp = new SimpleDateFormat(); sp.applyPattern(type); if (date != null && type != null) { return sp.format(date); } return "时间格式化异常!"; } /** * 格式化某一时间字符串 * * @param date * @param type * @return */ public Date getDate(String date, String type) { SimpleDateFormat sp = new SimpleDateFormat(); sp.applyPattern(type); try { if (date != null && type != null) { return sp.parse(date); } return null; } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 某日期前、后N天的日期 * @param date 某一日期 * @param type N天 0表示当天,N或+N表示后N天的日期,-N表示前N天的日期 * @return java.util.Date */ public Date getDate(Date date,i