1. /**
    2. * 获取日期
    3. * 格式: 2020-10-12
    4. */
    5. private static String getDateToday(int period){
    6. Calendar calendar = Calendar.getInstance();
    7. calendar.add(Calendar.DATE,period);
    8. return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    9. }
    10. /**
    11. * 获取周的第一天
    12. * @param period 期数
    13. * @return String
    14. * **/
    15. private static String getWeekStart(int period){
    16. Calendar cal=Calendar.getInstance();
    17. cal.add(Calendar.WEEK_OF_MONTH, period);
    18. cal.set(Calendar.DAY_OF_WEEK, 2);
    19. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    20. }
    21. /**
    22. * 获取周的最后一天
    23. * 期次
    24. * @return String
    25. * **/
    26. private static String getWeekEnd(int period){
    27. Calendar cal=Calendar.getInstance();
    28. cal.add(Calendar.WEEK_OF_MONTH, period);
    29. cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
    30. cal.add(Calendar.DAY_OF_WEEK, 1);
    31. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    32. }
    33. /**
    34. * 获取本月开始日期
    35. * @return String
    36. * **/
    37. public static String getMonthStart(int period){
    38. Calendar cal=Calendar.getInstance();
    39. cal.add(Calendar.MONTH, period);
    40. cal.set(Calendar.DAY_OF_MONTH, 1);
    41. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    42. }
    43. /**
    44. * 获取本月最后一天
    45. * @return String
    46. * **/
    47. public static String getMonthEnd(int period){
    48. Calendar cal=Calendar.getInstance();
    49. cal.add(Calendar.MONTH, period);
    50. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    51. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    52. }
    53. /**
    54. * 获取季度开始日期
    55. * @return
    56. */
    57. private static String getSeasonStart(int period) {
    58. Calendar calendar = Calendar.getInstance();
    59. int month = calendar.get(Calendar.MONTH) + 1+(period*3);
    60. //根据月份获取所在季度
    61. int quarter = new Double(Math.ceil(month / 3.0D)).intValue();
    62. //所在季度的第一个月
    63. int startMonth = quarter * 3 - 2;
    64. Calendar a = Calendar.getInstance();
    65. a.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
    66. a.set(Calendar.MONTH, startMonth - 1);
    67. a.set(Calendar.DATE, 1);
    68. return new SimpleDateFormat("yyyy-MM-dd").format(a.getTime());
    69. }
    70. /**
    71. * 获取季度开始日期
    72. * @return
    73. */
    74. private static String getSeasonEnd(int period) {
    75. Calendar calendar = Calendar.getInstance();
    76. int month = calendar.get(Calendar.MONTH) + 1+(period*3);
    77. //根据月份获取所在季度
    78. int quarter = new Double(Math.ceil(month / 3.0D)).intValue();
    79. //所在季度的第一个月
    80. int startMonth = quarter * 3;
    81. Calendar a = Calendar.getInstance();
    82. a.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
    83. a.set(Calendar.MONTH, startMonth - 1);
    84. a.set(Calendar.DATE, 1);
    85. a.set(Calendar.DAY_OF_MONTH, a.getActualMaximum(Calendar.DAY_OF_MONTH));
    86. return new SimpleDateFormat("yyyy-MM-dd").format(a.getTime());
    87. }
    88. /**
    89. * 获取年的第一天
    90. * @return String
    91. * **/
    92. public static String getYearStart(int period){
    93. Calendar cal = Calendar.getInstance();
    94. cal.add(Calendar.YEAR, period);
    95. return new SimpleDateFormat("yyyy").format(cal.getTime())+"-01-01";
    96. }
    97. /**
    98. * 获取本年的最后一天
    99. * @return String
    100. * **/
    101. public static String getYearEnd(int period){
    102. Calendar calendar = Calendar.getInstance();
    103. calendar.add(Calendar.YEAR, period);
    104. calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
    105. calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    106. return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    107. }
    108. /**
    109. * 获取后半年的开始时间
    110. */
    111. private static String getHalfYearStart(){
    112. Calendar c = Calendar.getInstance();
    113. c.set(Calendar.MONTH, 6);
    114. c.set(Calendar.DATE, 1);
    115. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    116. }
    117. /**
    118. * 获取前半年的结束时间
    119. */
    120. private static String getHalfYearEnd(){
    121. Calendar c = Calendar.getInstance();
    122. c.set(Calendar.MONTH, 5);
    123. c.set(Calendar.DATE, 30);
    124. return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    125. }