Java 日期工具类(日期,月份加减等)--转

时间:2023-03-08 20:51:33
  1. package util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /***
  7. * 日期工具类
  8. *
  9. * @author damao
  10. *
  11. */
  12. public class DateAndTimeUtil {
  13. /***
  14. * 日期月份减一个月
  15. *
  16. * @param datetime
  17. *            日期(2014-11)
  18. * @return 2014-10
  19. */
  20. public static String dateFormat(String datetime) {
  21. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  22. Date date = null;
  23. try {
  24. date = sdf.parse(datetime);
  25. } catch (ParseException e) {
  26. e.printStackTrace();
  27. }
  28. Calendar cl = Calendar.getInstance();
  29. cl.setTime(date);
  30. cl.add(Calendar.MONTH, -1);
  31. date = cl.getTime();
  32. return sdf.format(date);
  33. }
  34. public static String dateFormat(Date date) {
  35. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  36. return sdf.format(date);
  37. }
  38. /****
  39. * 传入具体日期 ,返回具体日期减一个月。
  40. *
  41. * @param date
  42. *            日期(2014-04-20)
  43. * @return 2014-03-20
  44. * @throws ParseException
  45. */
  46. public static String subMonth(String date) throws ParseException {
  47. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  48. Date dt = sdf.parse(date);
  49. Calendar rightNow = Calendar.getInstance();
  50. rightNow.setTime(dt);
  51. rightNow.add(Calendar.MONTH, -1);
  52. Date dt1 = rightNow.getTime();
  53. String reStr = sdf.format(dt1);
  54. return reStr;
  55. }
  56. /****
  57. * 获取月末最后一天
  58. *
  59. * @param sDate
  60. *            2014-11-24
  61. * @return 30
  62. */
  63. private static String getMonthMaxDay(String sDate) {
  64. SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
  65. Calendar cal = Calendar.getInstance();
  66. Date date = null;
  67. try {
  68. date = sdf_full.parse(sDate + "-01");
  69. } catch (ParseException e) {
  70. e.printStackTrace();
  71. }
  72. cal.setTime(date);
  73. int last = cal.getActualMaximum(Calendar.DATE);
  74. return String.valueOf(last);
  75. }
  76. // 判断是否是月末
  77. public static boolean isMonthEnd(Date date) {
  78. Calendar cal = Calendar.getInstance();
  79. cal.setTime(date);
  80. if (cal.get(Calendar.DATE) == cal
  81. .getActualMaximum(Calendar.DAY_OF_MONTH))
  82. return true;
  83. else
  84. return false;
  85. }
  86. /***
  87. * 日期减一天、加一天
  88. *
  89. * @param option
  90. *            传入类型 pro:日期减一天,next:日期加一天
  91. * @param _date
  92. *            2014-11-24
  93. * @return 减一天:2014-11-23或(加一天:2014-11-25)
  94. */
  95. public static String checkOption(String option, String _date) {
  96. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  97. Calendar cl = Calendar.getInstance();
  98. Date date = null;
  99. try {
  100. date = (Date) sdf.parse(_date);
  101. } catch (ParseException e) {
  102. e.printStackTrace();
  103. }
  104. cl.setTime(date);
  105. if ("pre".equals(option)) {
  106. // 时间减一天
  107. cl.add(Calendar.DAY_OF_MONTH, -1);
  108. } else if ("next".equals(option)) {
  109. // 时间加一天
  110. cl.add(Calendar.DAY_OF_YEAR, 1);
  111. } else {
  112. // do nothing
  113. }
  114. date = cl.getTime();
  115. return sdf.format(date);
  116. }
  117. /***
  118. * 判断日期是否为当前月, 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日
  119. * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日
  120. *
  121. * @param date
  122. *            日期 例如:2014-11
  123. * @return String[] 开始日期,结束日期,上月开始日期,上月结束日期
  124. * @throws ParseException
  125. */
  126. public static String[] getNow_Pre_Date(String date) throws ParseException {
  127. String[] str_date = new String[4];
  128. Date now = new Date();
  129. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  130. SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
  131. String stMonth = sdf.format(now);
  132. String stdate = "";// 开始日期
  133. String endate = "";// 结束日期
  134. String preDate_start = "";// 上月开始日期
  135. String preDate_end = "";// 上月结束日期
  136. // 当前月
  137. if (date.equals(stMonth)) {
  138. stdate = stMonth + "-01"; // 2014-11-01
  139. endate = sdf_full.format(now);// 2014-11-24
  140. preDate_start = subMonth(stdate);// 2014-10-01
  141. preDate_end = subMonth(endate);// 2014-10-24
  142. } else {
  143. // 非当前月
  144. String monthMaxDay = getMonthMaxDay(date);
  145. stdate = date + "-01";// 2014-10-01
  146. endate = date + "-" + monthMaxDay;// 2014-10-31
  147. preDate_start = subMonth(stdate);// 2014-09-01
  148. preDate_end = subMonth(endate);// 2014-09-30
  149. }
  150. str_date[0] = stdate;
  151. str_date[1] = endate;
  152. str_date[2] = preDate_start;
  153. str_date[3] = preDate_end;
  154. return str_date;
  155. }
  156. public static void main(String[] args) throws ParseException {
  157. /*
  158. * String a =DateAndTimeUtil.dateFormat(new Date());
  159. * System.out.println(a); String b =
  160. * DateAndTimeUtil.subMonth("2014-03-31"); System.out.println(b);
  161. * SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date
  162. * dt=sdf.parse("2014-03-31");
  163. * System.out.println(DateAndTimeUtil.isMonthEnd(dt));
  164. */
  165. String str = null;
  166. // str = DateAndTimeUtil.checkOption("next", "2014-11-30");
  167. // str = getMonthMaxDay("2014-11-24");
  168. // str = dateFormat("2014-11");
  169. str = getNow_Pre_Date("2014-10")[0];
  170. System.out.println(str);
  171. }
  172. }