localdatetime转化为date_还在用SimpleDateFormat?Java8都发布N年了,转LocalDateTime吧

前言

Java8发布,已有数年之久,但是发现很多人都还是坚持着用SimpleDateFormat和Date进行时间操作。SimpleDateFormat这个类不是线程安全的,在使用的时候稍不注意,就会产生致命的问题。Date这个类,是可以重新设置时间的,这对于一些类内部的属性来说,是非常不安全的。

SimpleDateFormat是线程不安全的类

在阿里巴巴规约手册里,强制规定SimpleDateFormat是线程不安全的类,当定义为静态变量时,必须加锁处理。忽略线程安全问题,正是大多数Java初学者在进行时间转化时容易踩坑的点。

ce71df64775ef1eeed44ff3a2d0e0181.png

Date属性可以重新设置时间

比如有User.java如下:

public class User {​ private String username;​ private Date birthday;​ public User(String username, Date birthday) { this.username = username; this.birthday = birthday; }​ public String getUsername() { return username; }​ public void setUsername(String username) { this.username = username; }​ public Date getBirthday() { return birthday; }​ public void setBirthday(Date birthday) { this.birthday = birthday; }}

我们实例化该User

public class Main {​ public static void main(String[] args) { User user = new User("happyjava
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值