我们之前讨论过时间,在Java 中有一些方法会出现横线?比如Date 过期方法。

参考文章:知识点:java一些方法会有横线?以Date 过期方法为例

Java中的日期和时间处理方法

  • Date类(官方不再推荐使用,官方解释Date类不利于国际化,推荐使用Calendar类)
  • Calendar类
  • DateFormat类 使用此类来时间初始化

我们发现,时间toLocalString 会有横线:

vo.setSubmitDate(new Date().toLocaleString());

可以改为:

vo.setSubmitDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).getTime()));

在最近的项目中,用SimpleDateFormat出现一些奇奇怪怪的BUG:

  • 1.结果值不对:转换的结果值经常和预期不同。
  • 2.内存泄漏: 由于转换的结果值不对,后续的一些操作会导致系统内存泄漏,频繁触发GC(垃圾回收),造成系统不可用。

1 什么是SimpleDateFormat

在java doc对SimpleDateFormat的解释如下:

SimpleDateFormatis a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting(date → text), parsing (text → date), and normalization.

SimpleDateFormat是一个用来对位置敏感的格式化和解析日期的实体类。他允许把日期格式化成text,把text解析成日期和规范化。

1.1 使用SimpleDateFormat

simpleDateFormat的使用方法比较简单:

public static void main(String[] args) throws Exception { 

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");

    System.out.println(simpleDateFormat.format(new Date())); 

    System.out.println(simpleDateFormat.parse("2018-10-24  12:10:24")); 

}
  • 1.定义一个日期"yyyy-mm-dd HH:mm:ss"的pattern, 也就是我们这个simpleDateFormat不管是格式化还是解析都需要按照这个pattern。
  • 2.对于format需要传递Date的对象,会返回一个String类型,这个String会按照我们上面的格式生成。
  • 3.对于parse需要传递一个按照上面pattern的字符串,如果传递错误的pattern会抛出java.text.ParseException异常,如果传递正确的会生成一个Date对象。

附:格式占位符 G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区复制代码

回到我们遇到的坑:

2 为什么SimpleDateFormat会线程不安全呢?

在SimpleDateFormat源码中,所有的格式化和解析都需要通过一个中间对象Calendar进行转换,而这将会出现线程不安全的操作

比如当多个线程操作同一个Calendar的时候后来的线程会覆盖先来线程的数据,那最后其实返回的是后来线程的数据,这样就导致我们上面所述的BUG的产生

为什么会出现这么多问题呢?

因为SimpleDateFormat线程不安全,很多人都会写个Util类,然后把SimpleDateFormat定义成全局的一个常量,所有线程都共享这个常量:

protected static final SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

public static Date formatDate(String date) throws ParseException {

    returndayFormat.parse(date);

}

3.如何避坑

对于SimpleDateFormat的解决方法有下面几种:

3.1 新建SimpleDateFormat

原因:所有线程都共用一个SimpleDateFormat,

解决办法:每次使用的时候都创建一个新的SimpleDateFormat,我们可以在DateUtils中将创建SimpleDateFormat放在方法内部:

public static Date formatDate(String date) throws ParseException { 

    SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

    return dayFormat.parse(date);

}

上面这个方法虽然能解决我们的问题但是引入了另外一个问题就是,如果这个方法使用量比较大,有可能会频繁造成Young gc,整个系统还是会受一定的影响。

3.2 使用ThreadLocal

使用ThreadLocal能避免造成Young gc,我们对每个线程都使用ThreadLocal进行保存

由于ThreadLocal是线程之间隔离开的,所以不会出现线程安全问题:

private static ThreadLocal simpleDateFormatThreadLocal = new ThreadLocal<>();

public static Date formatDate(String date) throws ParseException { 

    SimpleDateFormat dayFormat = getSimpleDateFormat();
returndayFormat.parse(date); } private static SimpleDateFormatgetSimpleDateFormat() {
SimpleDateFormat simpleDateFormat = simpleDateFormatThreadLocal.get(); if(simpleDateFormat == null){ simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss") simpleDateFormatThreadLocal.set(simpleDateFormat); } returnsimpleDateFormat; }

3.3使用第三方工具包

虽然上面的ThreadLocal能解决我们出现的问题,但是第三方工具包提供的功能更加强大,在java中有两个类库比较出名一个是Joda-Time,一个是Apache common包

3.3.1 Joda-Time(推荐)

Joda-Time 令时间和日期值变得易于管理、操作和理解。对于我们复杂的操作都可以使用Joda-Time操作,下面我列举两个例子,对于把日期加上90天,如果使用原生的Jdk我们需要这样写:

Calendar calendar = Calendar.getInstance();calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);SimpleDateFormat sdf = new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS");

calendar.add(Calendar.DAY\_OF\_MONTH, 90);

System.out.println(sdf.format(calendar.getTime()));

但是在我们的joda-time中只需要两句话,并且api也比较通俗易懂,所以你为什么不用Joda-Time呢?

DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);

System.out.println(dateTime.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS");
3.3.2 common-lang包

在common-lang包中有个类叫FastDateFormat,由于common-lang这个包基本被很多Java项目都会引用,所以你可以不用专门去引用处理时间包,即可处理时间,在FastDateFormat中每次处理时间的时候会创建一个calendar,使用方法比较简单代码如下所示:

FastDateFormat.getInstance().format(new Date());

3.4升级jdk8(推荐)

在java8中Date这个类中的很多方法包括构造方法都被打上了@Deprecated废弃的注解,取而代之的是LocalDateTime,

LocalDate LocalTime这三个类:

  • LocalDate无法包含时间;
  • LocalTime无法包含日期;
  • LocalDateTime才能同时包含日期和时间。

知识点:Java8 在日期的格式化和解析方面不用考虑线程安全性

代码如下:

public static String formatTime(LocalDateTime time,String pattern) {

returntime.format(DateTimeFormatter.ofPattern(pattern)); 

}

localDateTime不仅解决了线程安全的问题,同样也提供了一些其他的运算比如加减天数:

//日期加上一个数,根据field不同加不同值,field为ChronoUnit.* 

public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {

    return time.plus(number, field); 

}

//日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.* 

public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){

    return time.minus(number,field); 

}
延伸

使用LocalDateTime 会改变现有的代码,我们可以将他们两进行互转:

//Date转换为LocalDateTime 

public static LocalDateTime convertDateToLDT(Date date) {

    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); 

}

//LocalDateTime转换为Date 

public static Date convertLDTToDate(LocalDateTime time) {

    return Date.from(time.atZone(ZoneId.systemDefault()).toInstant()); 

}

【公众号】:一只阿木木

填坑:Java 中的日期转换的更多相关文章

  1. java中的日期转换

    在java中有两种Date对象,一种是java.sql.Date,另一种是java.util.Date 一.java.sql.Date对象: 这种Date对象使用了进行数据库操作的,它对应了数据库中的 ...

  2. Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  3. Delphi与Java中的日期互换

    在最近做的一个项目中用到了Java和Delphi,发现它们不能正确读取对方的日期类型,如在Java中写入一个值为“2007-12-1”的日期值,通过Delphi读取却不是这个值了.通过查阅资料,发现两 ...

  4. Java中的日期操作

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  5. Java中的日期和时间

    Java中的日期和时间 Java在java.util包中提供了Date类,这个类封装了当前的日期和时间. Date类支持两种构造函数.第一个构造函数初始化对象的当前日期和时间. Date() 下面的构 ...

  6. java中的时区转换

    目录 java中的时区转换 一.时区的说明 二.时间的表示 三.时间戳 四.Date类和时间戳 五.java中的时区转换 java中的时区转换 一.时区的说明 地球表面按经线从东到西,被划成一个个区域 ...

  7. java中汉字自动转换成拼音

    java中汉字自动转换成拼音 1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path. 2.[代码]PinYinUti ...

  8. 第七节:详细讲解Java中的日期,java.util.date

    前言 大家好,给大家带来详细讲解Java中的日期,java.util.date的概述,希望你们喜欢 类Date Java.lang.Object->java.util.Date public c ...

  9. MYSQL中的日期转换

    MYSQL中的日期转换 网址: http://www.eygle.com/digest/2006/09/mysql_date_convert.html 对于每个类型拥有的值范围以及并且指定日期何时间值 ...

随机推荐

  1. datafactory5.6向mysql5.7添加大量测试数据

    1.下载安装datafactory5.6 2.下载安装mysql5.7,并创建数据库guest_test和表sign_event 3.下载安装odbc5.3 4.打开datafactory配置数据源, ...

  2. flume中sink到hdfs,文件系统频繁产生文件,文件滚动配置不起作用?

    在测试hdfs的sink,发现sink端的文件滚动配置项起不到任何作用,配置如下: a1.sinks.k1.type=hdfs a1.sinks.k1.channel=c1 a1.sinks.k1.h ...

  3. ArcMap图层属性表中添加图片

    一看标题是不是有点懵?懵就对了!刚接触到的时候我也有点懵,属性表不是都是文本啊数字啊之类的格式,怎么还可以存图片,下面就带大家来看看吧! 一.关于图层入库问题 图层进入数据库和图层以shp格式存储时, ...

  4. Mac上,Apache启动正常,却无法访问localhost和127.0.0.1

    mac系统,之前一直好好的,今天突然localhost以及127就突然打不开了.显示拒绝访问. 各种方法都试过了,不是端口占用, 不是日志文件缺失,任何情况都不是. 想了想,之前有升级过PHP从5升级 ...

  5. 使用CUPS打印服务

    目录 1. 测试环境 2 2. CUPS介绍 3 2.1 CUPS的配置文件 3 2.1.1 cupsd.conf 3 2.1.2 cups-files.conf 3 2.1.3 printcap 3 ...

  6. ssm项目整合shiro

    pom.xml <properties> <shiro.version>1.2.2</shiro.version> </properties> < ...

  7. React 组件间通信介绍

    React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍:   父组件向子 ...

  8. 《python for data analysis》第八章,绘图与可视化

    <利用python进行数据分析>一书的第8章,关于matplotlib库的使用,各小节的代码. # -*- coding:utf-8 -*-import numpy as npimport ...

  9. c# 实现 HSV 调色板

    界面相关核心代码如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private vo ...

  10. MySQL Hardware--网络测试

    使用Ping测试丢包 ## ping测试 ## -c 100表示100次 ping -c 100 192.168.1.2 输出结果: ping -c 100 192.168.1.2 PING 192. ...