java中Date类和Strng类的灵活转化
作者:神秘时光? 时间:2022-12-09 07:47:24
在java开发的实际场景中,我们经常要对时间进行格式化处理,但是每次获取开发中自己需要的格式都要重新写一个方法,这样的代码看起来是非常的笨重并且冗余,为此通过以下的方法可以灵活的对时间进行Date类型和String类型进行转化:
话不多说,直接上代码!
先定义一个获取时间格式的枚举类
```//首先定义一个枚举类
public enum DateFormatEnum{
//年份
Y("yyyy"),
//年月
YM("yyyy-MM"),
//年月日
YMD("yyyy-MM-dd"),
//年月日时
YMDH("yyyy-MM-dd HH"),
//年月日时分
YMDHM("yyyy-MM-dd HH:mm"),
//年月日时分秒
YMDHMS("yyyy-MM-dd HH:mm:ss");
private String dataFormatType;
//构造函数
DateFormatEnum(String dataFormatType){
this.dataFormatType=dataFormatType;
}
//外部调用时间格式类型
String getDataFormatType(){
return this,dataFormatType;
}
}
再写一个简单的工具类方法就可以让时间灵活在Date类和String类上灵活转化了
public class DateUtil{
//Date类转换为String类(任意格式)
public static String changeDateToStr(Date date,DateFormatEnum dataFormatEnum){
SimpleDateFormat format = new SimpleDateFormat(dateFormatEnum.getDateFormatType);
return format.format(date)
}
//String类转换为Date类(任意格式)
public static Date changeStrToDate(String date,DateFormatEnum dateFormatEnum){
SimpleDateFormat format = new SimpleDateFormat(dateFormatEnum.getDateFormatType);
return format.parse(date);
}
//获取当前时间的String类型(任意格式)
public static String getStrNowDate(DateFormatEnum dateFormatEnum){
SimpleDateFormat format = new SimpleDateFormat(dateFormatEnum.getDateFormatType);
return format.format(new Date())
}
//获取当前时间的Date类型(任意格式)
public static Date getDateNowDate(DateFormatEnum dateFormatEnum){
SimpleDateFormat format = new SimpleDateFormat(dateFormatEnum.getDateFormatType);
return format.parse(new Date())
}
}
来源:https://blog.csdn.net/fei476662546/article/details/108752428
标签:java,Date类,Strng类,转化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Gradle学习教程之部署上传项目详解
2023-08-08 20:46:46
Kotlin实现在类里面创建main函数
2022-08-26 20:11:13
![](https://img.aspxhome.com/file/2023/7/94997_0s.jpg)
Android自定义view实现滑动解锁九宫格控件
2021-12-28 02:24:16
![](https://img.aspxhome.com/file/2023/2/98632_0s.gif)
Java C++ 算法题解leetcode652寻找重复子树
2022-08-17 23:58:09
![](https://img.aspxhome.com/file/2023/0/61120_0s.jpg)
详解Android中Runtime解决屏幕旋转问题(推荐)
2022-04-03 03:12:02
深入理解C#中foreach遍历的使用方法
2023-11-02 08:51:04
![](https://img.aspxhome.com/file/2023/5/82135_0s.jpg)
深入Android HandlerThread 使用及其源码完全解析
2023-11-30 19:16:56
![](https://img.aspxhome.com/file/2023/4/138614_0s.gif)
Java concurrency线程池之线程池原理(四)_动力节点Java学院整理
2023-08-12 21:13:13
Android 读取文件内容实现方法总结
2022-09-15 12:57:56
C#使用后台线程BackgroundWorker处理任务的总结
2023-12-08 10:28:19
![](https://img.aspxhome.com/file/2023/7/84427_0s.png)
C#基于DBContext(EF)实现通用增删改查的REST方法实例
2021-06-23 20:56:07
C# readnodefile()不能读取带有文件名为汉字的osg文件解决方法
2021-11-08 07:41:00
Spring Boot项目实战之拦截器与过滤器
2022-08-04 15:55:31
![](https://img.aspxhome.com/file/2023/0/72610_0s.png)
SpringBoot 在项目启动之后执行自定义方法的两种方式小结
2021-05-25 15:46:36
![](https://img.aspxhome.com/file/2023/8/71168_0s.jpg)
Java语法关于泛型与类型擦除的分析
2023-12-22 06:15:22
![](https://img.aspxhome.com/file/2023/9/86719_0s.png)
谈谈为JAXB和response设置编码,解决wechat4j中文乱码的问题
2023-07-31 01:34:27
![](https://img.aspxhome.com/file/2023/1/57621_0s.png)
零基础入门SpringMVC拦截器的配置与使用
2023-07-17 21:59:28
![](https://img.aspxhome.com/file/2023/0/116130_0s.png)
java 可变参数详解及实例
2021-06-01 10:02:42
Java多线程编程详细解释
2022-12-22 10:19:41
Android实现仿通讯录侧边栏滑动SiderBar效果代码
2021-08-03 21:07:45
![](https://img.aspxhome.com/file/2023/6/118966_0s.png)