1./** 2. * 计算前一天 、后一天 3. * 4. * @param date 当前输入的日期 格式为 2010-01-21 5. * @param amount 6. * @return 7. */ 8. public static String getYesterday(String date, int amount) { 9. String newDate = Constant.EMPTY_STRING; 10. if (date != null) { 11. DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 12. Date dd; 13. try { 14. dd = format.parse(date); 15. Calendar calendar = Calendar.getInstance(); 16. calendar.setTime(dd); 17. calendar.add(Calendar.DAY_OF_MONTH, amount); 18. newDate = format.format(calendar.getTime()); 19. 20. } catch (ParseException e) { 21. e.printStackTrace(); 22. } 23. 24. } 25. return newDate; 26. } 27. 28. 29. 30./** 31. * 计算两天之间相差的天数 调用此方法必须保证 时间格式与 timeFormatStr指定格式一致 否则异常 32. * 33. * @param beginDate 开始时间 34. * @param endDate 结束时间 35. * @param timeFormatStr 输入时间格式 36. * @return int 时间相差天数 37. */ 38. public static int getCountDays(String beginDate, String endDate, String timeFormatStr) { 39. int days = 0; 40. 41. if (beginDate != null && endDate != null && timeFormatStr != null) { 42. SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormatStr); 43. Calendar beginCalendar = Calendar.getInstance(); 44. Calendar endCalendar = Calendar.getInstance(); 45. 46. try { 47. endCalendar.setTime(dateFormat.parse(endDate)); 48. beginCalendar.setTime(dateFormat.parse(beginDate)); 49. while (beginCalendar.before(endCalendar)) { 50. days++; 51. beginCalendar.add(Calendar.DAY_OF_YEAR, 1); 52. } 53. } catch (ParseException e) { 54. // 日期格式异常 55. logger.error("getCountDays failed the" + beginDate + "or" + endDate + "is not format" 56. + timeFormatStr, e); 57. } 58. } 59. return days; 60. } 61. 62. 63./** 取近期一年的数据*/ 64. public static List<String> getYears() { 65. List<String> result = new ArrayList<String>(); 66. Calendar c = Calendar.getInstance(); 67. c.set(Calendar.YEAR, Integer.parseInt(DateUtil.getSysDate("yyyy"))); 68. c.set(Calendar.MONTH, Integer.parseInt(DateUtil.getSysDate("MM"))); 69. 70. result.add(DateUtil.getSysDate("yyyyMM")); 71. 72. for (int i = 0; i < 11; i++) { 73. c.add(Calendar.MONTH, -1); 74. int y = c.get(Calendar.YEAR); 75. int m = c.get(Calendar.MONTH); 76. 77. if (m == 0) { 78. result.add((y - 1) + "12"); 79. } else { 80. if (m < 10) 81. result.add(y + "0" + m); 82. else 83. result.add(y + (m + "")); 84. } 85. } 86. return result; 87. } 88. 89. /** 取上个月 */ 90. public static String getPrevMonth(String yearMonth) { 91. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); 92. Calendar calendar = Calendar.getInstance(); 93. Date curDate = java.sql.Date.valueOf(yearMonth + "-10"); 94. calendar.setTime(curDate); 95. // 取得现在时间 96. // System.out.println(sdf.format(curDate)); 97. 98. // 取得上一个时间 99. calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1); 100. 101. // 取得上一个月的下一天 102. calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1); 103. return sdf.format(calendar.getTime()); 104. } 105. 106. /** 取当月下的最后第一天 */ 107. public static void getLastDay(String yearMonth) { 108. Calendar cal = Calendar.getInstance(); 109. cal.setTime(java.sql.Date.valueOf(yearMonth + "-01")); 110. 111. cal.roll(Calendar.DAY_OF_MONTH, -1); 112. System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); 113. } 114. 115. /** 取当月下的第一天 */ 116. public static void getFirstDay(String yearMonth) { 117. Calendar cal = Calendar.getInstance(); 118. cal.setTime(java.sql.Date.valueOf(yearMonth + "-01")); 119. cal.set(Calendar.DAY_OF_MONTH, 1); 120. System.out.println(new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(cal.getTime())); 121. }