CommonUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.common.utils;
  18. import org.springblade.core.tool.utils.DateUtil;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. /**
  23. * 通用工具类
  24. *
  25. * @author Chill
  26. */
  27. public class CommonUtil {
  28. public static final String pattern = "yyyy-MM-dd";
  29. /**
  30. * 获取传入时间的第二天
  31. * @param today
  32. * @return
  33. */
  34. public static String getNextDay(String today){
  35. Calendar cd = Calendar.getInstance();
  36. cd.setTime(DateUtil.parse(today, pattern));
  37. cd.add(Calendar.DATE, 1);
  38. String nextDay = DateUtil.format(cd.getTime(), pattern);
  39. return nextDay;
  40. }
  41. /**
  42. * 计算传入时间的相对位移时间,后几天或者前几天
  43. * @param theDay
  44. * @param interval
  45. * @return
  46. */
  47. public static String getDay(String theDay, int interval){
  48. Calendar cd = Calendar.getInstance();
  49. cd.setTime(DateUtil.parse(theDay, pattern));
  50. cd.add(Calendar.DATE, interval);
  51. String ThatDay = DateUtil.format(cd.getTime(), pattern);
  52. return ThatDay;
  53. }
  54. /**
  55. * 计算两个日期相差天数
  56. * @param beginDate
  57. * @param endDate
  58. * @return
  59. */
  60. public static long getInterval(String beginDate, String endDate){
  61. Date begin = DateUtil.parse(beginDate, pattern);
  62. Date end = DateUtil.parse(endDate, pattern);
  63. return (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000);
  64. }
  65. /**
  66. * 判断日期是否为周五
  67. * @param theDay
  68. * @return
  69. */
  70. public static boolean isWeekLastDay(String theDay){
  71. Calendar cal = Calendar.getInstance();
  72. cal.setTime(DateUtil.parse(theDay, pattern));
  73. int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
  74. int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
  75. return weekDays[w] == 5;
  76. }
  77. /**
  78. * 根据传入时间,算出这周开始日期和结束日期
  79. * @param theDay
  80. * @return
  81. */
  82. public static String[] getWeekRange(String theDay){
  83. SimpleDateFormat format = new SimpleDateFormat(pattern);
  84. Calendar cal = Calendar.getInstance();
  85. cal.setTime(DateUtil.parse(theDay, pattern));
  86. int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
  87. int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
  88. int today = weekDays[w];
  89. int begin = today == 7 ? 0 : -today;
  90. int end = today == 7 ? 6 : 6 - today;
  91. Calendar beginDate = Calendar.getInstance();
  92. beginDate.setTime(DateUtil.parse(theDay, pattern));
  93. Calendar endDate = Calendar.getInstance();
  94. endDate.setTime(DateUtil.parse(theDay, pattern));
  95. beginDate.add(Calendar.DAY_OF_MONTH, begin);
  96. endDate.add(Calendar.DAY_OF_MONTH, end);
  97. String beginDateStr = format.format(beginDate.getTime());
  98. String endDateStr = format.format(endDate.getTime());
  99. return new String[]{beginDateStr, endDateStr};
  100. }
  101. /**
  102. * 根据传入时间,算出这个月的开始日期和结束日期
  103. * @param theDay
  104. * @return
  105. */
  106. public static String[] getMonthRange(String theDay){
  107. String[] split = theDay.split("-");
  108. String beginDateStr = split[0] + "-" + split[1] + "-" + "01";
  109. Calendar endDate = Calendar.getInstance();
  110. endDate.setTime(DateUtil.parse(theDay, pattern));
  111. int actualMaximum = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
  112. String endDateStr = split[0] + "-" + split[1] + "-" + actualMaximum;
  113. return new String[]{beginDateStr, endDateStr};
  114. }
  115. /**
  116. * 判断日期是否为周五
  117. * @param theDay
  118. * @return
  119. */
  120. public static boolean isMonthLastDay(String theDay){
  121. Calendar calendar = Calendar.getInstance();
  122. calendar.setTime(DateUtil.parse(theDay, pattern));
  123. return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  124. }
  125. }