当前位置: 首页 > java >正文

SpringBoot--手写日期格式转换工具类

原文网址:SpringBoot--手写日期格式转换工具类-CSDN博客

简介

本文介绍SpringBoot的手写日期格式化工具类。

实际上本工具类可以用于所有Java项目,不是SpringBoot的也可以。

问题描述

在实际开发中经常会需要日期的格式转换,比如:字符串解析为LocalDateTime、字符串解析为Date、LocalDateTime转Date等。

将其整理为一个工具类,可以方便地调用。(虽然hutool有类似的工具,但由于此工具bug太多,非常不建议使用)。

核心代码

常量

package com.knife.example.common.core.constant;public interface DateTimeFormatConstant {String DATE_TIME_FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss";String DATE_FORMAT_NORMAL = "yyyy-MM-dd";String TIME_FORMAT_NORMAL = "HH:mm:ss";
}

工具类

package com.knife.example.common.core.util;import com.knife.example.common.core.constant.DateTimeFormatConstant;import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;public class DateTimeUtil {/*** 格式化** @param localDateTime 时间* @param format        格式。见{@link DateTimeFormatConstant}*/public static String format(LocalDateTime localDateTime,String format) {return localDateTime.format(DateTimeFormatter.ofPattern(format));}public static String format(LocalDate localDate,String format) {return localDate.format(DateTimeFormatter.ofPattern(format));}public static String format(LocalTime localTime,String format) {return localTime.format(DateTimeFormatter.ofPattern(format));}public static LocalDateTime parseToLocalDateTime(String localDateTimeString,String format) {return LocalDateTime.parse(localDateTimeString, DateTimeFormatter.ofPattern(format));}public static LocalDate parseToLocalDate(String localDateString,String format) {return LocalDate.parse(localDateString, DateTimeFormatter.ofPattern(format));}public static LocalTime parseToLocalTime(String localTimeString,String format) {return LocalTime.parse(localTimeString, DateTimeFormatter.ofPattern(format));}public static LocalDateTime toLocalLocalDateTime(Date date) {return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();}public static LocalDate toLocalDate(Date date) {return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();}public static LocalTime toLocalTime(Date date) {return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();}public static Date toDate(LocalDateTime localDateTime) {return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());}public static Date toDate(LocalDate localDate) {return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());}public static Long toTimeStamp(LocalDateTime localDateTime) {return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();}public static Long toTimeStamp(LocalDate localDate) {return localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();}public static LocalDateTime toLocalDateTime(Long timeStamp) {return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp),ZoneId.systemDefault());}public static LocalDate toLocalDate(Long timeStamp) {return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp),ZoneId.systemDefault()).toLocalDate();}// public static void main(String[] args) {//     String format = format(LocalDateTime.now(), DateTimeFormatConstant.DATE_TIME_FORMAT_NORMAL);//     System.out.println(format);// }
}

用法

String format = DateTimeUtil.format(LocalDateTime.now(), DateTimeFormatConstant.DATE_TIME_FORMAT_NORMAL);
System.out.println(format);

http://www.xdnf.cn/news/19809.html

相关文章:

  • TiDB v8.5.3 单机集群部署指南
  • ASP.NET Core上传文件到minio
  • 【leetcode】236. 二叉树的最近公共祖先
  • 利用Base64传输二进制文件并执行的方法(适合没有ssh ftp等传输工具的嵌入式离线场景)
  • 研发文档版本混乱的根本原因是什么,怎么办
  • ELK 统一日志分析系统部署与实践指南(上)
  • 撤销修改 情况⼀:对于⼯作区的代码,还没有 add
  • 餐饮、跑腿、零售多场景下的同城外卖系统源码扩展方案
  • 图片移到根目录
  • Spring Boot + Spring MVC 项目结构
  • ARM汇编记忆
  • C# 简述委托,Func与Action委托。 他们之前有什么区别?
  • 告别手动复制粘贴:C# 实现 Excel 与 TXT 文本文件高效互转
  • 搭建分布式Hadoop集群[2025] 实战笔记
  • SQL分类详解:掌握DQL、DML、DDL等数据库语言类型
  • p049基于Flask的医疗预约与诊断系统
  • 删除⽂件之git
  • 避免侵权!这6个可免费下载字体网站能放心商用
  • 大模型推理加速深度对比:vLLM vs TensorRT-LLM vs ONNX Runtime,谁是生产环境最优解?
  • hot100——第十周
  • linux(cut,sort,uniq ,tr,sed,awk)命令介绍
  • 两个矩形之间的距离 python
  • 互联网大厂Java面试三大回合全解析:从语言特性到性能安全
  • Python数据分析与处理(一):读取不同格式.mat文件的具体方法【超详细】
  • 图解设计模式
  • python - ( js )object对象、json对象、字符串对象的相关方法、数组对象的相关方法、BOM对象、BOM模型中 Navigator 对象
  • Ubuntu中配置JMmeter工具
  • Java 类加载机制(ClassLoader)的必会知识点汇总
  • 当合规成为主旋律,PSP 如何推动链上消费市场迈向新蓝海?
  • MidJourney AI绘图工具测评:支持Discord指令生成图片,含图生图与非商业版权使用功能