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(); } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦