LocalDateTimeUtils.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package com.ydtech.modules.inv.utils;
  2. /**
  3. * @author jianlong
  4. * @date 2024-01-24 18:26
  5. */
  6. import java.time.*;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.ChronoUnit;
  9. import java.time.temporal.TemporalAdjusters;
  10. import java.util.Date;
  11. /**
  12. * LocalDateTime工具类
  13. *
  14. * @author CL
  15. */
  16. public class LocalDateTimeUtils {
  17. /**
  18. * 当前时间
  19. *
  20. * @return
  21. */
  22. public static LocalDateTime now() {
  23. return LocalDateTime.now();
  24. }
  25. /**
  26. * Date 转 LocalDateTime
  27. *
  28. * @return
  29. */
  30. public static LocalDateTime convert(Date date) {
  31. return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
  32. }
  33. /**
  34. * 获取12月前第一天
  35. */
  36. public static LocalDateTime get12MonthsBefore() {
  37. return LocalDateTime.now().minusMonths(11).with(TemporalAdjusters.firstDayOfMonth())
  38. .withHour(0)
  39. .withMinute(0)
  40. .withSecond(0)
  41. .withNano(0);
  42. }
  43. /**
  44. * 获取当前月最后一天
  45. */
  46. public static LocalDateTime getTodayLastDay() {
  47. return LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth()).withHour(0)
  48. .withMinute(0)
  49. .withSecond(0)
  50. .withNano(0);
  51. }
  52. /**
  53. * LocalDateTime 转 Date
  54. *
  55. * @return
  56. */
  57. public static Date convert(LocalDateTime localDateTime) {
  58. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  59. }
  60. /**
  61. * 今天开始时间
  62. *
  63. * @return
  64. */
  65. public static LocalDateTime todayStartTime() {
  66. return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
  67. }
  68. /**
  69. * 今天结束时间
  70. *
  71. * @return
  72. */
  73. public static LocalDateTime todayEndTime() {
  74. return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
  75. }
  76. /**
  77. * 昨天开始时间
  78. *
  79. * @return
  80. */
  81. public static LocalDateTime yesterdayStartTime() {
  82. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MIN);
  83. }
  84. /**
  85. * 昨天结束时间
  86. *
  87. * @return
  88. */
  89. public static LocalDateTime yesterdayEndTime() {
  90. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.DAYS), LocalTime.MAX);
  91. }
  92. /**
  93. * 最近7天开始时间
  94. *
  95. * @return
  96. */
  97. public static LocalDateTime last7DaysStartTime() {
  98. return LocalDateTime.of(LocalDate.now().minus(6L, ChronoUnit.DAYS), LocalTime.MIN);
  99. }
  100. /**
  101. * 最近7天结束时间
  102. *
  103. * @return
  104. */
  105. public static LocalDateTime last7DaysEndTime() {
  106. return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
  107. }
  108. /**
  109. * 最近30天开始时间
  110. *
  111. * @return
  112. */
  113. public static LocalDateTime last30DaysStartTime() {
  114. return LocalDateTime.of(LocalDate.now().minus(29L, ChronoUnit.DAYS), LocalTime.MIN);
  115. }
  116. /**
  117. * 最近30天结束时间
  118. *
  119. * @return
  120. */
  121. public static LocalDateTime last30DaysEndTime() {
  122. return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
  123. }
  124. /**
  125. * 最近一年开始时间
  126. *
  127. * @return
  128. */
  129. public static LocalDateTime last1YearStartTime() {
  130. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).plus(1L, ChronoUnit.DAYS), LocalTime.MIN);
  131. }
  132. /**
  133. * 最近一年结束时间
  134. *
  135. * @return
  136. */
  137. public static LocalDateTime last1YearEndTime() {
  138. return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
  139. }
  140. /**
  141. * 本周开始时间
  142. *
  143. * @return
  144. */
  145. public static LocalDateTime weekStartTime() {
  146. LocalDate now = LocalDate.now();
  147. return LocalDateTime.of(now.minusDays(now.getDayOfWeek().getValue() - 1), LocalTime.MIN);
  148. }
  149. /**
  150. * 本周结束时间
  151. *
  152. * @return
  153. */
  154. public static LocalDateTime weekEndTime() {
  155. LocalDate now = LocalDate.now();
  156. return LocalDateTime.of(now.plusDays(7 - now.getDayOfWeek().getValue()), LocalTime.MAX);
  157. }
  158. /**
  159. * 本月开始时间
  160. *
  161. * @return
  162. */
  163. public static LocalDateTime monthStartTime() {
  164. return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
  165. }
  166. /**
  167. * 本月结束时间
  168. *
  169. * @return
  170. */
  171. public static LocalDateTime monthEndTime() {
  172. return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
  173. }
  174. /**
  175. * 本季度开始时间
  176. *
  177. * @return
  178. */
  179. public static LocalDateTime quarterStartTime() {
  180. LocalDate now = LocalDate.now();
  181. Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
  182. return LocalDateTime.of(LocalDate.of(now.getYear(), month, 1), LocalTime.MIN);
  183. }
  184. /**
  185. * 本季度结束时间
  186. *
  187. * @return
  188. */
  189. public static LocalDateTime quarterEndTime() {
  190. LocalDate now = LocalDate.now();
  191. Month month = Month.of(now.getMonth().firstMonthOfQuarter().getValue()).plus(2L);
  192. return LocalDateTime.of(LocalDate.of(now.getYear(), month, month.length(now.isLeapYear())), LocalTime.MAX);
  193. }
  194. /**
  195. * 本半年开始时间
  196. *
  197. * @return
  198. */
  199. public static LocalDateTime halfYearStartTime() {
  200. LocalDate now = LocalDate.now();
  201. Month month = (now.getMonthValue() > 6) ? Month.JULY : Month.JANUARY;
  202. return LocalDateTime.of(LocalDate.of(now.getYear(), month, 1), LocalTime.MIN);
  203. }
  204. /**
  205. * 本半年结束时间
  206. *
  207. * @return
  208. */
  209. public static LocalDateTime halfYearEndTime() {
  210. LocalDate now = LocalDate.now();
  211. Month month = (now.getMonthValue() > 6) ? Month.DECEMBER : Month.JUNE;
  212. return LocalDateTime.of(LocalDate.of(now.getYear(), month, month.length(now.isLeapYear())), LocalTime.MAX);
  213. }
  214. /**
  215. * 本年开始时间
  216. *
  217. * @return
  218. */
  219. public static LocalDateTime yearStartTime() {
  220. return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
  221. }
  222. /**
  223. * 本年结束时间
  224. *
  225. * @return
  226. */
  227. public static LocalDateTime yearEndTime() {
  228. return LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
  229. }
  230. /**
  231. * 上周开始时间
  232. *
  233. * @return
  234. */
  235. public static LocalDateTime lastWeekStartTime() {
  236. LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
  237. return LocalDateTime.of(lastWeek.minusDays(lastWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
  238. }
  239. /**
  240. * 上周结束时间
  241. *
  242. * @return
  243. */
  244. public static LocalDateTime lastWeekEndTime() {
  245. LocalDate lastWeek = LocalDate.now().minus(1L, ChronoUnit.WEEKS);
  246. return LocalDateTime.of(lastWeek.plusDays(7 - lastWeek.getDayOfWeek().getValue()), LocalTime.MAX);
  247. }
  248. /**
  249. * 上月开始时间
  250. *
  251. * @return
  252. */
  253. public static LocalDateTime lastMonthStartTime() {
  254. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
  255. }
  256. /**
  257. * 上月结束时间
  258. *
  259. * @return
  260. */
  261. public static LocalDateTime lastMonthEndTime() {
  262. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
  263. }
  264. /**
  265. * 上季度开始时间
  266. *
  267. * @return
  268. */
  269. public static LocalDateTime lastQuarterStartTime() {
  270. LocalDate now = LocalDate.now();
  271. Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
  272. Month firstMonthOfLastQuarter = firstMonthOfQuarter.minus(3L);
  273. int yearOfLastQuarter = firstMonthOfQuarter.getValue() < 4 ? now.getYear() - 1 : now.getYear();
  274. return LocalDateTime.of(LocalDate.of(yearOfLastQuarter, firstMonthOfLastQuarter, 1), LocalTime.MIN);
  275. }
  276. /**
  277. * 上季度结束时间
  278. *
  279. * @return
  280. */
  281. public static LocalDateTime lastQuarterEndTime() {
  282. LocalDate now = LocalDate.now();
  283. Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
  284. Month firstMonthOfLastQuarter = firstMonthOfQuarter.minus(1L);
  285. int yearOfLastQuarter = firstMonthOfQuarter.getValue() < 4 ? now.getYear() - 1 : now.getYear();
  286. return LocalDateTime.of(LocalDate.of(yearOfLastQuarter, firstMonthOfLastQuarter, firstMonthOfLastQuarter.maxLength()), LocalTime.MAX);
  287. }
  288. /**
  289. * 上半年开始时间
  290. *
  291. * @return
  292. */
  293. public static LocalDateTime lastHalfYearStartTime() {
  294. LocalDate now = LocalDate.now();
  295. int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() : now.getYear() - 1;
  296. Month firstMonthOfLastHalfYear = (now.getMonthValue() > 6) ? Month.JANUARY : Month.JULY;
  297. return LocalDateTime.of(LocalDate.of(lastHalfYear, firstMonthOfLastHalfYear, 1), LocalTime.MIN);
  298. }
  299. /**
  300. * 上半年结束时间
  301. *
  302. * @return
  303. */
  304. public static LocalDateTime lastHalfYearEndTime() {
  305. LocalDate now = LocalDate.now();
  306. int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() : now.getYear() - 1;
  307. Month lastMonthOfLastHalfYear = (now.getMonthValue() > 6) ? Month.JUNE : Month.DECEMBER;
  308. return LocalDateTime.of(LocalDate.of(lastHalfYear, lastMonthOfLastHalfYear, lastMonthOfLastHalfYear.maxLength()), LocalTime.MAX);
  309. }
  310. /**
  311. * 上一年开始时间
  312. *
  313. * @return
  314. */
  315. public static LocalDateTime lastYearStartTime() {
  316. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
  317. }
  318. /**
  319. * 上一年结束时间
  320. *
  321. * @return
  322. */
  323. public static LocalDateTime lastYearEndTime() {
  324. return LocalDateTime.of(LocalDate.now().minus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
  325. }
  326. /**
  327. * 下周开始时间
  328. *
  329. * @return
  330. */
  331. public static LocalDateTime nextWeekStartTime() {
  332. LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
  333. return LocalDateTime.of(nextWeek.minusDays(nextWeek.getDayOfWeek().getValue() - 1), LocalTime.MIN);
  334. }
  335. /**
  336. * 下周结束时间
  337. *
  338. * @return
  339. */
  340. public static LocalDateTime nextWeekEndTime() {
  341. LocalDate nextWeek = LocalDate.now().plus(1L, ChronoUnit.WEEKS);
  342. return LocalDateTime.of(nextWeek.plusDays(7 - nextWeek.getDayOfWeek().getValue()), LocalTime.MAX);
  343. }
  344. /**
  345. * 下月开始时间
  346. *
  347. * @return
  348. */
  349. public static LocalDateTime nextMonthStartTime() {
  350. return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
  351. }
  352. /**
  353. * 下月结束时间
  354. *
  355. * @return
  356. */
  357. public static LocalDateTime nextMonthEndTime() {
  358. return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.MONTHS).with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
  359. }
  360. /**
  361. * 下季度开始时间
  362. *
  363. * @return
  364. */
  365. public static LocalDateTime nextQuarterStartTime() {
  366. LocalDate now = LocalDate.now();
  367. Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
  368. Month firstMonthOfNextQuarter = firstMonthOfQuarter.plus(3L);
  369. int yearOfNextQuarter = firstMonthOfQuarter.getValue() > 9 ? now.getYear() + 1 : now.getYear();
  370. return LocalDateTime.of(LocalDate.of(yearOfNextQuarter, firstMonthOfNextQuarter, 1), LocalTime.MIN);
  371. }
  372. /**
  373. * 下季度结束时间
  374. *
  375. * @return
  376. */
  377. public static LocalDateTime nextQuarterEndTime() {
  378. LocalDate now = LocalDate.now();
  379. Month firstMonthOfQuarter = Month.of(now.getMonth().firstMonthOfQuarter().getValue());
  380. Month firstMonthOfNextQuarter = firstMonthOfQuarter.plus(5L);
  381. int yearOfNextQuarter = firstMonthOfQuarter.getValue() > 9 ? now.getYear() + 1 : now.getYear();
  382. return LocalDateTime.of(LocalDate.of(yearOfNextQuarter, firstMonthOfNextQuarter, firstMonthOfNextQuarter.maxLength()), LocalTime.MAX);
  383. }
  384. /**
  385. * 上半年开始时间
  386. *
  387. * @return
  388. */
  389. public static LocalDateTime nextHalfYearStartTime() {
  390. LocalDate now = LocalDate.now();
  391. int nextHalfYear = (now.getMonthValue() > 6) ? now.getYear() + 1 : now.getYear();
  392. Month firstMonthOfNextHalfYear = (now.getMonthValue() > 6) ? Month.JANUARY : Month.JULY;
  393. return LocalDateTime.of(LocalDate.of(nextHalfYear, firstMonthOfNextHalfYear, 1), LocalTime.MIN);
  394. }
  395. /**
  396. * 上半年结束时间
  397. *
  398. * @return
  399. */
  400. public static LocalDateTime nextHalfYearEndTime() {
  401. LocalDate now = LocalDate.now();
  402. int lastHalfYear = (now.getMonthValue() > 6) ? now.getYear() + 1 : now.getYear();
  403. Month lastMonthOfNextHalfYear = (now.getMonthValue() > 6) ? Month.JUNE : Month.DECEMBER;
  404. return LocalDateTime.of(LocalDate.of(lastHalfYear, lastMonthOfNextHalfYear, lastMonthOfNextHalfYear.maxLength()), LocalTime.MAX);
  405. }
  406. /**
  407. * 下一年开始时间
  408. *
  409. * @return
  410. */
  411. public static LocalDateTime nextYearStartTime() {
  412. return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.firstDayOfYear()), LocalTime.MIN);
  413. }
  414. /**
  415. * 下一年结束时间
  416. *
  417. * @return
  418. */
  419. public static LocalDateTime nextYearEndTime() {
  420. return LocalDateTime.of(LocalDate.now().plus(1L, ChronoUnit.YEARS).with(TemporalAdjusters.lastDayOfYear()), LocalTime.MAX);
  421. }
  422. }