import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 获取当前的日期格式为:yyyy-MM-dd HH:mm:ss * * @author Arthur(user_006) * @version 1.0.0 2010/04/24 14:00(星期六) */ public class TimerUtil { /** * 获取当前的日期 , 默认格式 * * @return 当前的日期 */ public synchronized static Date getCurrentCalendar() { Calendar calendar = Calendar.getInstance(); return calendar.getTime(); } /** * 获取当前日期 , 格式yyyy-MM-dd , 如: 2010-04-24 * * @return */ public synchronized static String getCurrentDate() { Calendar calendar = Calendar.getInstance(); StringBuffer sb = new StringBuffer(); sb.append(calendar.get(Calendar.YEAR)).append("-"); sb.append(calendar.get(Calendar.MONTH) + 1).append("-"); sb.append(calendar.get(Calendar.DAY_OF_MONTH)); return sb.toString(); } /** * 把字符形式的日期转换成Date类型 , 格式yyyy-MM-DD * * @param date * 日期的字符串形式 * * @return Data类型的日期 */ public synchronized static Date convertToDate(String date) { DateFormat format = new SimpleDateFormat("yyyy-MM-DD"); try { return format.parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 把日期转换为指定的格式 * * @param date * 要转换的日期 * @param pattern * 转换的格式 * @return */ public synchronized static Date format(Date date, String pattern) { if (pattern != null && pattern.trim().length() > 0) { DateFormat format = new SimpleDateFormat(pattern); String stringDate = format.format(date); return TimerUtil.convertToDate(stringDate); } return null; } /** * 将日期转换为字符串 , 格式yyyy-MM-dd HH:mm:ss * * @param date * 要转换的日期 * @return */ public synchronized static String convertToString(Date date) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); } /** * 将日期转换为字符串 , 格式yyyy-MM-dd HH:mm:ss * * @param date * 要转换的日期 * @return */ public synchronized static String convertToStrShort(Date date) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(date); } /** * 将日期转换为字符串 , 格式yyyyMMddHHmmss * * @param date * 要转换的日期 * @return */ public synchronized static String convertToString2(Date date) { DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); return format.format(date); } /** * 将日期转换为指定格式的字符串 * * @param date * 要转换的日期 * @param formatText * 转换的格式 * @return */ public synchronized static String convertToString(Date date, String formatText) { DateFormat format = new SimpleDateFormat(formatText); Calendar calendar = Calendar.getInstance(); StringBuffer sb = new StringBuffer(); sb.append(calendar.get(Calendar.YEAR)).append("-"); sb.append(calendar.get(Calendar.MONTH) + 1).append("-"); sb.append(calendar.get(Calendar.DAY_OF_MONTH)); return format.format(date); } /** * 获取本周一的日期 * * @return */ public synchronized static Date getMondayOFWeek() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 这里设置从周一開始,若须要依据系统时区自己主动获取,则採用下边的方式 return calendar.getTime(); } /** * 获取本周日日期 * * @return */ public synchronized static Date getCurrentWeekday() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, getCurrentPlus());// 把当前日期的DATE加上当前日期与本周日之间相差的天数 return calendar.getTime(); } /** * 获取上周日的日期 * * @return */ public synchronized static Date getPreviousWeekday() { Calendar calendar = Calendar.getInstance(); // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7); calendar.add(Calendar.DATE, getCurrentPlus() - 7);// 把当前日期的DATE加上当前日期与本周日之间相差的天数 return calendar.getTime(); } /** * 获取上周日的日期 * * @return */ public synchronized static Date getPreviousMonday() { Calendar calendar = Calendar.getInstance(); // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7); calendar.add(Calendar.DATE, getCurrentPlus() - 13);// 把当前日期的DATE加上当前日期与本周日之间相差的天数 return calendar.getTime(); } /** * 获取上周日的日期 * * @return */ public synchronized static Date getNextWeekday() { Calendar calendar = Calendar.getInstance(); // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7); calendar.add(Calendar.DATE, getCurrentPlus() + 1 + 6);// 把当前日期的DATE加上当前日期与本周日之间相差的天数 return calendar.getTime(); } /** * 获取上周日的日期 * * @return */ public synchronized static Date getNextMonday() { Calendar calendar = Calendar.getInstance(); // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7); calendar.add(Calendar.DATE, getCurrentPlus() + 1);// 把当前日期的DATE加上当前日期与本周日之间相差的天数 return calendar.getTime(); } /** * 获取当前日期与本周周日之间相差的天数 * * @return */ public synchronized static int getCurrentPlus() { Calendar calendar = Calendar.getInstance(); int days = calendar.get(Calendar.DAY_OF_WEEK) - 1;// 在中国是已星期一作为一周的第一天,所以这里减1 return 7 - days; } public synchronized static int getCurrentYear() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR); } /** * 返回指定日期的当月第一天 * * @param date * @return */ @SuppressWarnings("static-access") public synchronized static Date getFirstDayInMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(calendar.DATE, 1); return calendar.getTime(); } /** * 返回指定日期的当月最后一天 * * @param date * @return */ @SuppressWarnings("static-access") public synchronized static Date getLastDayInMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(calendar.MONTH, 1); calendar.set(calendar.DATE, 1); calendar.add(calendar.DATE, -1); return calendar.getTime(); } /* * 计算两个日期之间的天数 */ public static int getDaysBetween(Calendar d1, Calendar d2) { if (d1.after(d2)) { java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { d1 = (Calendar) d1.clone(); do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数 d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; } /* * 计算两个日期之间的时间 */ public static String getGapTime(Date createTime) { // TODO Auto-generated method stub Calendar d2 = Calendar.getInstance(); Calendar d1 = Calendar.getInstance(); d1.setTime(createTime); int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); if (days < 1) { int hour = d2.get(Calendar.HOUR_OF_DAY) - d1.get(Calendar.HOUR_OF_DAY); if (hour < 1) { int munites = d2.get(Calendar.MINUTE) - d1.get(Calendar.MINUTE); if (munites == 0) { return "刚刚"; } else { return munites + "分钟前"; } } else { return hour + "小时前"; } } else { return days + "天前"; } } //加天数 public static String addDays(int day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, day); StringBuffer sb = new StringBuffer(); sb.append(calendar.get(Calendar.YEAR)).append("-"); sb.append(calendar.get(Calendar.MONTH) + 1).append("-"); sb.append(calendar.get(Calendar.DAY_OF_MONTH)); return sb.toString(); } //加年份 public static String addYears(String now, int year) throws ParseException{ Calendar fromCal=Calendar.getInstance(); DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); Date date=dateFormat.parse(now); fromCal.setTime(date); fromCal.add(Calendar.YEAR, year); return dateFormat.format(fromCal.getTime()); } //加天数(特定时间) public static String addDate(String now, int day) throws ParseException{ Calendar fromCal=Calendar.getInstance(); DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); Date date=dateFormat.parse(now); fromCal.setTime(date); fromCal.add(Calendar.DATE, day); return dateFormat.format(fromCal.getTime()); } /** * 格多少时间 * @throws ParseException */ @SuppressWarnings("deprecation") public static void main(String[] args) throws ParseException { System.out.println(TimerUtil.addYears("2008-12-01",7)); } /*public static void main(String[] args) { String createDate = "2008-12-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); try { Date date = sdf.parse(createDate); Calendar cl = Calendar.getInstance(); cl.setTime(date); cl.add(Calendar.DATE,31); String temp = ""; temp = sdf.format(cl.getTime()); System.out.println(temp); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ }