Java核心技术:日期和时间新旧API对比

5cab9f71666e9e6ae28d1cd614f75d71.gif

本文作者:小虚竹,CSDN博客专家&CSDN原力计划作者,Java领域优质创作者,掘金年度人气作者,华为云专家,阿里云专家博主,51CTO专家博主。是一个乐于分享“IT圈”技术的博主。

540b2bb831d53a6fcd8621e049e2c24b.png

开篇介绍

学习Java的经典名著非常多,这本书是你一定不能错过的:由凯.霍斯特曼写的《Core Java》,中文版名为《Java核心技术》。这本书几乎出现在每个“学Java要看什么书”类似的书单里,影响了几代技术人。豆瓣的评分很高:

5e2e71f481e91af150d1dc14717c67a8.png

主题

《Java核心技术》 这本书好在哪?我们都知道技术会过时的,技术会升级的,尤其是Java技术一直在持续不断地更新。《Java核心技术》 好在与时俱进,我查了下这本书的历程,1996年出版以来,至今已经更新了11版。写一本需要投入大量的精力,升级一本书也需要大量的精力,当前书的内容已经补充了Java SE 9,10,11的新版本最新特性,这对于我来说,帮助挺大的,可以及时跟上Java前进的脚步。(本书基于Java SE 17 的中文版本也即将出版)

对我的影响

《Java核心技术》 分为两本书,卷一和卷二。卷一主要是介绍Java语言的基本概念以及程序设计的基础知识,我在这里吸收到了基础知识,对原先掌握的知识进行了查缺补漏。对我编写专栏 《Java筑基100例》 起到了指导作用。

卷二主要涉及企业级专业软件开发时需要了解的高级主题,我更喜欢卷二,因为卷二的章节大部分是相互独立的,可以直接研究自己感兴趣的章节,不用关心前后章节。学习卷二的内容,我掌握了不少新知识,例如Java 8 的流库,文件的加锁机制,文件的ZIP压缩和解压缩技术,日期和时间新旧API对比,和网络通信等。


98c0a672171ac898c16bb4dac5c621d0.png

内容分享:日期和时间新旧API对比

下面我们来分享一个知识点:日期和时间新旧API对比

Java提供了两套处理日期和时间的API

1、旧的API,放在java.util 这个包下的:比较常用的有Date和Calendar等

2、新的API是java 8新引入的,放在java.time 这个包下的:LocalDateTime,ZonedDateTime,DateTimeFormatter和Instant等

为什么会有两套日期时间API,这个是有历史原因的,旧的API是jdk刚开始就提供的,随着版本的升级,逐渐发现原先的api不满足需要,暴露了一些问题,所以在java 8 这个版本中,重新引入新API。

这两套API都要了解,为什么呢?

因为java 8 发布时间是2014年,很多之前的系统还是沿用旧的API,所以这两套API都要了解,同时还要掌握两套API相互转化的技术。

1、Date

1.1 Date类说明

Date类负责时间的表示,在计算机中,时间的表示是一个较大的概念,现有的系统基本都是利用从1970.1.1 00:00:00 到当前时间的毫秒数进行计时,这个时间称为epoch(时间戳)

package java.util;


public class Date
    implements java.io.Serializable, Cloneable, Comparable<Date>
{
 ...


      private transient long fastTime;
     ....
}

java.util.Date是java提供表示日期和时间的类,类里有个long 类型的变量fastTime,它是用来存储以毫秒表示的时间戳。

1.2 date常用的用法

import java.util.Date;
-----------------------------------------
    //获取当前时间
    Date date = new Date();
    System.out.println("获取当前时间:"+date);
    //获取时间戳
    System.out.println("获取时间戳:"+date.getTime());


    // date时间是否大于afterDate 等于也为false
    Date afterDate = new Date(date.getTime()-3600*24*1000);
    System.out.println("after:"+date.after(afterDate));
    System.out.println("after:"+date.after(date));


    // date时间是否小于afterDate 等于也为false
    Date beforeDate = new Date(date.getTime()+3600*24*1000);
    System.out.println("before:"+date.before(beforeDate));
    System.out.println("before:"+date.before(date));


    //两个日期比较
    System.out.println("compareTo:"+date.compareTo(date));
    System.out.println("compareTo:"+date.compareTo(afterDate));
    System.out.println("compareTo:"+date.compareTo(beforeDate));


    //转为字符串
    System.out.println("转为字符串:"+date.toString());
    //转为GMT时区 toGMTString() java8 中已废弃
    System.out.println("转为GMT时区:"+date.toGMTString());
    //转为本地时区 toLocaleString() java8 已废弃
    System.out.println("转为本地时区:"+date.toLocaleString());

31591e47a6264b5e4c7417686861d6b5.png

53804bb71d0bc7f0f1fecb962229566e.png

1.3 自定义时间格式-SimpleDateFormat

date的toString方法转成字符串,不是我们想要的时间格式,如果要自定义时间格式,就要使用SimpleDateFormat

//获取当前时间
    Date date = new Date();
    System.out.println("获取当前时间:"+date);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(simpleDateFormat.format(date));
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
    System.out.println(simpleDateFormat1.format(date));

ac67950fbd988d3a8c3bee1f0837f019.png

SimpleDateFormat也可以方便的将字符串转成Date

//获取当前时间
    String str = "2021-07-13 23:48:23";
    try {
      Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
      System.out.println(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }

4c667e5219cf879322ec1b323f5250f6.png

日期和时间格式化参数说明

yyyy:年
MM:月
dd:日
hh:1~12小时制(1-12)
HH:24小时制(0-23)
mm:分
ss:秒
S:毫秒
E:星期几
D:一年中的第几天
F:一月中的第几个星期(会把这个月总共过的天数除以7)
w:一年中的第几个星期
W:一月中的第几星期(会根据实际情况来算)
a:上下午标识
k:和HH差不多,表示一天24小时制(1-24)。
K:和hh差不多,表示一天12小时制(0-11)。
z:表示时区

1.4 SimpleDateFormat线程不安全原因

1.4.1SimpleDateFormat线程为什么是线程不安全的呢?

来看看SimpleDateFormat的源码,先看format方法:

// Called from Format after creating a FieldDelegate
    private StringBuffer format(Date date, StringBuffer toAppendTo,
                                FieldDelegate delegate) {
        // Convert input date to time field list
        calendar.setTime(date);
...
    }

问题就出在成员变量calendar,如果在使用SimpleDateFormat时,用static定义,那SimpleDateFormat变成了共享变量。那SimpleDateFormat中的calendar就可以被多个线程访问到。

SimpleDateFormat的parse方法也是线程不安全的:

public Date parse(String text, ParsePosition pos)
    {
     ...
         Date parsedDate;
        try {
            parsedDate = calb.establish(calendar).getTime();
            // If the year value is ambiguous,
            // then the two-digit year == the default start year
            if (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;  
 }

由源码可知,最后是调用**parsedDate = calb.establish(calendar).getTime();**获取返回值。方法的参数是calendar,calendar可以被多个线程访问到,存在线程不安全问题。

我们再来看看**calb.establish(calendar)**的源码

bc86576565c9c3a47f69a0c8323142b0.png

calb.establish(calendar)方法先后调用了cal.clear()和cal.set(),先清理值,再设值。但是这两个操作并不是原子性的,也没有线程安全机制来保证,导致多线程并发时,可能会引起cal的值出现问题了。

1.4.2 验证SimpleDateFormat线程不安全

public class SimpleDateFormatDemoTest {


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


    public static void main(String[] args) {
        //1、创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);
        //2、为线程池分配任务
        ThreadPoolTest threadPoolTest = new ThreadPoolTest();
        for (int i = 0; i < 10; i++) {
            pool.submit(threadPoolTest);
        }
        //3、关闭线程池
        pool.shutdown();
    }


    static class  ThreadPoolTest implements Runnable{


        @Override
        public void run() {
        String dateString = simpleDateFormat.format(new Date());
        try {
          Date parseDate = simpleDateFormat.parse(dateString);
          String dateString2 = simpleDateFormat.format(parseDate);
          System.out.println(Thread.currentThread().getName()+" 线程是否安全: "+dateString.equals(dateString2));
        } catch (Exception e) {
          System.out.println(Thread.currentThread().getName()+" 格式化失败 ");
        }
        }
    }
}

453acabd5220d5a8a8e07fc61a151142.png

出现了两次false,说明线程是不安全的。而且还抛异常,这个就严重了。

2、LocalDateTime

2.1 LocalDateTime类说明

表示当前日期时间,相当于:yyyy-MM-ddTHH:mm:ss

2.2 LocalDateTime常用的用法

2.2.1获取当前日期和时间

LocalDate d = LocalDate.now(); // 当前日期
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
System.out.println(d); // 严格按照ISO 8601格式打印
System.out.println(t); // 严格按照ISO 8601格式打印
System.out.println(dt); // 严格按照ISO 8601格式打印

由运行结果可行,本地日期时间通过now()获取到的总是以当前默认时区返回的

2.2.2获取指定日期和时间

LocalDate d2 = LocalDate.of(2021, 07, 14); // 2021-07-14, 注意07=07月
LocalTime t2 = LocalTime.of(13, 14, 20); // 13:14:20
LocalDateTime dt2 = LocalDateTime.of(2021, 07, 14, 13, 14, 20);
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
System.out.println("指定日期时间:"+dt2);
System.out.println("指定日期时间:"+dt3);

5cb2aec7cce041c0a1741e927f1f0177.png

2.2.3 日期时间的加减法及修改

LocalDateTime currentTime = LocalDateTime.now(); // 当前日期和时间
System.out.println("------------------时间的加减法及修改-----------------------");
//3.LocalDateTime的加减法包含了LocalDate和LocalTime的所有加减,上面说过,这里就只做简单介绍
System.out.println("3.当前时间:" + currentTime);
System.out.println("3.当前时间加5年:" + currentTime.plusYears(5));
System.out.println("3.当前时间加2个月:" + currentTime.plusMonths(2));
System.out.println("3.当前时间减2天:" + currentTime.minusDays(2));
System.out.println("3.当前时间减5个小时:" + currentTime.minusHours(5));
System.out.println("3.当前时间加5分钟:" + currentTime.plusMinutes(5));
System.out.println("3.当前时间加20秒:" + currentTime.plusSeconds(20));
//还可以灵活运用比如:向后加一年,向前减一天,向后加2个小时,向前减5分钟,可以进行连写
System.out.println("3.同时修改(向后加一年,向前减一天,向后加2个小时,向前减5分钟):" + currentTime.plusYears(1).minusDays(1).plusHours(2).minusMinutes(5));
System.out.println("3.修改年为2025年:" + currentTime.withYear(2025));
System.out.println("3.修改月为12月:" + currentTime.withMonth(12));
System.out.println("3.修改日为27日:" + currentTime.withDayOfMonth(27));
System.out.println("3.修改小时为12:" + currentTime.withHour(12));
System.out.println("3.修改分钟为12:" + currentTime.withMinute(12));
System.out.println("3.修改秒为12:" + currentTime.withSecond(12));

bad044e24fad4024f1b5234b5e8f5e07.png

2.3 线程安全

网上大家都在说Java 8提供的LocalDateTime是线程安全的,但是它是如何实现的呢,今天让我们来挖一挖:

public final class LocalDateTime
        implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
        ... 
        }

由上面的源码可知,LocalDateTime是不可变类。我们都知道一个Java并发编程规则:不可变对象永远是线程安全的。

对比下Date的源码 ,Date是可变类,所以是线程不安全的。

public class Date
    implements java.io.Serializable, Cloneable, Comparable<Date>
{
...
}

3、LocalDateTime和Date相互转化

3.1 Date转LocalDateTime

System.out.println("------------------方法一:分步写-----------------------");
//实例化一个时间对象
Date date = new Date();
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = date.toInstant();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
//转化为LocalDateTime
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println("方法一:原Date = " + date);
System.out.println("方法一:转化后的LocalDateTime = " + localDateTime);


System.out.println("------------------方法二:一步到位(推荐使用)-----------------------");
//实例化一个时间对象
Date todayDate = new Date();
//Instant.ofEpochMilli(long l)使用1970-01-01T00:00:00Z的纪元中的毫秒来获取Instant的实例
LocalDateTime ldt = Instant.ofEpochMilli(todayDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("方法二:原Date = " + todayDate);
System.out.println("方法二:转化后的LocalDateTime = " + ldt);

77dbaf03f650b052ead089f4466ce3c4.png

3.2 LocalDateTime转Date

System.out.println("------------------方法一:分步写-----------------------");
//获取LocalDateTime对象,当前时间
LocalDateTime localDateTime = LocalDateTime.now();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = zonedDateTime.toInstant();
//转化为Date
Date date = Date.from(instant);
System.out.println("方法一:原LocalDateTime = " + localDateTime);
System.out.println("方法一:转化后的Date = " + date);




System.out.println("------------------方法二:一步到位(推荐使用)-----------------------");
//实例化一个LocalDateTime对象
LocalDateTime now = LocalDateTime.now();
//转化为date
Date dateResult = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("方法二:原LocalDateTime = " + now);
System.out.println("方法二:转化后的Date = " + dateResult);

52783b969dfcdc780bb1494a96aca607.png

四、DateTimeFormatter

4.1DateTimeFormatter类说明

DateTimeFormatter的作用是进行格式化显示,且DateTimeFormatter是不可变类且是线程安全的。

public final class DateTimeFormatter {
...
}

说到时间的格式化显示,就要说老朋友SimpleDateFormat了,之前格式化Date就要用上。但是我们知道SimpleDateFormat是线程不安全的,文前有介绍。

4.2 DateTimeFormatter常用的用法

ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
System.out.println(formatter.format(zonedDateTime));


DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
System.out.println(usFormatter.format(zonedDateTime));


DateTimeFormatter chinaFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
System.out.println(chinaFormatter.format(zonedDateTime));

2c2b130ee2c99b635ec251fa606f5915.png

总结

《Java核心技术》 它不仅仅是一本书,还是学习Java语言的进阶路径指导。它高层建瓯地介绍了Java语言的核心概念、语法、重要特性,又对Java语言基础知识进行了专业级详解。既照顾了新手易入门,易上手,也照顾到了中高级Java开发人员需要了解的高级主题。

书也要与时俱进,刚好响应了技术更新换代的市场需求。对于开发人员来说,学习有用的新技术是必要的。选择大于努力,一本优秀的书籍可以让我们不走弯路,而 《Java核心技术》 正是我们值得花时间去学习研究的一本好书。

f5a4e3219aae443089d20bee4d0454c9.png

711263edc979777120bd6efd69724f8c.png

0227bd146f5b87332100cc530cf5199d.gif

更多精彩回顾

书讯 | 5月书讯(上)|  元宇宙、因果推断、薛定谔方程...你关注的都在这

书讯 | 5月书讯(下)|设计致物系列+少儿编程好书推荐

资讯 |为企业数字人才建粮仓:专访极客邦科技双数研究院院长付晓岩

资讯 |2022美国科学院院士名单公布:图灵奖得主、龙书作者Alfred V. Aho当选!

干货 | 为什么每一名程序员都应该学习 C++?

干货 | 如果机器翻译始终存在缺陷和错误,那它还有什么用处?

0ff005ec1c60ce77a9f3469c77779b7e.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值