package com.mcf.cmims.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils extends org.apache.commons.lang.time.DateUtils { public static Date getToday(){ Date today = org.apache.commons.lang.time.DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH); return today; } /** * @return 从开始日期到结束日期所剩的时间 */ public static String getDeadline(Date startDate, Date endDate){ if (startDate.compareTo(endDate)>=0){ return "期限无效"; } String strDeadline = ""; Calendar startCld = Calendar.getInstance(); startCld.setTime(startDate); Calendar endCld = Calendar.getInstance(); endCld.setTime(endDate); int year = endCld.get(Calendar.YEAR)-startCld.get(Calendar.YEAR); int month = endCld.get(Calendar.MONTH)-startCld.get(Calendar.MONTH); int date = endCld.get(Calendar.DATE)-startCld.get(Calendar.DATE); int daysOfMonth = 30; int cyear = endCld.get(Calendar.YEAR); int cmonth = endCld.get(Calendar.MONTH); // System.out.println("cyear"+cyear+ "cmonth "+cmonth); switch (cmonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:daysOfMonth = 31;break; case 2: daysOfMonth = 28; /*判断是不是闰年*/ if(cyear%400==0||(cyear%4==0&&cyear%100!=0)){ daysOfMonth+=1; } break; case 4: case 6: case 9: case 11:daysOfMonth = 30;break; default: break; } // System.out.println("daysOfMonth"+daysOfMonth); if (date < 0){ date += daysOfMonth;// 30 : 31 : 29 month -=1; } if (month < 0){ month += 12; year -=1;// year = year - 1; } if (year != 0){ strDeadline = year+" 年"; } if (month != 0){ strDeadline = strDeadline + month + " 月"; } if (date != 0){ strDeadline = strDeadline + date + " 天"; } return strDeadline; } public static boolean isMonthLastDate(Date date){ Calendar c = Calendar.getInstance(); c.clear(); c.setTime(date); int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); int curDay = c.get(Calendar.DATE); return lastDay == curDay; } public static Date getMonthFirstDate(Date date){ Calendar c = Calendar.getInstance(); c.clear(); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, 1); return c.getTime(); } public static Date getMonthFirstDate(Integer year, Integer month){ if( year == null ) return null; if( month == null ) return null; Calendar c = Calendar.getInstance(); c.clear(); // 0-base c.set(year.intValue(), month.intValue()-1, 1); return c.getTime(); } public static Date getMonthLastDate(Date date){ Calendar c = Calendar.getInstance(); c.clear(); c.setTime(date); // 0-base c.set(Calendar.DAY_OF_MONTH, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.DAY_OF_MONTH, -1); return c.getTime(); } public static Date getMonthLastDate(Integer year, Integer month){ if( year == null ) return null; if( month == null ) return null; month = month+1; if( month > 12 ){ month = 1; year = year + 1; } Calendar c = Calendar.getInstance(); c.clear(); // 0-base c.set(year.intValue(), month.intValue()-1, 1); c.add(Calendar.DAY_OF_MONTH, -1); return c.getTime(); } public static Date addDays(Date date, int adds){ if( date == null ) return null; Calendar c = Calendar.getInstance(); c.clear(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, adds); return c.getTime(); } public static Date lessDays(Date date, int days){ if( date == null ) return null; Calendar c = Calendar.getInstance(); c.clear(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, -days); return c.getTime(); } }
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)