date日期相减 java_Java对日期Date类进行日期加减运算,年份加减,月份加减

1 packagecom.cy;

59 importjava.security.InvalidParameterException;60 importjava.text.ParseException;61 importjava.text.SimpleDateFormat;62 importjava.util.ArrayList;63 importjava.util.Calendar;64 importjava.util.Date;65 importjava.util.HashMap;66 importjava.util.Map;67

68 public classDateUtils {69 private static final long MILLIS_IN_A_SECOND = 1000;70

71 private static final long SECONDS_IN_A_MINUTE = 60;72

73 private static final int MONTHS_IN_A_YEAR = 12;74

75 /**

76 * 获得指定日期之后一段时期的日期。例如某日期之后3天的日期等 时间为23:59:59。77 *78 *@paramorigDate79 * 基准日期80 *@paramamount81 * 时间数量82 *@paramtimeUnit83 * 时间单位,如年、月、日等。用Calendar中的常量代表84 *@return{@linkDate}85 */

86 public static final Date dateAfter(Date origDate, int amount, inttimeUnit) {87 Calendar calendar =Calendar.getInstance();88 calendar.setTime(origDate);89 //将小时至090 //calendar.set(Calendar.HOUR_OF_DAY, 23);91 //将分钟至092 //calendar.set(Calendar.MINUTE, 59);93 //将秒至094 //calendar.set(Calendar.SECOND, 59);95 //将毫秒至096 //calendar.set(Calendar.MILLISECOND, 0);

97 calendar.add(timeUnit, amount);98 returncalendar.getTime();99 }100

101 /**

102 * 获得指定日期之后一段时期的日期。例如某日期之后3天的日期等。103 *104 *@paramorigDate105 * 基准日期106 *@paramamount107 * 时间数量108 *@paramtimeUnit109 * 时间单位,如年、月、日等。用Calendar中的常量代表110 *@return{@linkDate}111 */

112 public static final Date timeAfter(Date origDate, int amount, inttimeUnit) {113 Calendar calendar =Calendar.getInstance();114 calendar.setTime(origDate);115 calendar.add(timeUnit, amount);116 returncalendar.getTime();117 }118

119 /**

120 * 获得指定日期之前一段时期的日期。例如某日期之前3天的日期等。121 *122 *@paramorigDate123 * 基准日期124 *@paramamount125 * 时间数量126 *@paramtimeUnit127 * 时间单位,如年、月、日等。用Calendar中的常量代表128 *@return{@linkDate}129 */

130 public static final Date dateBefore(Date origDate, int amount, inttimeUnit) {131 Calendar calendar =Calendar.getInstance();132 calendar.add(timeUnit, -amount);133 returncalendar.getTime();134 }135

136 /**

137 * 根据年月日构建日期对象。注意月份是从1开始计数的,即month为1代表1月份。138 *139 *@paramyear140 * 年141 *@parammonth142 * 月。注意1代表1月份,依此类推。143 *@paramday144 * 日145 *@return{@linkDate}146 */

147 public static Date date(int year, int month, intdate) {148 Calendar calendar =Calendar.getInstance();149 calendar.set(year, month - 1, date, 0, 0, 0);150 calendar.set(Calendar.MILLISECOND, 0);151 returncalendar.getTime();152 }153

154 /**

155 * 计算两个日期(不包括时间)之间相差的周年数156 *157 *@paramdate1158 * 开始时间159 *@paramdate2160 * 结束时间161 *@return{@linkInteger}162 */

163 public static intgetYearDiff(Date date1, Date date2) {164 if (date1 == null || date2 == null) {165 throw new InvalidParameterException("date1 and date2 cannot be null!");166 }167 if(date1.after(date2)) {168 throw new InvalidParameterException("date1 cannot be after date2!");169 }170

171 Calendar calendar =Calendar.getInstance();172 calendar.setTime(date1);173 int year1 =calendar.get(Calendar.YEAR);174 int month1 =calendar.get(Calendar.MONTH);175 int day1 =calendar.get(Calendar.DATE);176

177 calendar.setTime(date2);178 int year2 =calendar.get(Calendar.YEAR);179 int month2 =calendar.get(Calendar.MONTH);180 int day2 =calendar.get(Calendar.DATE);181

182 int result = year2 -year1;183 if (month2

191 /**

192 * 计算两个日期(不包括时间)之间相差的整月数193 *194 *@paramdate1195 * 开始时间196 *@paramdate2197 * 结束时间198 *@return{@linkInteger}199 */

200 public static intgetMonthDiff(Date date1, Date date2) {201 if (date1 == null || date2 == null) {202 throw new InvalidParameterException("date1 and date2 cannot be null!");203 }204 if(date1.after(date2)) {205 throw new InvalidParameterException("date1 cannot be after date2!");206 }207

208 Calendar calendar =Calendar.getInstance();209 calendar.setTime(date1);210 int year1 =calendar.get(Calendar.YEAR);211 int month1 =calendar.get(Calendar.MONTH);212 int day1 =calendar.get(Calendar.DATE);213

214 calendar.setTime(date2);215 int year2 =calendar.get(Calendar.YEAR);216 int month2 =calendar.get(Calendar.MONTH);217 int day2 =calendar.get(Calendar.DATE);218

219 int months = 0;220 if (day2 >=day1) {221 months = month2 -month1;222 } else{223 months = month2 - month1 - 1;224 }225 return (year2 - year1) * MONTHS_IN_A_YEAR +months;226 }227

228 /**

229 * 统计两个日期之间包含的天数。包含date1,但不包含date2230 *231 *@paramdate1232 * 开始时间233 *@paramdate2234 * 结束时间235 *@return{@linkInteger}236 *@throwsParseException237 */

238 public static intgetDayDiff(Date date1, Date date2) {239 if (date1 == null || date2 == null) {240 throw new InvalidParameterException("date1 and date2 cannot be null!");241 }242 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");243 Date smdate;244 try{245 smdate =sdf.parse(sdf.format(date1));246 Date bdate =sdf.parse(sdf.format(date2));247 Calendar cal =Calendar.getInstance();248 cal.setTime(smdate);249 long time1 =cal.getTimeInMillis();250 cal.setTime(bdate);251 long time2 =cal.getTimeInMillis();252 long between_days = (time2 - time1) / (1000 * 3600 * 24);253 returnInteger.parseInt(String.valueOf(between_days));254 } catch(ParseException e) {255 e.printStackTrace();256 }257 return 0;258 }259

260 /**

261 * 计算time2比time1晚多少分钟,忽略日期部分262 *263 *@paramtime1264 *@paramtime2265 *@return

266 */

267 public static intgetMinuteDiffByTime(Date time1, Date time2) {268 long startMil = 0;269 long endMil = 0;270 Calendar calendar =Calendar.getInstance();271 calendar.setTime(time1);272 calendar.set(1900, 1, 1);273 startMil =calendar.getTimeInMillis();274 calendar.setTime(time2);275 calendar.set(1900, 1, 1);276 endMil =calendar.getTimeInMillis();277 return (int) ((endMil - startMil) / MILLIS_IN_A_SECOND /SECONDS_IN_A_MINUTE);278 }279

280 /**

281 * 计算时间是否是同一天282 *283 *@paramdateA284 *@paramdateB285 *@return

286 */

287 public static booleanareSameDay(Date dateA, Date dateB) {288 Calendar calDateA =Calendar.getInstance();289 calDateA.setTime(dateA);290

291 Calendar calDateB =Calendar.getInstance();292 calDateB.setTime(dateB);293

294 return calDateA.get(Calendar.YEAR) ==calDateB.get(Calendar.YEAR)295 && calDateA.get(Calendar.MONTH) ==calDateB.get(Calendar.MONTH)296 && calDateA.get(Calendar.DAY_OF_MONTH) ==calDateB.get(Calendar.DAY_OF_MONTH);297 }298

299 /**

300 * @Title: getCurrYearLast301 * @Description: 获取某年最后一天302 *@paramdate303 *@returnDate304 */

305 public staticDate getCurrYearLast(Date date) {306 Calendar currCal =Calendar.getInstance();307 currCal.setTime(date);308 int currentYear =currCal.get(Calendar.YEAR);309 returngetYearLast(currentYear);310 }311

312 private static Date getYearLast(intyear) {313 Calendar calendar =Calendar.getInstance();314 calendar.clear();315 calendar.set(Calendar.YEAR, year);316

317 calendar.roll(Calendar.DAY_OF_YEAR, -1);318 //将小时至0

319 calendar.set(Calendar.HOUR_OF_DAY, 23);320 //将分钟至0

321 calendar.set(Calendar.MINUTE, 59);322 //将秒至0

323 calendar.set(Calendar.SECOND, 59);324 //将毫秒至0

325 calendar.set(Calendar.MILLISECOND, 0);326

327 Date currYearLast =calendar.getTime();328 returncurrYearLast;329 }330 /*

331 * private static Date getYearLast(int year) { Calendar calendar =332 * Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR,333 * year); calendar.roll(Calendar.DAY_OF_YEAR, -1); Date currYearLast =334 * calendar.getTime(); return currYearLast; }335 */

336 /**

337 *338 *@paramargs339 *@throwsParseException340 *341 * 根据结算周期数 对开始时间与结束时间进行分段342 */

343 public static void main(String[] args) throwsParseException {344 /*开始时间*/

345 Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-1-20 10:31:36");346 /*结束时间*/

347 Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-5-12 10:31:36");348 /*周期数*/

349 int solt = 2;350 ArrayList> list = new ArrayList<>();351

352 while (startDate.getTime() map = new HashMap<>();354 map.put("开始时间",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate));355 startDate=dateAfter(startDate, solt, Calendar.MONTH);356 if (startDate.getTime()>endDate.getTime()) {357 map.put("结束时间",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endDate));358

359 } else{360 map.put("结束时间",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startDate));361 }362 list.add(map);363 }364 System.out.println(list);365 }366 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值