【java时间工具】Java获取当天、本周、本月、本季度、本年等 开始及结束时间

  1. import java.sql.Timestamp;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.time.Month;
  6. import java.time.ZoneOffset;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. /**
  11. * 时间工具类
  12. *
  13. * @time 2020年5月11日 下午3:55:18
  14. */
  15. public class DateUtil {
  16. /**
  17. * YYYY_MM_ddHHmmss字符串转 YYYYMMdd
  18. *
  19. * @param Str
  20. * @return
  21. */
  22. public static String getYYYYMMddStr(String Str) {
  23. String needStr = "";
  24. try {
  25. Date date = getDateYYYY_MM_ddHHmmss(Str);
  26. needStr = getStrYYYYMMdd(date);
  27. } catch (Exception e) {
  28. // TODO: handle exception
  29. }
  30. return needStr;
  31. }
  32. /**
  33. * YYYYMMdd字符串转 YYYY-MM-dd
  34. *
  35. * @param Str
  36. * @return
  37. */
  38. public static String getYYYY_MM_ddStr(String Str) {
  39. String needStr = "";
  40. try {
  41. Date date = getDateYYYYMMdd(Str);
  42. needStr = getDateStr(date, "yyy-MM-dd");
  43. } catch (Exception e) {
  44. // TODO: handle exception
  45. }
  46. return needStr;
  47. }
  48. /**
  49. * YYYYMMdd字符串转 月份
  50. *
  51. * @param Str
  52. * @return
  53. */
  54. public static String getMonthStr(String Str) {
  55. String[] months = {"一月", "二月", "三月", "四月", "五月", "六月",
  56. "七月", "八月", "九月", "十月", "十一月", "十二月"};
  57. String needStr = "";
  58. try {
  59. Date date = getDateYYYYMMdd(Str);
  60. needStr = getDateStr(date, "yyy-MM-dd");
  61. } catch (Exception e) {
  62. // TODO: handle exception
  63. }
  64. Integer monthIndex = Integer.parseInt(needStr.split("-")[1]) - 1;
  65. return months[monthIndex];
  66. }
  67. public static String getDateStr(Date date, String format) {
  68. SimpleDateFormat df = new SimpleDateFormat(format);
  69. return df.format(date);
  70. }
  71. public static String getStrYYYYMMdd(Date date) {
  72. SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
  73. return df.format(date);
  74. }
  75. public static String getStrYYYYMMddHHmmss(Date date) {
  76. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  77. return df.format(date);
  78. }
  79. public static String getStrYYYY_MM_ddHHmmss(Date date) {
  80. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  81. return df.format(date);
  82. }
  83. public static String getCurrentDateTime() {
  84. Date date = new Date();
  85. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  86. return df.format(date);
  87. }
  88. public static Date getDateYYYYMMdd(String str) throws ParseException {
  89. SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
  90. return df.parse(str);
  91. }
  92. public static Date getDateYYYYMMddHHmmss(String str) throws ParseException {
  93. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
  94. return df.parse(str);
  95. }
  96. public static Date getDateYYYY_MM_ddHHmmss(String str) throws ParseException {
  97. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  98. return df.parse(str);
  99. }
  100. public static Date getDate(Date date) throws ParseException {
  101. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  102. String sDate = sdf2.format(date);
  103. //Date date1 = getDateYYYY_MM_ddHHmmss(sDate);
  104. Date date1 = Timestamp.valueOf(sDate);
  105. return date1;
  106. }
  107. public static String getFetureDate(int past) {
  108. Calendar calendar = Calendar.getInstance();
  109. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
  110. Date today = calendar.getTime();
  111. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  112. String result = format.format(today);
  113. return result;
  114. }
  115. public static ArrayList<String> getFetureDateAll(int intervals) {
  116. ArrayList<String> fetureDaysList = new ArrayList<>();
  117. for (int i = 0; i < intervals; i++) {
  118. fetureDaysList.add(getFetureDate(i));
  119. }
  120. return fetureDaysList;
  121. }
  122. public static String dateToWeek(String datetime) {
  123. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  124. String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  125. Calendar cal = Calendar.getInstance();
  126. Date date;
  127. try {
  128. date = sdf.parse(datetime);
  129. cal.setTime(date);
  130. } catch (ParseException e) {
  131. e.printStackTrace();
  132. }
  133. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  134. return weekDays[w];
  135. }
  136. public static String getOffsetStr(String format, Integer offsetDay) {
  137. //获取多少天之前的
  138. Calendar calendar1 = Calendar.getInstance();
  139. SimpleDateFormat sdf1 = new SimpleDateFormat(format);
  140. calendar1.add(Calendar.DATE, offsetDay);
  141. String agoDate = sdf1.format(calendar1.getTime());
  142. return agoDate;
  143. }
  144. /**
  145. * 获得当天0点时间
  146. *
  147. * @return
  148. */
  149. public static Date getTimesMorning() {
  150. Calendar cal = Calendar.getInstance();
  151. cal.set(Calendar.HOUR_OF_DAY, 0);
  152. cal.set(Calendar.SECOND, 0);
  153. cal.set(Calendar.MINUTE, 0);
  154. cal.set(Calendar.MILLISECOND, 0);
  155. return cal.getTime();
  156. }
  157. public static String getTimesMorningStr() {
  158. Date date = getTimesMorning();
  159. return getStrYYYY_MM_ddHHmmss(date);
  160. }
  161. /**
  162. * 获得本周一0点时间
  163. *
  164. * @return
  165. */
  166. public static Date getTimesWeekMorning() {
  167. Calendar cal = Calendar.getInstance();
  168. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  169. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  170. return cal.getTime();
  171. }
  172. public static String getTimesWeekMorningStr() {
  173. Date date = getTimesWeekMorning();
  174. return getStrYYYY_MM_ddHHmmss(date);
  175. }
  176. /**
  177. * 获得本月第一天0点时间
  178. *
  179. * @return
  180. */
  181. public static Date getTimesMonthMorning() {
  182. Calendar cal = Calendar.getInstance();
  183. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  184. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  185. return cal.getTime();
  186. }
  187. public static String getTimesMonthMorningStr() {
  188. Date date = getTimesMonthMorning();
  189. return getStrYYYY_MM_ddHHmmss(date);
  190. }
  191. /**
  192. * 获取本季度零点
  193. *
  194. * @return
  195. */
  196. public static Date getCurrentQuarterStartTime() {
  197. Calendar c = Calendar.getInstance();
  198. int currentMonth = c.get(Calendar.MONTH) + 1;
  199. SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  200. SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
  201. Date now = null;
  202. try {
  203. if (currentMonth >= 1 && currentMonth <= 3)
  204. c.set(Calendar.MONTH, 0);
  205. else if (currentMonth >= 4 && currentMonth <= 6)
  206. c.set(Calendar.MONTH, 3);
  207. else if (currentMonth >= 7 && currentMonth <= 9)
  208. c.set(Calendar.MONTH, 4);
  209. else if (currentMonth >= 10 && currentMonth <= 12)
  210. c.set(Calendar.MONTH, 9);
  211. c.set(Calendar.DATE, 1);
  212. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. }
  216. return now;
  217. }
  218. public static String getCurrentQuarterStartTimeStr() {
  219. Date date = getStartDayOfQuarter();
  220. return getStrYYYY_MM_ddHHmmss(date);
  221. }
  222. /**
  223. * 本年度开始时间
  224. *
  225. * @return
  226. */
  227. public static Date getCurrentYearStartTime() {
  228. Calendar currCal = Calendar.getInstance();
  229. int currentYear = currCal.get(Calendar.YEAR);
  230. return getYearFirst(currentYear);
  231. }
  232. public static String getCurrentYearStartTimeStr() {
  233. Date date = getCurrentYearStartTime();
  234. return getStrYYYY_MM_ddHHmmss(date);
  235. }
  236. /**
  237. * 获取某年第一天日期
  238. *
  239. * @param year 年份
  240. * @return Date
  241. */
  242. public static Date getYearFirst(int year) {
  243. Calendar calendar = Calendar.getInstance();
  244. calendar.clear();
  245. calendar.set(Calendar.YEAR, year);
  246. Date currYearFirst = calendar.getTime();
  247. return currYearFirst;
  248. }
  249. /**
  250. * 获取某季度的第一天
  251. * @return
  252. */
  253. public static Date getStartDayOfQuarter() {
  254. LocalDate resDate = LocalDate.now();
  255. Month month = resDate.getMonth();
  256. Month firstMonthOfQuarter = month.firstMonthOfQuarter();
  257. resDate = LocalDate.of(resDate.getYear(), firstMonthOfQuarter, 1);
  258. return Date.from(resDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
  259. }
  260. public static void main(String[] args) {
  261. String result = getCurrentQuarterStartTimeStr();
  262. System.out.println(result);
  263. }
  264. }

发表评论

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

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

相关阅读