package com.controller.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { public static Date parseStr(String formatDate,String date) throws ParseException{ if(StringUtil.isNull(date))return null; return new SimpleDateFormat(formatDate).parse(date); } public static String formatDate(String formatDate,Date date) throws ParseException{ if(StringUtil.isNull(date))return ""; return new SimpleDateFormat(formatDate).format(date); } public static String parseStr(String formatStr,String formatDate,String date) throws ParseException{ return new SimpleDateFormat(formatDate).format(new SimpleDateFormat(formatStr).parse(date)); } public static Long diffDate(Date d1,Date d2){ return (d2.getTime()-d1.getTime())/1000; } public static int getMonthDay(int year, int month) { int result = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: result = 31; break; case 4: case 6: case 9: case 11: result = 30; break; case 2: result = 28; if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { result = 29; } break; } return result; } public static int getMonthDays(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); return cal.getActualMaximum(Calendar.DATE); } //获取年份 public static int getyear(Date year){ Calendar cld = Calendar.getInstance(); Date date = year; cld.setTime(date); cld.get(Calendar.YEAR); return cld.get(Calendar.YEAR); } public static String defFormatDate(Date date) throws ParseException{ return new SimpleDateFormat("yyyy-MM-dd").format(date); } /** * 获得两个日期的相差月数 * */ public static int getMonth(Date start, Date end) { if(null==start || null==end){ return 0 ; } if (start.after(end)) { Date t = start; start = end; end = t; } Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(start); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(end); Calendar temp = Calendar.getInstance(); temp.setTime(end); temp.add(Calendar.DATE, 1); int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH); if ((startCalendar.get(Calendar.DATE) == 1)&& (temp.get(Calendar.DATE) == 1)) { return year * 12 + month + 1; } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) { return year * 12 + month; } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) { return year * 12 + month; } else { return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month); } } public static Date firstDay( Date date) throws ParseException{ if(StringUtil.isNull(date))return null; Calendar calaf = Calendar.getInstance(); //设置当月为最后一天 calaf.setTime(date); calaf.set(Calendar.DAY_OF_MONTH , 1); date = calaf.getTime(); return date; } ////获取当前月最后一天 public static Date lastDay( Date date) throws ParseException{ if(StringUtil.isNull(date))return null; Calendar calast = Calendar.getInstance(); calast.setTime(date); calast.set(Calendar.DAY_OF_MONTH, calast.getActualMaximum(Calendar.DAY_OF_MONTH)); date = calast.getTime(); System.out.println("当月最后一天:"+date); return date; } }