转载整合:SimpleDateFormat日期格式的使用示例 和 JAVA多线程中SimpleDateFormat不安全的解决方案

本文主要是介绍转载整合:SimpleDateFormat日期格式的使用示例 和 JAVA多线程中SimpleDateFormat不安全的解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SimpleDateFormat日期格式的使用示例

SimpleDateFormat构造函数中字符串的格式,以及各部分代表的含义:

 import java.text.SimpleDateFormat;
import java.util.Date;public class test{public static void main(String args[]) {Date newTime = new Date();//设置时间格式SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E");SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E");SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E");SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E");//获取的时间,是本机的时间String formatDate1 = sdf1.format(newTime);String formatDate2 = sdf2.format(newTime);String formatDate3 = sdf3.format(newTime);String formatDate4 = sdf4.format(newTime);System.out.println(formatDate1);  System.out.println(formatDate2); System.out.println(formatDate3); System.out.println(formatDate4); }
}

运行结果:
在这里插入图片描述

字符串"yyyy-MM-dd hh:mm:ss",其中:

yyyy : 代表年(不去区分大小写) 假设年份为 2017

"y" , "yyy" , "yyyy" 匹配的都是4位完整的年 如 : "2017""yy" 匹配的是年分的后两位 如 : "15"超过4位,会在年份前面加"0"补位 如 "YYYYY"对应"02017"

MM : 代表月(只能使用大写) 假设月份为 9

"M" 对应 "9""MM" 对应 "09""MMM" 对应 "Sep""MMMM" 对应 "Sep"超出3位,仍然对应 "September"

dd : 代表日(只能使用小写) 假设为13号

"d" , "dd" 都对应 "13"超出2位,会在数字前面加"0"补位. 例如 "dddd" 对应 "0013"

hh : 代表时(区分大小写,大写为24进制计时,小写为12进制计时) 假设为15时

"H" , "HH" 都对应 "15" , 超出2位,会在数字前面加"0"补位. 例如 "HHHH" 对应 "0015""h" 对应 "3""hh" 对应 "03" , 超出2位,会在数字前面加"0"补位. 例如 "hhhh" 对应 "0003"

mm : 代表分(只能使用小写) 假设为32分

"m" , "mm" 都对应 "32" ,  超出2位,会在数字前面加"0"补位. 例如 "mmmm" 对应 "0032"

ss : 代表秒(只能使用小写) 假设为15秒

"s" , "ss" 都对应 "15" , 超出2位,会在数字前面加"0"补位. 例如 "ssss" 对应 "0015"

E : 代表星期(只能使用大写) 假设为 Sunday

"E" , "EE" , "EEE" 都对应 "Sun""EEEE" 对应 "Sunday" , 超出4位 , 仍然对应 "Sunday"

a : 代表上午还是下午,如果是上午就对应 “AM” , 如果是下午就对应 “PM”

其中的分隔符"-"可以替换成其他非字母的任意字符(也可以是汉字),例如:

部分修改:
在这里插入图片描述
运行结果:
在这里插入图片描述

JAVA多线程中SimpleDateFormat不安全的解决方案

不安全代码测试:

package cn.thread.first.unsafe;import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;class SimpleDome extends Thread {private SimpleDateFormat simpleDateFormat;private String str;public SimpleDome(SimpleDateFormat sf, String str) {this.simpleDateFormat = sf;this.str = str;}@Overridepublic void run() {try {Date date = simpleDateFormat.parse(str);System.out.println(str + "=" + date);} catch (ParseException e) {e.printStackTrace();}}}public class SimpleDateFormatDome {public static void main(String args[]) {//第一种:输出结果对不上SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");String[] strs = {"2017-01-10", "2017-01-11", "2017-01-12", "2017-01-13", "2017-01-14", "2017-01-15"};SimpleDome[] domes = new SimpleDome[6];for (int i = 0; i < 6; i++) {domes[i] = new SimpleDome(simpleDateFormat, strs[i]);}for (int i = 0; i < 6; i++) {domes[i].start();} }//第二种:报各种异常final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");new Thread(new Runnable() {public void run() {String str = "2017-01-10";Date date = new SimpleDome(simpleDateFormat2, str).stToDate();System.out.println(str + "=" + date);}}).start();new Thread(new Runnable() {public void run() {String str = "2017-01-11";Date date = new SimpleDome(simpleDateFormat2,str).stToDate();System.out.println(str + "=" + date);}}).start();

第一种输出结果:
异常一:Exception in thread “Thread-3” java.lang.NumberFormatException: For input string: “20172017E4”
异常二:Exception in thread “Thread-2” java.lang.NumberFormatException: multiple points
异常三:转换后的日期有时能对上,有时不能对上。

第二种:报各种异常
异常一:Exception in thread “Thread-3” java.lang.NumberFormatException: For input string: “20172017E4”
异常二:Exception in thread “Thread-2” java.lang.NumberFormatException: multiple points
异常三:Exception in thread “Thread-3” java.lang.NumberFormatException: For input string: “”

解决方案一

1.在转换的过程中加入锁:

 private static final Object object=newe Object();@Overridepublic void run() {synchronized(object){try {Date date = simpleDateFormat.parse(str);System.out.println(str + "=" + date);} catch (ParseException e) {e.printStackTrace();}}}

这种方式在高并发下面,会导致大量线程阻塞,严重影响性能,一般不建议这样使用。

解决方案二

class SimpleDome 
...  private SimpleDateFormat simpleDateFormat= new   SimpleDateFormat("yyyy-MM-dd");

对于每个线程来说都是一个新的simpleDateFormat对象,不存在资源竞争。这种方式的缺陷是,同样访问量大时,会创建大量的simpleDateFormat对象,并且转换后就丢去,短暂的占用内存,导致垃圾回收时也要费一番功夫。耗时,耗空间,更不划算。

现在比较流行的解决方案三


package cn.thread.first.unsafe;import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;class SimpleHelp {private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {@Overrideprotected DateFormat initialValue() {return new SimpleDateFormat("yyyy-MM-dd");}};//写法一,和上面的初始化一起哦!public static Date convert(String source) {try {Thread.sleep(100);return df.get().parse(source);} catch (Exception e) {e.printStackTrace();}return null;}//写法二 getDateFormat()和convert2()public static DateFormat getDateFormat() {DateFormat dateFormat = df.get();if (dateFormat == null) {dateFormat = new SimpleDateFormat("yyyy-MM-dd");df.set(dateFormat);}return dateFormat;}public static Date convert2(String source) {try {Thread.sleep(100);return getDateFormat().parse(source);} catch (Exception e) {e.printStackTrace();}return null;}}public class SimpleDateFormatDome {public static void main(String args[]) {//下面是线程安全的new Thread(new Runnable() {public void run() {String str = "2017-01-12";Date date = SimpleHelp.convert(str);System.out.println(str + "=" + date);}}).start();new Thread(new Runnable() {public void run() {String str = "2017-01-13";Date date = SimpleHelp.convert(str);System.out.println(str + "=" + date);}}).start();new Thread(new Runnable() {public void run() {String str = "2017-01-14";Date date = SimpleHelp.convert(str);System.out.println(str + "=" + date);}}).start();}
}

正常输出结果。
说明:使用ThreadLocal对与每个线程来说都会存一个副本出来,每个线程拥有自己的ThreadLocal-> DateFormat,等于每个线程各自拥有各子的DateFormat。

ThreadLocal在《java多线程-线程间通讯(七)》中也有讲过。这里再讲一下,说明一下网上很多人认为ThreadLocal有多牛,其实也没你想象中的牛。

package cn.thread.first.threadlocal;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;class ClassDome {}class UserTask2 implements Runnable {private static ThreadLocal<ClassDome> startDate = new ThreadLocal<ClassDome>();public ClassDome getClassDome() {ClassDome dome = startDate.get();if (dome == null) {System.out.println("我被实例化了");dome = new ClassDome();startDate.set(dome);}return dome;}public void run() {try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}ClassDome classDome = new ClassDome();System.out.println("localEnd+" + Thread.currentThread().getId() + "==" + getClassDome().getClass().hashCode());System.out.println("1111+" + Thread.currentThread().getId() + "==" + classDome.hashCode());startDate.remove();//最好显示情况ThreadLocal,不然容易导致内存溢出}
}public class ThreadLocalDemo2 {public static void main(String[] args) {UserTask2 task = new UserTask2();ExecutorService ex = Executors.newCachedThreadPool();for (int i = 0; i < 3; i++) {ex.execute(task);try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}}ex.shutdown();}
}

输出结果:
我被实例化了
localEnd+10==1443517032
1111+10==1489876697
我被实例化了
localEnd+11==1443517032
1111+11==1514623189
我被实例化了
localEnd+10==1443517032
1111+10==502084774

看到了吗,三个线程,被实例化了三次,也就是说三个线程,创建了三个new ClassDome();那和上面说的第二种方式都就变成一样的了嘛。
说实话,就是一样的。那为什么还要用ThreadLocal?如果一个线程里面有多个日期需要转换时,这个时候就拥有只会有一个哦!假如一个线程处理10条数据的格式转换,这样是对于ThreadLocal方式来说,一个线程里面只创建了一个SimpleDateFormat对象,而用第二种方式则会创建10个SimpleDateFormat对象。把日期转换写成一个工具类,不要继承Thread,就知道ThreadLocal的好处了。
如:
Date date=ConcurrentDateUtil.parse(“日期字符串1”);
Date date=ConcurrentDateUtil.parse(“日期字符串2”);
Date date=ConcurrentDateUtil.parse(“日期字符串3”);

SimpleDateFormat不安全的原因?

SimpleDateFormat继承了DateFormat,在DateFormat中定义了一个protected属性的 Calendar类的对象:calendar。只是因为Calendar累的概念复杂,牵扯到时区与本地化等等,Jdk的实现中使用了成员变量来传递参数,这就造成在多线程的时候会出现错误。

字符串日期转换代码:
simpleDateFormat.parse(str);
parse源码如下:

public Date parse(String text, ParsePosition pos){checkNegativeNumberExpression();int start = pos.index;int oldStart = start;int textLength = text.length();boolean[] ambiguousYear = {false};CalendarBuilder calb = new CalendarBuilder();for (int i = 0; i < compiledPattern.length; ) {int tag = compiledPattern[i] >>> 8;int count = compiledPattern[i++] & 0xff;if (count == 255) {count = compiledPattern[i++] << 16;count |= compiledPattern[i++];}switch (tag) {case TAG_QUOTE_ASCII_CHAR:if (start >= textLength || text.charAt(start) != (char)count) {pos.index = oldStart;pos.errorIndex = start;return null;}start++;break;case TAG_QUOTE_CHARS:while (count-- > 0) {if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {pos.index = oldStart;pos.errorIndex = start;return null;}start++;}break;default:boolean obeyCount = false;boolean useFollowingMinusSignAsDelimiter = false;if (i < compiledPattern.length) {int nextTag = compiledPattern[i] >>> 8;if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||nextTag == TAG_QUOTE_CHARS)) {obeyCount = true;}if (hasFollowingMinusSign &&(nextTag == TAG_QUOTE_ASCII_CHAR ||nextTag == TAG_QUOTE_CHARS)) {int c;if (nextTag == TAG_QUOTE_ASCII_CHAR) {c = compiledPattern[i] & 0xff;} else {c = compiledPattern[i+1];}if (c == minusSign) {useFollowingMinusSignAsDelimiter = true;}}}start = subParse(text, start, tag, count, obeyCount,ambiguousYear, pos,useFollowingMinusSignAsDelimiter, calb);if (start < 0) {pos.index = oldStart;return null;}}}// At this point the fields of Calendar have been set.  Calendar// will fill in default values for missing fields when the time// is computed.pos.index = start;Date parsedDate;try {parsedDate = calb.establish(calendar).getTime();// If the year value is ambiguous,// then the two-digit year == the default start yearif (ambiguousYear[0]) {if (parsedDate.before(defaultCenturyStart)) {parsedDate = calb.addYear(100).establish(calendar).getTime();}}}// An IllegalArgumentException will be thrown by Calendar.getTime()// if any fields are out of range, e.g., MONTH == 17.catch (IllegalArgumentException e) {pos.errorIndex = start;pos.index = oldStart;return null;}return parsedDate;}

第一:Calendar是共享成员变量,而establish这个方法里面有一个clear清除操作,然后再对Calendar重新设置。
其中,calendar是DateFormat的protected字段。这条语句改变了calendar,稍后,calendar还会用到(在subFormat方法里),而这就是引发问题的根源。
在一个多线程环境下,有两个线程持有了同一个SimpleDateFormat的实例,分别调用format方法:
线程1调用format方法,改变了calendar这个字段。
中断来了。
线程2开始执行,它也改变了calendar。
又中断了。
线程1回来了,此时,calendar已然不是它所设的值,而是走上了线程2设计的道路。
稍微花点时间分析一下format的实现,我们便不难发现,用到calendar,唯一的好处,就是在调用subFormat时,少了一个参数,却带来了这许多的问题。其实,只要在这里用一个局部变量,一路传递下去,所有问题都将迎刃而解。

这个问题背后隐藏着一个更为重要的问题–无状态:无状态方法的好处之一,就是它在各种环境下,都可以安全的调用。衡量一个方法是否是有状态的,就看它是否改动了其它的东西,比如全局变量,比如实例的字段。format方法在运行过程中改动了SimpleDateFormat的calendar字段,所以,它是有状态的。

这也同时提醒我们在开发和设计系统的时候注意下一下三点:

1.自己写公用类的时候,要对多线程调用情况下的后果在注释里进行明确说明
  2.对线程环境下,对每一个共享的可变变量都要注意其线程安全性
  3.我们的类和方法在做设计的时候,要尽量设计成无状态的

参照文章:深入理解Java:SimpleDateFormat安全的时间格式化

总结一

1)使用本地DateFormat 或SimpleDateFormat 对象转换或格式化Java中的日期。使他们本地化确保它们不会在多个线程之间共享。

2)如果您在Java中为SimpleDateFormat 类共享Date ,则需要在外部进行同步调用format()和parse()方法,因为它们会改变DateFormat 对象的状态,并且可以创建微妙和难度在Java中格式化字符串或创建日期时修复错误。最好是避免共享DateFormat 类。

3)如果您有选项,请使用joda-date库 进行日期和时间相关操作。它使用Java Date API易于理解和便携,并解决与Java 中的SimpleDateFormat 相关的所有线程安全问题。

4)Java 中SimpleDateFormat的另一个好的替代方法是Apaches的commons.lang 包,它包含一个名为 FastDateFormat的实用类和线程安全替代Java中的SimpleDateFormat 的类。

5)同步DateFormat 和SimpleDateFormat的另一种方法是使用ThreadLocal,它在每个Thread基础上创建SimpleDateFormat ,但如果不仔细使用,它可能是严重内存泄漏的源头和java.lang.OutOfMemoryError,所以避免你没有任何其他选择。

总结二

SimpleDateFormat 的不安全来自于使用了一个全局变量Calendar,而这个变量在操作过程中做了clear,set操作,类似-1,+1操作,这样就导致了SimpleDateFormat在多线程下操作是不安全的。

注:多线程下只有共享了SimpleDateFormat才会出现上面的情况哦!

本文转载整合了以下两篇博文:
转自:https://www.cnblogs.com/jyiqing/p/6858224.html
转自:https://blog.csdn.net/piaoslowly/article/details/81476059
引用了:

深入理解Java:SimpleDateFormat安全的时间格式化

这篇关于转载整合:SimpleDateFormat日期格式的使用示例 和 JAVA多线程中SimpleDateFormat不安全的解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/908029

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4