| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*
- * Copyright 2019-2020 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package me.zhengjie.utils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.*;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- /**
- * @author: liaojinlong
- * @date: 2020/6/11 16:28
- * @apiNote: JDK 8 新日期类 格式化与字符串转换 工具类
- */
- public class DateUtil {
- public static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- public static final DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- public static final DateTimeFormatter DFYMD = DateTimeFormatter.ofPattern("yyyyMMdd");
- public static final DateTimeFormatter yyyy_mm_dd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- /**
- * LocalDateTime 转时间戳
- *
- * @param localDateTime /
- * @return /
- */
- public static Long getTimeStamp(LocalDateTime localDateTime) {
- return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
- }
- /**
- * 时间戳转LocalDateTime
- *
- * @param timeStamp /
- * @return /
- */
- public static LocalDateTime fromTimeStamp(Long timeStamp) {
- return LocalDateTime.ofEpochSecond(timeStamp, 0, OffsetDateTime.now().getOffset());
- }
- /**
- * LocalDateTime 转 Date Jdk8 后 不推荐使用 {@link Date} Date
- *
- * @param localDateTime /
- * @return /
- */
- public static Date toDate(LocalDateTime localDateTime) {
- return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
- }
- /**
- * LocalDate 转 Date Jdk8 后 不推荐使用 {@link Date} Date
- *
- * @param localDate /
- * @return /
- */
- public static Date toDate(LocalDate localDate) {
- return toDate(localDate.atTime(LocalTime.now(ZoneId.systemDefault())));
- }
- /**
- * Date转 LocalDateTime Jdk8 后 不推荐使用 {@link Date} Date
- *
- * @param date /
- * @return /
- */
- public static LocalDateTime toLocalDateTime(Date date) {
- return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
- }
- /**
- * 日期 格式化
- *
- * @param localDateTime /
- * @param patten /
- * @return /
- */
- public static String localDateTimeFormat(LocalDateTime localDateTime, String patten) {
- DateTimeFormatter df = DateTimeFormatter.ofPattern(patten);
- return df.format(localDateTime);
- }
- /**
- * 日期 格式化
- *
- * @param localDateTime /
- * @param df /
- * @return /
- */
- public static String localDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter df) {
- return df.format(localDateTime);
- }
- /**
- * 日期格式化 yyyy-MM-dd HH:mm:ss
- *
- * @param localDateTime /
- * @return /
- */
- public static String localDateTimeFormatyMdHms(LocalDateTime localDateTime) {
- return DFY_MD_HMS.format(localDateTime);
- }
- /**
- * 日期格式化 yyyy-MM-dd
- *
- * @param localDateTime /
- * @return /
- */
- public String localDateTimeFormatyMd(LocalDateTime localDateTime) {
- return DFY_MD.format(localDateTime);
- }
- /**
- * 日期格式化 yyyy-MM-dd
- *
- * @param localDateTime /
- * @return /
- */
- public static String formatYmd(LocalDate localDateTime) {
- return DFYMD.format(localDateTime);
- }
- public static LocalDate parseYmd(String localDateTime) {
- return LocalDate.parse(localDateTime, DFYMD);
- }
- /**
- * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
- *
- * @param localDateTime /
- * @return /
- */
- public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, String pattern) {
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
- return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
- }
- /**
- * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
- *
- * @param localDateTime /
- * @return /
- */
- public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, DateTimeFormatter dateTimeFormatter) {
- return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
- }
- public static String format(String date) {
- SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
- SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy年MM月dd");
- Date d;
- try {
- d = sourceFormat.parse(date);
- String formatDate = targetFormat.format(d);
- return formatDate;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return date;
- }
- public static String localDate(LocalDate date) {
- return DFY_MD.format(date);
- }
- /**
- * 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd HH:mm:ss
- *
- * @param localDateTime /
- * @return /
- */
- public static LocalDateTime parseLocalDateTimeFormatyMdHms(String localDateTime) {
- return LocalDateTime.from(DFY_MD_HMS.parse(localDateTime));
- }
- public static void main(String[] args) {
- System.out.println(DateUtil.format("20230601"));
- ;
- }
- }
|