DateUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2019-2020 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.utils;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.time.*;
  20. import java.time.format.DateTimeFormatter;
  21. import java.util.Date;
  22. /**
  23. * @author: liaojinlong
  24. * @date: 2020/6/11 16:28
  25. * @apiNote: JDK 8 新日期类 格式化与字符串转换 工具类
  26. */
  27. public class DateUtil {
  28. public static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  29. public static final DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  30. public static final DateTimeFormatter DFYMD = DateTimeFormatter.ofPattern("yyyyMMdd");
  31. public static final DateTimeFormatter yyyy_mm_dd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  32. /**
  33. * LocalDateTime 转时间戳
  34. *
  35. * @param localDateTime /
  36. * @return /
  37. */
  38. public static Long getTimeStamp(LocalDateTime localDateTime) {
  39. return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
  40. }
  41. /**
  42. * 时间戳转LocalDateTime
  43. *
  44. * @param timeStamp /
  45. * @return /
  46. */
  47. public static LocalDateTime fromTimeStamp(Long timeStamp) {
  48. return LocalDateTime.ofEpochSecond(timeStamp, 0, OffsetDateTime.now().getOffset());
  49. }
  50. /**
  51. * LocalDateTime 转 Date Jdk8 后 不推荐使用 {@link Date} Date
  52. *
  53. * @param localDateTime /
  54. * @return /
  55. */
  56. public static Date toDate(LocalDateTime localDateTime) {
  57. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  58. }
  59. /**
  60. * LocalDate 转 Date Jdk8 后 不推荐使用 {@link Date} Date
  61. *
  62. * @param localDate /
  63. * @return /
  64. */
  65. public static Date toDate(LocalDate localDate) {
  66. return toDate(localDate.atTime(LocalTime.now(ZoneId.systemDefault())));
  67. }
  68. /**
  69. * Date转 LocalDateTime Jdk8 后 不推荐使用 {@link Date} Date
  70. *
  71. * @param date /
  72. * @return /
  73. */
  74. public static LocalDateTime toLocalDateTime(Date date) {
  75. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  76. }
  77. /**
  78. * 日期 格式化
  79. *
  80. * @param localDateTime /
  81. * @param patten /
  82. * @return /
  83. */
  84. public static String localDateTimeFormat(LocalDateTime localDateTime, String patten) {
  85. DateTimeFormatter df = DateTimeFormatter.ofPattern(patten);
  86. return df.format(localDateTime);
  87. }
  88. /**
  89. * 日期 格式化
  90. *
  91. * @param localDateTime /
  92. * @param df /
  93. * @return /
  94. */
  95. public static String localDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter df) {
  96. return df.format(localDateTime);
  97. }
  98. /**
  99. * 日期格式化 yyyy-MM-dd HH:mm:ss
  100. *
  101. * @param localDateTime /
  102. * @return /
  103. */
  104. public static String localDateTimeFormatyMdHms(LocalDateTime localDateTime) {
  105. return DFY_MD_HMS.format(localDateTime);
  106. }
  107. /**
  108. * 日期格式化 yyyy-MM-dd
  109. *
  110. * @param localDateTime /
  111. * @return /
  112. */
  113. public String localDateTimeFormatyMd(LocalDateTime localDateTime) {
  114. return DFY_MD.format(localDateTime);
  115. }
  116. /**
  117. * 日期格式化 yyyy-MM-dd
  118. *
  119. * @param localDateTime /
  120. * @return /
  121. */
  122. public static String formatYmd(LocalDate localDateTime) {
  123. return DFYMD.format(localDateTime);
  124. }
  125. public static LocalDate parseYmd(String localDateTime) {
  126. return LocalDate.parse(localDateTime, DFYMD);
  127. }
  128. /**
  129. * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
  130. *
  131. * @param localDateTime /
  132. * @return /
  133. */
  134. public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, String pattern) {
  135. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
  136. return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
  137. }
  138. /**
  139. * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
  140. *
  141. * @param localDateTime /
  142. * @return /
  143. */
  144. public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, DateTimeFormatter dateTimeFormatter) {
  145. return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
  146. }
  147. public static String format(String date) {
  148. SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
  149. SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy年MM月dd");
  150. Date d;
  151. try {
  152. d = sourceFormat.parse(date);
  153. String formatDate = targetFormat.format(d);
  154. return formatDate;
  155. } catch (ParseException e) {
  156. e.printStackTrace();
  157. }
  158. return date;
  159. }
  160. public static String localDate(LocalDate date) {
  161. return DFY_MD.format(date);
  162. }
  163. /**
  164. * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd HH:mm:ss
  165. *
  166. * @param localDateTime /
  167. * @return /
  168. */
  169. public static LocalDateTime parseLocalDateTimeFormatyMdHms(String localDateTime) {
  170. return LocalDateTime.from(DFY_MD_HMS.parse(localDateTime));
  171. }
  172. public static void main(String[] args) {
  173. System.out.println(DateUtil.format("20230601"));
  174. ;
  175. }
  176. }