Java處理日期的兩個工具方法,太實用了!

在日常開發中我們經常要對日期進行處理,今天就分享兩個處理日期的工具方法,不多說了,上代碼

  1. public class DateUtils{

  2. /**

  3. * yy-MM-dd

  4. */

  5. public static final String yy_MM_dd = "yy-MM-dd";

  6. /**

  7. * yyyy-MM

  8. */

  9. public static final String yyyy_MM = "yyyy-MM";

  10. /**

  11. * 計算開始時間結束時間相差多少天的日期集合

  12. * @param dateStart 開始時間,如:2017-11-11

  13. * @param dateEnd 結束時間 如:2017-11-13

  14. * @param style 將字元串格式化,DateUtils.yyyy_MM_dd

    Advertisements

  15. * @return 輸出結果:[2017-11-11, 2017-11-12, 2017-11-13]

  16. */

  17. @SuppressWarnings({ "rawtypes", "unchecked" })

  18. public static ArrayList getDayList(String dateStart, String dateEnd,String style) {

  19. ArrayList list = new ArrayList();

  20. SimpleDateFormat myFormatter = new SimpleDateFormat(style);

  21. Date date1 = null;

  22. Date date2 = null;

    Advertisements

  23. try {

  24. date1 = myFormatter.parse(dateStart);

  25. date2 = myFormatter.parse(dateEnd);

  26. } catch (Exception ex1) {

  27. System.out.println("日期轉換格式應和傳入的日期格式一致" + ex1.toString());

  28. }

  29. long day = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000)+ 1;

  30. long date3 = date1.getTime();

  31. for (int i = 0; i < day; i++) {

  32. String dateStr = myFormatter.format((new Date(date3)));

  33. list.add(dateStr);

  34. date3 = date3 + 24 * 60 * 60 * 1000;

  35. }

  36. return list;

  37. }

  38. /**

  39. * 傳入年-月,算出中間隔了幾個月

  40. * @param dateStart 開始時間 年-月

  41. * @param dateEnd 結束時間 年-月

  42. * @param style

  43. * @return

  44. * @throws ParseException

  45. *

  46. * 例如傳入參數 "2017-11","2018-02","yyyy-MM",

  47. * 輸出的結果是2017-11,2017-12,2018-01,2018-02

  48. */

  49. @SuppressWarnings({ "rawtypes", "unchecked" })

  50. public static List getYearAndMonthList(String dateStart, String dateEnd, String style) {

  51. List list=new ArrayList();

  52. SimpleDateFormat sdf=new SimpleDateFormat(style);

  53. try {

  54. Calendar c1=Calendar.getInstance();

  55. Calendar c2=Calendar.getInstance();

  56. c1.setTime(sdf.parse(dateStart));

  57. c2.setTime(sdf.parse(dateEnd));

  58. int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);

  59. int monthDistance;

  60. //開始日期若小月結束日期

  61. if(year<0){

  62. year=-year;

  63. monthDistance = year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);

  64. }

  65. monthDistance = year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);

  66. int cyear;

  67. int cmonth;

  68. for(int i=0;i<monthDistance+1;i++){

  69. //獲取開始時間的年份

  70. cyear=c1.get(c1.YEAR);

  71. //這裡的月份要加1才是開始時間的月份

  72. c1.add(c1.MONTH, 1);

  73. //這裡第一次加完之後是開始時間的月份

  74. cmonth=c1.get(c1.MONTH);

  75. //0代表12月

  76. if(cmonth==0){

  77. cmonth=12;

  78. }

  79. String yearAndMonth;

  80. if(cmonth<10){

  81. yearAndMonth=cyear+""+"-"+"0"+cmonth+"";

  82. }else{

  83. yearAndMonth=cyear+""+"-"+cmonth+"";

  84. }

  85. list.add(yearAndMonth);

  86. }

  87. } catch (Exception e) {

  88. e.printStackTrace();

  89. }

  90. return list;

  91. }

  92. }

接下來測試下這兩個方法:

  1. public class text {

  2. public static void main(String[] args) {

  3. String beginTime="2018-01-10";

  4. String endTime="2018-01-16";

  5. List dayList = DateUtils.getDayList(beginTime,endTime,DateUtils.yyyy_MM_dd);

  6. System.out.println(dayList);

  7. }

輸出結果:

  1. public class text {

  2. public static void main(String[] args) {

  3. String beginTime="2017-08";

  4. String endTime="2018-01";

  5. List yearAndMonthList = DateUtils.getYearAndMonthList(beginTime,endTime,DateUtils.yyyy_MM);

  6. System.out.println(yearAndMonthList);

  7. }

  8. }

輸出結果:

相信大家看到這裡都知道怎麼用了,也希望大家有什麼好的建議也可以在評論區提提,你們的關注就是我每天更新的動力。好的,今天就分享這些。

Advertisements

你可能會喜歡