JDK8以前使用SImpleDateFormate类格式化日期,因为在SImple DateFormate中存在Calendar实例引用,而在caleander中得establish中存在clear()和set()操作,在多线程情况下会产生问题,导致后面得后面线程修改之前线程,因此SImpleDateFormate是线程不安全得;

JDK8中使用DateTimeFormate日期格式化。在JDK8中新增得类是final关键子修饰不可变线程安全,所以可以安全的操作日期转换。

解决SImpleDateFormate线程不安全:

采用局部变量的方式,缺点是每次调用方法时,会有对象产生,对象的回收

采用synchronized或lock,缺点是同一时间内只有一个线程在操作,降低效率

采用第三方框架:joda-time

使用ThreadLocal采用线程副本的方式,使每个线程自身拥有SimpleDateFormate对象。

public class DateUtil {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static ThreadLocal threadLocal = new ThreadLocal(); public static Date parse(String textDate) throws ParseException {
return getDateFormat().parse(textDate);
}
public static String format(Date date) throws ParseException {
return getDateFormat().format(date);
}
public static DateFormat getDateFormat() {
DateFormat df = (DateFormat) threadLocal.get();
if (df == null) {
df = new SimpleDateFormat(DATE_FORMAT);
threadLocal.set(df);
}
return df;
} public static void main(String[] args) throws ParseException {
String format = DateUtil.format(new Date());
System.out.println(format);
Date parse = DateUtil.parse("2019-12-03 16:02:59");
System.out.println(parse);
}
}

JDK8中日期函数操作

1. 获得当前时间,当前时间

        LocalDate localDate = LocalDate.now();
System.out.println(localDate);//2019-12-03
Date date = new Date();
System.out.println(date);//Tue Dec 03 14:49:30 CST 2019

2. 获得当前毫秒数

Long millisecond = Instant.now().toEpochMilli();

3.  毫秒数转日期

LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1575356644592L), ZoneId.systemDefault());
System.out.println(localDateTime);

4. 日期转字符串

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = now.format(formatter);
System.out.println(format);//2019-12-03 15:09:38

5. 字符串转日期

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-12-03 13:52:50", dtf);
System.out.println(parse);

最新文章

  1. pk8和x509.pem转换成keystore
  2. mysql 5.7配置文件参数详解
  3. PostSharp-4.3.22安装包_KeyGen发布
  4. html5 datalist自动完成
  5. 字节流和字符流(BufferedReader类和BufferedWriter类)
  6. ADB理解
  7. 【原】Oracle拼接字段
  8. textarea文本域宽度和高度(width、height)自己主动适应变化处理
  9. python 模块部分补充知识
  10. windows服务器安全管理工具——IISCrypto
  11. Chrome添加Axure RP插件
  12. Nmap 命令操作详解
  13. 一、IntelliJ IDEA创建java项目
  14. Debian 9 strech 安装 ROS lunar
  15. 一个kubeadm.config文件--定义了token,扩展了默认端口,外部ETCD集群,自定义docker仓库,基于ipvs的kubeproxy
  16. 剑指offer(61)序列化二叉树
  17. 基于Quartz.NET 实现可中断的任务(转)
  18. mysql中json_remove函数的使用?
  19. CountDownLatch、CyclicBarrier、Semaphore 区别
  20. Hadoop HA 高可用集群的搭建

热门文章

  1. C# 栈=>随时读取栈中最小值
  2. Scala操作外部数据
  3. [已解决]报错: warning: LF will be replaced by CRLF in lib/anime.min.js
  4. ac自动机(tree+kmp模板)
  5. python学习第六天运算符总结大全
  6. XSS——跨站脚本攻击
  7. 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符
  8. "=="、equals、hashCode之间的区别
  9. Codeforces 633F 树的直径/树形DP
  10. javascript 浏览器定位