第二节.日期时间API 一.JDK8之前日期时间API 1.1 java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差. 此方法适于计算时间差. @Test public void test1() { // 获取当前时间; // 返回的是距离1970年的一个long型的数[毫秒数],这种毫秒数通常称为时间戳,通常用于生成订单 long timeMi…
通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决了SimpleDateFormat线程安全问题.下面是关于DateTimeFormatter的使用实例,包括常用日期格式大全, 以2020-01-01 00:00:00为例. package com.xkzhangsan.time.test; import java.time.LocalDateTi…
通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneId. ZonedDateTime是用来处理时区相关的时间,它的各种计算都离不开ZoneId.先看ZoneId. 1. ZoneId 为时区ID,比如Europe/Paris,表示欧洲巴黎时区 1.1 时区相关知识,时区,UTC时间,GMT时间,Unix时间戳 时区 地球自西向东旋转,东边比西边先看到…
目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总结 0.前言 通过前面Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中主要的类关系简图如下: 可以看出主要的LocalDate, LocalTime, LocalDateTime, Instant都是实现相同的接口,这里以LocalDate为例分析jav…
从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转Date.下面是时间类互相转换大全,包含Instant.LocalDate.LocalDateTime.LocalTime.ZonedDateTime和Date的相互转换,时间转换大全,下面是一个工具类,仅供参考: 具体包含: LocalDateTime转Date,LocalDate转Date,Loc…
通过Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的优点,java8具有很多优点,现在网上查到的农历转换工具类都是基于jdk7及以前的类写的,下面使用java新的日期时间API重写农历LunarDate. package com.xkzhangsan.time; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import ja…
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了一个规范,他本人作为规范的领导人,该规范就是JSR 310,在Java8中实现并发布. 1.Java8日期.时间API包介绍 java.time包:这是新的Java日期/时间API的基础包,所有的主要基础类都是这个包的一部分,如:LocalDate, LocalTime, LocalDateTime…
1.不变性 新的日期/时间API中,所有的类都是不可变的,这对多线程环境有好处. 比如:LocalDateTime 2.关注点分离 新的API将人可读的日期时间和机器时间(unix timestamp)明确分离,它为日期(Date).时间(Time).日期时间(DateTime).时间戳(unix timestamp)以及时区定义了不同的类. 不同时间分解成了各个类,比如:LocalDate, LocalTime, LocalDateTime, Instant,Year,Month,YearMo…
1.DateTimeFormatter final修饰,线程安全,用于打印和解析日期-时间对象的格式化程序. 创建DateTimeFormatter: DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); 2.格式化日期 例如:LocalDateTime LocalDateTime localDa…
通过工作之余,对Java8中java.time包源码的不断学习,使用和总结,开发了xk-time,初步完成,欢迎试用和提出建议! xk-time xk-time is a datetime converter calculator and formatter tool set, based on java8 date and time API, thread safe, easy to use. 时间转换,计算,格式化,解析的工具,使用java8,线程安全,简单易用,多达20几种常用日期格式化模…