java datetime和date_Java 8时间接口localDateTime和Date的对比

java8提供了新的时间接口。相对Date,Calendar,个人感觉最大的好处是对时间操作的学习成本很低,比Calendar低。

1. LocalDate,LocalTime,LocalDateTime

LocalDate 代表日期,LocalTime表示时刻,类似11:23这样的时刻。 LocalDateTime就是前面2个的结合,这个可以从java.time.LocalDateTime#toString的代码看出一二:

@Override

public String toString() {

return date.toString() + 'T' + time.toString();

}

date,time 在java.time.LocalDateTime中

/**

* The date part.

*/

private final LocalDate date;

/**

* The time part.

*/

private final LocalTime time;

实际使用中,计算日期就用LocalDate,计算日期加时刻用LocalDateTime,如果只有时刻就是LocalTime(感觉在说废话)

这三个的用法基本上一样,通过方法名就知道用法那种

1.1 获取当前时间的对象

LocalDateTime localDateTime = LocalDateTime.now();

Date date = new Date();

localDateTime相比Date更像是一个 工具 类,就是为了时间操作使用。其构造方法是私有的。

1.2 从字符串中解析

字符串 2019-01-11 解析成时间对象

String str = "2019-01-11";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

LocalDate localDate = LocalDate.parse(str, formatter);

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

try {

Date date = simpleDateFormat.parse(str);

} catch (ParseException e) {

e.printStackTrace();

}

DateTimeFormatter的包路径是java.time.format和LocalDate一样在java.time下面,而SimpleDateFormat和Date是不同的。所以当判断引入路径的时候更容易判断。

当解析失败的时候,两个异常的抛出不一样,DateTimeFormatter抛出的是DateTimeParseException,继承自RuntimeException,而ParseException明显继承的是Exception。

个人感觉这个思路是,前者如果抛出异常那就是编程上错误,而后者则是的程序代码的不稳定性。我更倾向于第一种的异常设计,应该加强对入参的检测判断,而不是通过捕获异常去处理入参的错误。(类似NumberFormatException)

1.3 LocalDate比Date更强的初始化时间

Date 设置某个日期,基本上3个方式,时间戳/Calendar/字符串解析。相对的LocalDate就简单了很多

LocalDate.of(2019,1,12);

其他的也一样

83b59ebd4d5a73ac8f54104da469a14a.png

1.4 时间戳的转换

在这里时间戳的转换不如Date直接。主要因为LocalDate本身是没有时区的。

时间戳传LocalDateTime

long timestamp = System.currentTimeMillis();

Instant instant = Instant.ofEpochMilli(timestamp);

LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

LocalDateTime转时间戳

LocalDateTime dateTime = LocalDateTime.now();

dateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

dateTime.toInstant(ZoneOffset.of("+08:00")).toEpochMilli();

dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

关于时区的计算也很简单,就是相差几个小时就加上多少秒

57bd4ce8f87d78a2169c91b2e6538aed.png

有些时区计算的时候,不妨自己加时间也一样,elasticsearch+logstash设置@timestamp时间是默认UTC Z的时间,和咱们差了8个小时

LocalDateTime.parse(json.getString("@timestamp"), DateTimeFormatter.ISO_DATE_TIME).plusHours(8L)

1.5 和Date互转

import java.time.Instant;

import java.util.Date;

public class Main {

public static void main(String[] args) {

Date dt = new Date();

System.out.println("Date: " + dt);

Instant in = dt.toInstant();

System.out.println("Instant: " + in);

Date dt2 = Date.from(in);

System.out.println("Date: " + dt2);

}

}

Instant 和 LocalDate或LocalDateTime 就不赘述了...

1.6 更好的理解和操作方式

Date、Calendar的操作,例如设置月份,day of week 都有些让人迷惑,例如1月的定义是0,周一是0。1号好像也是0吧(我真没咋用过这东西,现用现百度...

LocalDate感觉好多了。例如DayOfWeek是枚举类型。使用枚举就不会理解错了吧

ab9beadae59f6b8d140c4a217aec1cd9.png

很多日期和时间操作,无非就是加减时间和比较.

使用‘加’的示例:

af91b335af87e8e5efd10abfb5e1d8cc.png

不用再去使用一个不熟悉的Calendar去操作了(Calendar提供的接口都是啥玩意,get,set的)

Calendar cal = Calendar.getInstance();

cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1)

线程安全性比较

LocalDate...系列是线程安全的 额..每一个字段都用了final关键字了,都变不了... 所以进行操作后都是返回新的copy对象

fa9f6bb88b288d703927feffd57d5a4d.png

至于说Date线程不安全,get,set的肯定在多线程的时候容易出现问题,不过set方法已经都@Deprecated废弃了。当然不是因为线程安全问题废弃的,是因为有了更好的替代

Calendar.set(Calendar.DAY_OF_MONTH, int date)

不过感觉还是不如这个更清晰明了

LocalDate.of(2019,1,12);

2.1 SimpleDateFormat的线程安全性

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

在一定负载情况下,SimpleDateFormat会出问题的。简单测试一下

package open.note;

import java.text.SimpleDateFormat;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.util.Date;

import java.util.concurrent.CountDownLatch;

import java.util.function.Consumer;

public class UnSafeTest {

private static String time = "2019-01-11 11:11:11";

private static long timestamp = 1547176271000L;

private static LocalDateTime dateTime = LocalDateTime.of(2019,1,11,11,11,11);

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

private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public static void main(String[] args) {

dateFormatTest((obj)->{

try {

Date date = dateFormat.parse(time);

if (date.getTime() != timestamp){

System.out.println(date);

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

});

System.out.println("---------------");

dateFormatTest((obj)->{

try {

LocalDateTime dateTime = LocalDateTime.parse(time,formatter);

if (!dateTime.isEqual(UnSafeTest.dateTime)){

System.out.println(dateTime);

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

});

}

private static void dateFormatTest(Consumer runnable){

CountDownLatch countDownLatch = new CountDownLatch(1000);

for (int i = 0; i < 1000; i++) {

new Thread(()->{

runnable.accept(null);

countDownLatch.countDown();

}).start();

}

try {

countDownLatch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

输出结果

multiple points

multiple points

empty String

Sat Jan 11 11:11:11 CST 111

Fri Jan 04 11:11:11 CST 2019

For input string: ""

Mon Dec 31 11:11:11 CST 2018

Mon Dec 31 11:11:11 CST 2018

For input string: ""

Tue Jan 11 11:11:11 CST 42101

测试过程中,SimpleDateFormat 1000个线程里,有5次,时间解析错了,5次异常了(时间错了,比抛出异常还可怕)

DateTimeFormatter只是对比参考一下,未出现异常(人家已经声明是线程安全了...)

当然SimpleDateFormat线程不安全应该人尽皆知的,但依然有不安全的使用,但每次使用都new一个实例,当负载大的时候也不好。所以一个线程一个SimpleDateFormat实例应该可以的。

最后

java8 对时间操作的类还有很多 到java.time包下去看看,以后总会用得到的地方。

Instant:时间戳

Duration:持续时间,时间差

LocalDate:只包含日期,比如:2016-10-20

LocalTime:只包含时间,比如:23:12:10

LocalDateTime:包含日期和时间,比如:2016-10-20 23:14:21

Period:时间段

ZoneOffset:时区偏移量,比如:+8:00

ZonedDateTime:带时区的时间

Clock:时钟,比如获取目前美国纽约的时间。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值