java时间间隔

た 入场券 2023-10-05 23:40 118阅读 0赞
  1. package com.yx.dwdb.manage.utils;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5. /**
  6. * @Auther: hsd
  7. * @Date: 2022/8/9 13:01
  8. * @Description:
  9. */
  10. public class DateBetweenUtils {
  11. /*季度*/
  12. public static List<String> getQuarterBetween(String startTime, String endTime) {
  13. List<String> monthList = new ArrayList<>();
  14. Set<String> months = new HashSet<String>();
  15. try {
  16. ArrayList<String> result = new ArrayList<String>();
  17. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
  18. Calendar min = Calendar.getInstance();
  19. Calendar max = Calendar.getInstance();
  20. min.setTime(sdf.parse(startTime));
  21. min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  22. max.setTime(sdf.parse(endTime));
  23. max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  24. Calendar curr = min;
  25. while (curr.before(max)) {
  26. result.add(sdf.format(curr.getTime()));
  27. curr.add(Calendar.MONTH, 1);
  28. }
  29. for (String string : result) {
  30. Date date = sdf.parse(string);
  31. DateFormat format1 = new SimpleDateFormat("MM");
  32. DateFormat format = new SimpleDateFormat("yyyy");
  33. String year = format.format(date);
  34. String month = format1.format(date);
  35. int parseInt = Integer.parseInt(month);
  36. if (parseInt >= 1 && parseInt <= 3) {
  37. month = "1";
  38. } else if (parseInt >= 4 && parseInt <= 6) {
  39. month = "2";
  40. } else if (parseInt >= 7 && parseInt <= 9) {
  41. month = "3";
  42. } else {
  43. month = "4";
  44. }
  45. months.add(year + "年" + month + "季度");
  46. }
  47. for (String mm: months ) {
  48. monthList.add(mm);
  49. }
  50. Collections.sort(monthList);
  51. return monthList;
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. return monthList;
  56. }
  57. /*月份*/
  58. public static List<String> getMonthBetween(String startTime, String endTime) {
  59. List<String> monthList = new ArrayList<>();
  60. try {
  61. ArrayList<String> result = new ArrayList<String>();
  62. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
  63. Calendar min = Calendar.getInstance();
  64. Calendar max = Calendar.getInstance();
  65. min.setTime(sdf.parse(startTime));
  66. min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  67. max.setTime(sdf.parse(endTime));
  68. max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  69. Calendar curr = min;
  70. while (curr.before(max)) {
  71. result.add(sdf.format(curr.getTime()));
  72. curr.add(Calendar.MONTH, 1);
  73. }
  74. for (String string : result) {
  75. Date date = sdf.parse(string);
  76. DateFormat format = new SimpleDateFormat("yyyy-MM");
  77. String month = format.format(date);
  78. monthList.add(month);
  79. }
  80. Collections.sort(monthList);
  81. return monthList;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. return monthList;
  86. }
  87. public static List<String> getYearBetween(String startTime, String endTime) {
  88. List<String> yearList = new ArrayList<>();
  89. try {
  90. ArrayList<String> result = new ArrayList<String>();
  91. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
  92. Calendar min = Calendar.getInstance();
  93. Calendar max = Calendar.getInstance();
  94. min.setTime(sdf.parse(startTime));
  95. min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
  96. max.setTime(sdf.parse(endTime));
  97. max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
  98. Calendar curr = min;
  99. while (curr.before(max)) {
  100. result.add(sdf.format(curr.getTime()));
  101. curr.add(Calendar.MONTH, 1);
  102. }
  103. Set<String> years = new HashSet<String>();
  104. for (String string : result) {
  105. Date date = sdf.parse(string);
  106. DateFormat format = new SimpleDateFormat("yyyy");
  107. String yearS = format.format(date);
  108. years.add(yearS);
  109. }
  110. for (String yy: years ) {
  111. yearList.add(yy);
  112. }
  113. Collections.sort(yearList);
  114. return yearList;
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return yearList;
  119. }
  120. }

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读

    相关 时间间隔(多实例测试)

    题目描述 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。要求程序定义如下两个函数,并在main()中调用这两个函数实现

    相关 考核的时间间隔思考

    绩效考核是个鸡肋,只是,许多公司不同程度存在着。反倒是有的公司,以没有绩效考核作为吸引高级人才的重要砝码。真是有趣的现象。 足见,绩效考核对于一些搬砖的工种来说,可能是有用的

    相关 java时间间隔

    Problem Description 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。 如:3点5分25秒应表示