如何在java8中使用time包

发布时间:2021-06-09 17:59:57 作者:Leah
来源:亿速云 阅读:190

这篇文章给大家介绍如何在java8中使用time包,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

新的设计思路

jdk1.8包目录简介:

如何在java8中使用time包

如何在java8中使用time包

如何在java8中使用time包

如何在java8中使用time包

如何在java8中使用time包

围绕常用点和大多数人经常用到做些用例

计算、格式化、字符串转换这三快可以说是很基础的功能,大多数人主要围绕这几块进行开发和封装,基础掌握后面的高级用法慢慢就可以积累。一般使用都是围绕time基础包中LocalDate\LocalDateTime\LocalTime类展开,先介绍一下这几个类及相关的类。

LocalDate: 一个ISO-8601日历系统下的data对象,如2007-12-03 LocalDateTime: 一个ISO-8601日历系统下的data-time对象,如2007-12-03T10:15:30 LocalTime: 一个ISO-8601日历系统下的time对象,如10:15:30 Instant: 一个瞬时的时间点值对象,从1970-01-01T00:00:00Z点毫秒计算的 Clock: 基于instant生成的时钟对象,遵守UTC时区规则可以去生成date和time Duration: 一个time范围值对象,单位也可以是分钟或小时 Period: 一个date范围值对象,单位可以是年、月、日,和duration正好是两个粒度对象 OffsetDateTime: 从UTC/Greenwich格式时间偏移成ISO-8601的date-time对象,如2007-12-03T10:15:30+01:00 OffSetTime: 和OffsetDateTime类似,粒度是到time的UTC时间格式对象,如10:15:30+01:00 ZonedDateTime: 带时区的ISO-8601日历系统的date-time对象,如2007-12-03T10:15:30+01:00 Europe/Paris ZonedOffset: 一个带时区的从Greenwich/UTC的偏移量范围中对象,如+02:00

网上找的新旧类的比较图挺好的,贴在这里:

如何在java8中使用time包

时间加减计算

LocalDateTime dateTime = LocalDateTime.now(Clock.system(ZoneId.systemDefault()));
LocalDateTime datetime2 =dateTime.minusDays(2);

printTest(dateTime);
printTest(datetime2);
printTest(datetime2.plusHours(3));
printTest(datetime2.minusWeeks(1));
printTest(datetime2.plus(1,ChronoUnit.MONTHS));

printTest(datetime2.compareTo(dateTime));
printTest(datetime2.withYear(2));
printTest(datetime2.isBefore(dateTime));

Duration duration = Duration.ofDays(5);
printTest(duration);
printTest(duration.plusHours(2).toMinutes());

结果:

1 : 2019-01-29 11:01:49
2 : 2019-01-27 11:01:49
3 : 2019-01-27 14:01:49
4 : 2019-01-20 11:01:49
5 : 2019-02-27 11:01:49
6 : -2
7 : 0002-01-27 11:01:49
8 : true
9 : PT120H
10 : 7320

时间输出格式化

LocalDateTime dateTime = LocalDateTime.now(Clock.systemDefaultZone());

printTest(dateTime);
printTest(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
printTest(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
printTest(dateTime.format(DateTimeFormatter.ISO_WEEK_DATE));
printTest(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH??ss.SSS")));

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendPattern("yyyy-MM-dd");
builder.parseStrict().toFormatter();
printTest(dateTime.format(builder.parseStrict().toFormatter()));

结果:

1 : 2019-01-29 11:14:07
2 : 20190129
3 : 2019-01-29T11:14:07.232
4 : 2019-W05-2
5 : 2019-01-29 11:14:07.232
6 : 2019-01-29

时间对象和string相互转换

LocalDateTime dateTime = LocalDateTime.now(Clock.system(ZoneId.systemDefault()));
printTest(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE));
printTest(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
printTest(dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME));

printTest(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
printTest(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH??ss")));
printTest(dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));
printTest(dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
printTest(dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.valueOf("MEDIUM"))));

printTest(LocalDateTime.parse("2019-12-03T10:15:30").toString());
printTest(LocalDate.parse("2019-12-03",DateTimeFormatter.ISO_LOCAL_DATE));
printTest(LocalTime.parse("10:15:30",DateTimeFormatter.ISO_LOCAL_TIME));

结果:

1 : 2019-01-29
2 : 2019-01-29T10:35:38.508
3 : 10:35:38.508
4 : 20190129
5 : 2019-01-29 10:35:38
6 : 19-1-29 上午10:35
7 : 2019-1-29 10:35:38
8 : 2019-1-29 10:35:38
9 : 2019-12-03T10:15:30
10 : 2019-12-03
11 : 10:15:30

新API和就Date转换策略

LocalDateTime localDateTime = LocalDateTime.now();
localDateTime.minusHours(2);
printTest(localDateTime);
Date localDateTime2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
printTest(localDateTime2.toString());

LocalDate localDate = LocalDate.now();
printTest(localDate);
Date localDate2 = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
printTest(localDate2);

Date date = new Date();
printTest(date);
LocalDateTime date2 = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
printTest(date2);

LocalTime localTime = LocalDateTime.ofInstant(new Date().toInstant(),ZoneId.systemDefault()).toLocalTime();
printTest(localTime);

结果:

1 : 2019-01-29 13:06:58
2 : Tue Jan 29 13:06:58 CST 2019
3 : 2019-01-29
4 : Tue Jan 29 00:00:00 CST 2019
5 : Tue Jan 29 13:06:58 CST 2019
6 : 2019-01-29 13:06:58
7 : 13:06:58.343

关于如何在java8中使用time包就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. golang中time包的使用方法
  2. 如何在golang中使用time包

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java8 time

上一篇:Java中内存区域和内存模型有什么区别

下一篇:Zookeeper和Eureka的优缺点对比分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》