声明
1)该文章整理自网上的大牛和专家无私奉献的资料,具体引用的资料请看参考文献。
2)本文仅供学术交流,非商用。如果某部分不小心侵犯了大家的利益,还望海涵,并联系博主删除。
3)博主才疏学浅,文中如有不当之处,请各位指出,共同进步,谢谢。
4)此属于第一版本,若有错误,还需继续修正与增删。还望大家多多指点。大家都共享一点点,一起为祖国科研的推进添砖加瓦。
Java 字符串型的日期对比大小
1 /** 2 * @description: 两个String类型,按照日期格式对比 3 */ 4 public static int compareTime(String dateOne, String dateTwo , String dateFormatType){ 5 6 DateFormat df = new SimpleDateFormat(dateFormatType); 7 Calendar calendarStart = Calendar.getInstance(); 8 Calendar calendarEnd = Calendar.getInstance(); 9 10 try { 11 calendarStart.setTime(df.parse(dateOne)); 12 calendarEnd.setTime(df.parse(dateTwo)); 13 } catch (ParseException e) { 14 e.printStackTrace(); 15 return 100; 16 } 17 18 int result = calendarStart.compareTo(calendarEnd); 19 if(result > 0){ 20 result = 1; 21 }else if(result < 0){ 22 result = -1; 23 }else{ 24 result = 0 ; 25 } 26 return result ; 27 }
java int型日期转为date或特定格式
1 Calendar c=Calendar.getInstance(); 2 int seconds = 1514871613;//数据库中提取的数据 3 long millions=new Long(seconds).longValue()*1000; 4 c.setTimeInMillis(millions); 5 System.out.println(""+c.getTime()); 6 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 7 String dateString = sdf.format(c.getTime()); 8 System.out.println(dateString); 9 10 11 根据以上例子实现int数据转DATE 12 13 1:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 14 //format套工具类方法进行赋值 15 String aInteger = sdf.format(DateFormatUtil.intFormatDate(hAlarm.getAttime())); 16 date.setAttime(aInteger);
java 日期转换
1 java 日期转换 2 package com.hoperun.self.supermarket.common; 3 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Calendar; 7 import java.util.Date; 8 import java.util.GregorianCalendar; 9 10 import java.sql.Timestamp; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 public class DateFormatUtil { 15 16 /** 17 * 获取当前的时间 yyyy-MM-dd HH:mm:ss 18 * @return 19 * @throws ParseException 20 */ 21 public static Date getNow(){ 22 Date date = new Date(); 23 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 24 String createdate = sdf.format(date); 25 Date date2 = null; 26 try { 27 date2 = sdf.parse(createdate); 28 } catch (ParseException e) { 29 e.printStackTrace(); 30 } 31 return date2; 32 } 33 34 /** 35 * 获取当前的时间 yyyy-MM-dd 36 * @return 37 */ 38 public static Date getNowDay(){ 39 Date date = new Date(); 40 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 41 String createdate = sdf.format(date); 42 Date date2 = null; 43 try { 44 date2 = sdf.parse(createdate); 45 } catch (ParseException e) { 46 e.printStackTrace(); 47 } 48 return date2; 49 } 50 51 /** 52 * 获取当前的时间 yyyy-MM-dd 53 * @return 54 */ 55 public static String getNowDayStr(){ 56 Date date = new Date(); 57 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 58 String createdate = sdf.format(date); 59 return createdate; 60 } 61 62 /** 63 * 获取当前时间 yyyyMMddHHmmss 64 * @return 65 */ 66 public static String getNowDayStr2(){ 67 Date date = new Date(); 68 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 69 String createdate = sdf.format(date); 70 return createdate; 71 } 72 73 /** 74 * 获取前一天的时间 yyyy-MM-dd 75 * @return 76 */ 77 public static String getLastDayStr() { 78 Calendar ca = Calendar.getInstance(); 79 ca.setTime(new Date()); 80 ca.add(Calendar.DATE, -1); 81 Date lastDay = ca.getTime(); //结果 82 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 83 String lastDayStr = sf.format(lastDay); 84 return lastDayStr; 85 } 86 87 /** 88 * 获取后一天的时间 yyyy-MM-dd 89 * @return 90 */ 91 public static String getAfterDayStr() { 92 Calendar ca = Calendar.getInstance(); 93 ca.setTime(new Date()); 94 ca.add(Calendar.DATE, 1); 95 Date lastDay = ca.getTime(); //结果 96 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 97 String lastDayStr = sf.format(lastDay); 98 return lastDayStr; 99 } 100 101 /** 102 * 字符串转为日期格式 103 * 104 * @param dateString 105 * @return 106 * @throws ParseException 107 */ 108 public static Date stringFormatDate(String dateString){ 109 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 110 Date date = null; 111 try { 112 date = bartDateFormat.parse(dateString); 113 } catch (ParseException e) { 114 e.printStackTrace(); 115 } 116 return date; 117 } 118 119 /** 120 * 字符串转为日期格式 121 * 122 * @param dateString 123 * @return 124 * @throws ParseException 125 */ 126 public static Date stringFormatDateTime(String dateString){ 127 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 128 Date date = null; 129 try { 130 date = bartDateFormat.parse(dateString); 131 } catch (ParseException e) { 132 e.printStackTrace(); 133 } 134 return date; 135 } 136 137 /** 138 * 字符串转为日期格式 139 * 140 * @param dateString 141 * @return 142 * @throws ParseException 143 */ 144 public static Date stringFormatDateTimeNoSecond(String dateString) throws ParseException { 145 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 146 Date date = bartDateFormat.parse(dateString); 147 return date; 148 } 149 150 /** 151 * 字符串转为日期格式 152 * 153 * @param dateString 154 * @return 155 * @throws ParseException 156 */ 157 public static Date stringFormatDateTime2(String dateString) throws ParseException { 158 SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 159 Date date = bartDateFormat.parse(dateString); 160 return date; 161 } 162 163 /** 164 * 将时间格式化为含时分秒的字符串 165 * 166 * @param date 167 * @return 168 * @throws ParseException 169 */ 170 public static String dateTimeFormatString(Date date){ 171 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 172 return dateFormat.format(date); 173 } 174 175 /** 176 * 将时间格式化为不含时分秒的字符串 177 * 178 * @param date 179 * @return 180 * @throws ParseException 181 */ 182 public static String dateFormatString(Date date) throws ParseException { 183 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 184 return dateFormat.format(date); 185 } 186 187 /** 188 * 将时间格式化为不含时分秒的字符串MM/dd 189 * 190 * @param date 191 * @return 192 * @throws ParseException 193 */ 194 public static String dateFormatString2(Date date) throws ParseException { 195 SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd"); 196 return dateFormat.format(date); 197 } 198 199 /** 200 * 将时间格式化为不含时分秒的字符串MM 201 * 202 * @param date 203 * @return 204 * @throws ParseException 205 */ 206 public static String dateFormatString3(Date date) throws ParseException { 207 SimpleDateFormat dateFormat = new SimpleDateFormat("M"); 208 return dateFormat.format(date); 209 } 210 211 /** 212 * 将int型日期转换为日期 213 * 214 * @param dateInt 215 * @return 216 */ 217 public static Date intFormatDate(int dateInt) { 218 Calendar c = Calendar.getInstance(); 219 long millions = new Long(dateInt).longValue() * 1000; 220 c.setTimeInMillis(millions); 221 return c.getTime(); 222 } 223 224 225 // 获取当天的开始时间 226 public static Date getDayBegin() { 227 Calendar cal = new GregorianCalendar(); 228 cal.set(Calendar.HOUR_OF_DAY, 0); 229 cal.set(Calendar.MINUTE, 0); 230 cal.set(Calendar.SECOND, 0); 231 cal.set(Calendar.MILLISECOND, 0); 232 return cal.getTime(); 233 } 234 235 // 获取当天的结束时间 236 public static Date getDayEnd() { 237 Calendar cal = new GregorianCalendar(); 238 cal.set(Calendar.HOUR_OF_DAY, 23); 239 cal.set(Calendar.MINUTE, 59); 240 cal.set(Calendar.SECOND, 59); 241 return cal.getTime(); 242 } 243 244 // 获取昨天的开始时间 245 public static Date getBeginDayOfYesterday() { 246 Calendar cal = new GregorianCalendar(); 247 cal.setTime(getDayBegin()); 248 cal.add(Calendar.DAY_OF_MONTH, -1); 249 return cal.getTime(); 250 } 251 252 // 获取昨天的结束时间 253 public static Date getEndDayOfYesterDay() { 254 Calendar cal = new GregorianCalendar(); 255 cal.setTime(getDayEnd()); 256 cal.add(Calendar.DAY_OF_MONTH, -1); 257 return cal.getTime(); 258 } 259 260 // 获取前天的开始时间 261 public static Date getBeginDayOfYesterday2() { 262 Calendar cal = new GregorianCalendar(); 263 cal.setTime(getDayBegin()); 264 cal.add(Calendar.DAY_OF_MONTH, -2); 265 return cal.getTime(); 266 } 267 268 // 获取前天的结束时间 269 public static Date getEndDayOfYesterDay2() { 270 Calendar cal = new GregorianCalendar(); 271 cal.setTime(getDayEnd()); 272 cal.add(Calendar.DAY_OF_MONTH, -2); 273 return cal.getTime(); 274 } 275 276 // 获取前3天的开始时间 277 public static Date getBeginDayOfYesterday3() { 278 Calendar cal = new GregorianCalendar(); 279 cal.setTime(getDayBegin()); 280 cal.add(Calendar.DAY_OF_MONTH, -3); 281 return cal.getTime(); 282 } 283 284 // 获取前3天的结束时间 285 public static Date getEndDayOfYesterDay3() { 286 Calendar cal = new GregorianCalendar(); 287 cal.setTime(getDayEnd()); 288 cal.add(Calendar.DAY_OF_MONTH, -3); 289 return cal.getTime(); 290 } 291 292 // 获取前4天的开始时间 293 public static Date getBeginDayOfYesterday4() { 294 Calendar cal = new GregorianCalendar(); 295 cal.setTime(getDayBegin()); 296 cal.add(Calendar.DAY_OF_MONTH, -4); 297 return cal.getTime(); 298 } 299 300 // 获取前4天的结束时间 301 public static Date getEndDayOfYesterDay4() { 302 Calendar cal = new GregorianCalendar(); 303 cal.setTime(getDayEnd()); 304 cal.add(Calendar.DAY_OF_MONTH, -4); 305 return cal.getTime(); 306 } 307 308 // 获取前5天的开始时间 309 public static Date getBeginDayOfYesterday5() { 310 Calendar cal = new GregorianCalendar(); 311 cal.setTime(getDayBegin()); 312 cal.add(Calendar.DAY_OF_MONTH, -5); 313 return cal.getTime(); 314 } 315 316 // 获取前5天的结束时间 317 public static Date getEndDayOfYesterDay5() { 318 Calendar cal = new GregorianCalendar(); 319 cal.setTime(getDayEnd()); 320 cal.add(Calendar.DAY_OF_MONTH, -5); 321 return cal.getTime(); 322 } 323 324 // 获取前6天的开始时间 325 public static Date getBeginDayOfYesterday6() { 326 Calendar cal = new GregorianCalendar(); 327 cal.setTime(getDayBegin()); 328 cal.add(Calendar.DAY_OF_MONTH, -6); 329 return cal.getTime(); 330 } 331 332 // 获取前6天的结束时间 333 public static Date getEndDayOfYesterDay6() { 334 Calendar cal = new GregorianCalendar(); 335 cal.setTime(getDayEnd()); 336 cal.add(Calendar.DAY_OF_MONTH, -6); 337 return cal.getTime(); 338 } 339 340 // 获取明天的开始时间 341 public static Date getBeginDayOfTomorrow() { 342 Calendar cal = new GregorianCalendar(); 343 cal.setTime(getDayBegin()); 344 cal.add(Calendar.DAY_OF_MONTH, 1); 345 346 return cal.getTime(); 347 } 348 349 // 获取明天的结束时间 350 public static Date getEndDayOfTomorrow() { 351 Calendar cal = new GregorianCalendar(); 352 cal.setTime(getDayEnd()); 353 cal.add(Calendar.DAY_OF_MONTH, 1); 354 return cal.getTime(); 355 } 356 357 // 获取本周的开始时间 358 @SuppressWarnings("unused") 359 public static Date getBeginDayOfWeek() { 360 Date date = new Date(); 361 if (date == null) { 362 return null; 363 } 364 Calendar cal = Calendar.getInstance(); 365 cal.setTime(date); 366 int dayofweek = cal.get(Calendar.DAY_OF_WEEK); 367 if (dayofweek == 1) { 368 dayofweek += 7; 369 } 370 cal.add(Calendar.DATE, 2 - dayofweek); 371 return getDayStartTime(cal.getTime()); 372 } 373 374 // 获取本周的结束时间 375 public static Date getEndDayOfWeek() { 376 Calendar cal = Calendar.getInstance(); 377 cal.setTime(getBeginDayOfWeek()); 378 cal.add(Calendar.DAY_OF_WEEK, 6); 379 Date weekEndSta = cal.getTime(); 380 return getDayEndTime(weekEndSta); 381 } 382 383 // 获取上周的开始时间 384 @SuppressWarnings("unused") 385 public static Date getBeginDayOfLastWeek() { 386 Date date = new Date(); 387 if (date == null) { 388 return null; 389 } 390 Calendar cal = Calendar.getInstance(); 391 cal.setTime(date); 392 int dayofweek = cal.get(Calendar.DAY_OF_WEEK); 393 if (dayofweek == 1) { 394 dayofweek += 7; 395 } 396 cal.add(Calendar.DATE, 2 - dayofweek - 7); 397 return getDayStartTime(cal.getTime()); 398 } 399 400 // 获取上周的结束时间 401 public static Date getEndDayOfLastWeek() { 402 Calendar cal = Calendar.getInstance(); 403 cal.setTime(getBeginDayOfLastWeek()); 404 cal.add(Calendar.DAY_OF_WEEK, 6); 405 Date weekEndSta = cal.getTime(); 406 return getDayEndTime(weekEndSta); 407 } 408 409 // 获取上2周的开始时间 410 @SuppressWarnings("unused") 411 public static Date getBeginDayOfLast2Week() { 412 Date date = new Date(); 413 if (date == null) { 414 return null; 415 } 416 Calendar cal = Calendar.getInstance(); 417 cal.setTime(date); 418 int dayofweek = cal.get(Calendar.DAY_OF_WEEK); 419 if (dayofweek == 1) { 420 dayofweek += 7; 421 } 422 cal.add(Calendar.DATE, 2 - dayofweek - 14); 423 return getDayStartTime(cal.getTime()); 424 } 425 426 // 获取上2周的结束时间 427 public static Date getEndDayOfLast2Week() { 428 Calendar cal = Calendar.getInstance(); 429 cal.setTime(getBeginDayOfLast2Week()); 430 cal.add(Calendar.DAY_OF_WEEK, 6); 431 Date weekEndSta = cal.getTime(); 432 return getDayEndTime(weekEndSta); 433 } 434 435 // 获取上3周的开始时间 436 @SuppressWarnings("unused") 437 public static Date getBeginDayOfLast3Week() { 438 Date date = new Date(); 439 if (date == null) { 440 return null; 441 } 442 Calendar cal = Calendar.getInstance(); 443 cal.setTime(date); 444 int dayofweek = cal.get(Calendar.DAY_OF_WEEK); 445 if (dayofweek == 1) { 446 dayofweek += 7; 447 } 448 cal.add(Calendar.DATE, 2 - dayofweek - 21); 449 return getDayStartTime(cal.getTime()); 450 } 451 452 // 获取上3周的结束时间 453 public static Date getEndDayOfLast3Week() { 454 Calendar cal = Calendar.getInstance(); 455 cal.setTime(getBeginDayOfLast3Week()); 456 cal.add(Calendar.DAY_OF_WEEK, 6); 457 Date weekEndSta = cal.getTime(); 458 return getDayEndTime(weekEndSta); 459 } 460 461 // 获取本月的开始时间 462 public static Date getBeginDayOfMonth() { 463 Calendar calendar = Calendar.getInstance(); 464 calendar.set(getNowYear(), getNowMonth() - 1, 1); 465 return getDayStartTime(calendar.getTime()); 466 } 467 468 // 获取本月的结束时间 469 public static Date getEndDayOfMonth() { 470 Calendar calendar = Calendar.getInstance(); 471 calendar.set(getNowYear(), getNowMonth() - 1, 1); 472 int day = calendar.getActualMaximum(5); 473 calendar.set(getNowYear(), getNowMonth() - 1, day); 474 return getDayEndTime(calendar.getTime()); 475 } 476 477 // 获取上月的开始时间 478 public static Date getBeginDayOfLastMonth() { 479 Calendar calendar = Calendar.getInstance(); 480 calendar.set(getNowYear(), getNowMonth() - 2, 1); 481 return getDayStartTime(calendar.getTime()); 482 } 483 484 // 获取上月的结束时间 485 public static Date getEndDayOfLastMonth() { 486 Calendar calendar = Calendar.getInstance(); 487 calendar.set(getNowYear(), getNowMonth() - 2, 1); 488 int day = calendar.getActualMaximum(5); 489 calendar.set(getNowYear(), getNowMonth() - 2, day); 490 return getDayEndTime(calendar.getTime()); 491 } 492 493 // 获取上2月的开始时间 494 public static Date getBeginDayOfLast2Month() { 495 Calendar calendar = Calendar.getInstance(); 496 calendar.set(getNowYear(), getNowMonth() - 3, 1); 497 return getDayStartTime(calendar.getTime()); 498 } 499 500 // 获取上2月的结束时间 501 public static Date getEndDayOfLast2Month() { 502 Calendar calendar = Calendar.getInstance(); 503 calendar.set(getNowYear(), getNowMonth() - 3, 1); 504 int day = calendar.getActualMaximum(5); 505 calendar.set(getNowYear(), getNowMonth() - 3, day); 506 return getDayEndTime(calendar.getTime()); 507 } 508 509 // 获取上月的开始时间 510 public static Date getBeginDayOfLast3Month() { 511 Calendar calendar = Calendar.getInstance(); 512 calendar.set(getNowYear(), getNowMonth() - 4, 1); 513 return getDayStartTime(calendar.getTime()); 514 } 515 516 // 获取上月的结束时间 517 public static Date getEndDayOfLast3Month() { 518 Calendar calendar = Calendar.getInstance(); 519 calendar.set(getNowYear(), getNowMonth() - 4, 1); 520 int day = calendar.getActualMaximum(5); 521 calendar.set(getNowYear(), getNowMonth() - 4, day); 522 return getDayEndTime(calendar.getTime()); 523 } 524 525 // 获取本年的开始时间 526 public static java.util.Date getBeginDayOfYear() { 527 Calendar cal = Calendar.getInstance(); 528 cal.set(Calendar.YEAR, getNowYear()); 529 // cal.set 530 cal.set(Calendar.MONTH, Calendar.JANUARY); 531 cal.set(Calendar.DATE, 1); 532 533 return getDayStartTime(cal.getTime()); 534 } 535 536 // 获取本年的结束时间 537 public static java.util.Date getEndDayOfYear() { 538 Calendar cal = Calendar.getInstance(); 539 cal.set(Calendar.YEAR, getNowYear()); 540 cal.set(Calendar.MONTH, Calendar.DECEMBER); 541 cal.set(Calendar.DATE, 31); 542 return getDayEndTime(cal.getTime()); 543 } 544 545 // 获取某个日期的开始时间 546 public static Timestamp getDayStartTime(Date d) { 547 Calendar calendar = Calendar.getInstance(); 548 if (null != d) 549 calendar.setTime(d); 550 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 551 0, 0); 552 calendar.set(Calendar.MILLISECOND, 0); 553 return new Timestamp(calendar.getTimeInMillis()); 554 } 555 556 // 获取某个日期的结束时间 557 public static Timestamp getDayEndTime(Date d) { 558 Calendar calendar = Calendar.getInstance(); 559 if (null != d) 560 calendar.setTime(d); 561 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 562 59, 59); 563 calendar.set(Calendar.MILLISECOND, 999); 564 return new Timestamp(calendar.getTimeInMillis()); 565 } 566 567 // 获取今年是哪一年 568 public static Integer getNowYear() { 569 Date date = new Date(); 570 GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 571 gc.setTime(date); 572 return Integer.valueOf(gc.get(1)); 573 } 574 575 // 获取本月是哪一月 576 public static int getNowMonth() { 577 Date date = new Date(); 578 GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 579 gc.setTime(date); 580 return gc.get(2) + 1; 581 } 582 583 // 两个日期相减得到的天数 584 public static int getDiffDays(Date beginDate, Date endDate) { 585 586 if (beginDate == null || endDate == null) { 587 throw new IllegalArgumentException("getDiffDays param is null!"); 588 } 589 590 long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24); 591 592 int days = new Long(diff).intValue(); 593 594 return days; 595 } 596 597 // 两个日期相减得到的毫秒数 598 public static long dateDiff(Date beginDate, Date endDate) { 599 long date1ms = beginDate.getTime(); 600 long date2ms = endDate.getTime(); 601 return date2ms - date1ms; 602 } 603 604 // 获取两个日期中的最大日期 605 public static Date max(Date beginDate, Date endDate) { 606 if (beginDate == null) { 607 return endDate; 608 } 609 if (endDate == null) { 610 return beginDate; 611 } 612 if (beginDate.after(endDate)) { 613 return beginDate; 614 } 615 return endDate; 616 } 617 618 // 获取两个日期中的最小日期 619 public static Date min(Date beginDate, Date endDate) { 620 if (beginDate == null) { 621 return endDate; 622 } 623 if (endDate == null) { 624 return beginDate; 625 } 626 if (beginDate.after(endDate)) { 627 return endDate; 628 } 629 return beginDate; 630 } 631 632 // 返回某月该季度的第一个月 633 public static Date getFirstSeasonDate(Date date) { 634 final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; 635 Calendar cal = Calendar.getInstance(); 636 cal.setTime(date); 637 int sean = SEASON[cal.get(Calendar.MONTH)]; 638 cal.set(Calendar.MONTH, sean * 3 - 3); 639 return cal.getTime(); 640 } 641 642 // 返回某个日期下几天的日期 643 public static Date getNextDay(Date date, int i) { 644 Calendar cal = new GregorianCalendar(); 645 cal.setTime(date); 646 cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); 647 return cal.getTime(); 648 } 649 650 // 返回某个日期前几天的日期 651 public static Date getFrontDay(Date date, int i) { 652 Calendar cal = new GregorianCalendar(); 653 cal.setTime(date); 654 cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); 655 return cal.getTime(); 656 } 657 658 // 返回昨天的日期 659 public static String getYesterDay() { 660 Date date = new Date();// 取时间 661 Calendar calendar = new GregorianCalendar(); 662 calendar.setTime(date); 663 calendar.add(Calendar.DATE, -1);// 把日期往后增加一天.整数往后推,负数往前移动 664 date = calendar.getTime(); // 这个时间就是日期往后推一天的结果 665 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 666 String dateString = formatter.format(date); 667 return dateString; 668 } 669 670 // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合) 671 @SuppressWarnings({ "rawtypes", "unchecked" }) 672 public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) { 673 List list = new ArrayList(); 674 if (beginYear == endYear) { 675 for (int j = beginMonth; j <= endMonth; j++) { 676 list.add(getTimeList(beginYear, j, k)); 677 678 } 679 } else { 680 { 681 for (int j = beginMonth; j < 12; j++) { 682 list.add(getTimeList(beginYear, j, k)); 683 } 684 685 for (int i = beginYear + 1; i < endYear; i++) { 686 for (int j = 0; j < 12; j++) { 687 list.add(getTimeList(i, j, k)); 688 } 689 } 690 for (int j = 0; j <= endMonth; j++) { 691 list.add(getTimeList(endYear, j, k)); 692 } 693 } 694 } 695 return list; 696 } 697 698 // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合) 699 @SuppressWarnings({ "unchecked", "rawtypes" }) 700 public static List getTimeList(int beginYear, int beginMonth, int k) { 701 List list = new ArrayList(); 702 Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); 703 int max = begincal.getActualMaximum(Calendar.DATE); 704 for (int i = 1; i < max; i = i + k) { 705 list.add(begincal.getTime()); 706 begincal.add(Calendar.DATE, k); 707 } 708 begincal = new GregorianCalendar(beginYear, beginMonth, max); 709 list.add(begincal.getTime()); 710 return list; 711 } 712 }