java获取当前时间的年周月季度等的开始结束时间

朱雀 2022-10-09 12:07 432阅读 0赞
  1. ```java
  2. package com.hzcloud.hz.admin.utils;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. * 获取当前时间的年周月季度等的开始结束时间
  8. */
  9. public class TimeUtil {
  10. public static void main(String[] args) {
  11. System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
  12. System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
  13. System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
  14. System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
  15. System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
  16. System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
  17. System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
  18. System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
  19. System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
  20. System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
  21. System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
  22. System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
  23. System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
  24. System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
  25. }
  26. /**
  27. * 获取 当前年、半年、季度、月、日、小时 开始结束时间
  28. */
  29. private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
  30. private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
  31. private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  32. /**
  33. * 获得本周的第一天,周一
  34. *
  35. * @return
  36. */
  37. public static Date getCurrentWeekDayStartTime() {
  38. Calendar c = Calendar.getInstance();
  39. try {
  40. int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
  41. c.add(Calendar.DATE, -weekday);
  42. c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. return c.getTime();
  47. }
  48. /**
  49. * 获得本周的最后一天,周日
  50. *
  51. * @return
  52. */
  53. public static Date getCurrentWeekDayEndTime() {
  54. Calendar c = Calendar.getInstance();
  55. try {
  56. int weekday = c.get(Calendar.DAY_OF_WEEK);
  57. c.add(Calendar.DATE, 8 - weekday);
  58. c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. return c.getTime();
  63. }
  64. /**
  65. * 获得本天的开始时间
  66. *
  67. * @return
  68. */
  69. public static Date getCurrentDayStartTime() {
  70. Date now = new Date();
  71. try {
  72. now = shortSdf.parse(shortSdf.format(now));
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. return now;
  77. }
  78. /**
  79. * 获得本天的结束时间
  80. *
  81. * @return
  82. */
  83. public static Date getCurrentDayEndTime() {
  84. Date now = new Date();
  85. try {
  86. now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. return now;
  91. }
  92. /**
  93. * 获得本小时的开始时间
  94. *
  95. * @return
  96. */
  97. public static Date getCurrentHourStartTime() {
  98. Date now = new Date();
  99. try {
  100. now = longHourSdf.parse(longHourSdf.format(now));
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. return now;
  105. }
  106. /**
  107. * 获得本小时的结束时间
  108. *
  109. * @return
  110. */
  111. public static Date getCurrentHourEndTime() {
  112. Date now = new Date();
  113. try {
  114. now = longSdf.parse(longHourSdf.format(now) + ":59:59");
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return now;
  119. }
  120. /**
  121. * 获得本月的开始时间
  122. *
  123. * @return
  124. */
  125. public static Date getCurrentMonthStartTime() {
  126. Calendar c = Calendar.getInstance();
  127. Date now = null;
  128. try {
  129. c.set(Calendar.DATE, 1);
  130. now = shortSdf.parse(shortSdf.format(c.getTime()));
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. return now;
  135. }
  136. /**
  137. * 本月的结束时间
  138. *
  139. * @return
  140. */
  141. public static Date getCurrentMonthEndTime() {
  142. Calendar c = Calendar.getInstance();
  143. Date now = null;
  144. try {
  145. c.set(Calendar.DATE, 1);
  146. c.add(Calendar.MONTH, 1);
  147. c.add(Calendar.DATE, -1);
  148. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152. return now;
  153. }
  154. /**
  155. * 当前年的开始时间
  156. *
  157. * @return
  158. */
  159. public static Date getCurrentYearStartTime() {
  160. Calendar c = Calendar.getInstance();
  161. Date now = null;
  162. try {
  163. c.set(Calendar.MONTH, 0);
  164. c.set(Calendar.DATE, 1);
  165. now = shortSdf.parse(shortSdf.format(c.getTime()));
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. }
  169. return now;
  170. }
  171. /**
  172. * 当前年的结束时间
  173. *
  174. * @return
  175. */
  176. public static Date getCurrentYearEndTime() {
  177. Calendar c = Calendar.getInstance();
  178. Date now = null;
  179. try {
  180. c.set(Calendar.MONTH, 11);
  181. c.set(Calendar.DATE, 31);
  182. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. return now;
  187. }
  188. /**
  189. * 当前季度的开始时间
  190. *
  191. * @return
  192. */
  193. public static Date getCurrentQuarterStartTime() {
  194. Calendar c = Calendar.getInstance();
  195. int currentMonth = c.get(Calendar.MONTH) + 1;
  196. Date now = null;
  197. try {
  198. if (currentMonth >= 1 && currentMonth <= 3)
  199. c.set(Calendar.MONTH, 0);
  200. else if (currentMonth >= 4 && currentMonth <= 6)
  201. c.set(Calendar.MONTH, 3);
  202. else if (currentMonth >= 7 && currentMonth <= 9)
  203. c.set(Calendar.MONTH, 4);
  204. else if (currentMonth >= 10 && currentMonth <= 12)
  205. c.set(Calendar.MONTH, 9);
  206. c.set(Calendar.DATE, 1);
  207. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. }
  211. return now;
  212. }
  213. /**
  214. * 当前季度的结束时间
  215. *
  216. * @return
  217. */
  218. public static Date getCurrentQuarterEndTime() {
  219. Calendar c = Calendar.getInstance();
  220. int currentMonth = c.get(Calendar.MONTH) + 1;
  221. Date now = null;
  222. try {
  223. if (currentMonth >= 1 && currentMonth <= 3) {
  224. c.set(Calendar.MONTH, 2);
  225. c.set(Calendar.DATE, 31);
  226. } else if (currentMonth >= 4 && currentMonth <= 6) {
  227. c.set(Calendar.MONTH, 5);
  228. c.set(Calendar.DATE, 30);
  229. } else if (currentMonth >= 7 && currentMonth <= 9) {
  230. c.set(Calendar.MONTH, 8);
  231. c.set(Calendar.DATE, 30);
  232. } else if (currentMonth >= 10 && currentMonth <= 12) {
  233. c.set(Calendar.MONTH, 11);
  234. c.set(Calendar.DATE, 31);
  235. }
  236. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  237. } catch (Exception e) {
  238. e.printStackTrace();
  239. }
  240. return now;
  241. }
  242. /**
  243. * 获取前/后半年的开始时间
  244. *
  245. * @return
  246. */
  247. public static Date getHalfYearStartTime() {
  248. Calendar c = Calendar.getInstance();
  249. int currentMonth = c.get(Calendar.MONTH) + 1;
  250. Date now = null;
  251. try {
  252. if (currentMonth >= 1 && currentMonth <= 6) {
  253. c.set(Calendar.MONTH, 0);
  254. } else if (currentMonth >= 7 && currentMonth <= 12) {
  255. c.set(Calendar.MONTH, 6);
  256. }
  257. c.set(Calendar.DATE, 1);
  258. now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
  259. } catch (Exception e) {
  260. e.printStackTrace();
  261. }
  262. return now;
  263. }
  264. /**
  265. * 获取前/后半年的结束时间
  266. *
  267. * @return
  268. */
  269. public static Date getHalfYearEndTime() {
  270. Calendar c = Calendar.getInstance();
  271. int currentMonth = c.get(Calendar.MONTH) + 1;
  272. Date now = null;
  273. try {
  274. if (currentMonth >= 1 && currentMonth <= 6) {
  275. c.set(Calendar.MONTH, 5);
  276. c.set(Calendar.DATE, 30);
  277. } else if (currentMonth >= 7 && currentMonth <= 12) {
  278. c.set(Calendar.MONTH, 11);
  279. c.set(Calendar.DATE, 31);
  280. }
  281. now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
  282. } catch (Exception e) {
  283. e.printStackTrace();
  284. }
  285. return now;
  286. }
  287. }

发表评论

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

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

相关阅读