未读代码 未读代码
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
首页
  • Java 18 新功能介绍
  • Java 17 新功能介绍
  • Java 16 新功能介绍
  • Java 15 新功能介绍
  • Java 14 新功能介绍
  • Java 8 新特性

    • Java 8 Lambda 表达式
    • Java 8 Stream 流式操作
    • Java 8 时间处理介绍
    • Java 8 Optional 介绍
  • Java 开发工具
Java 源码分析
Spring Boot 系列
  • Arthas 问题定位
  • JMH 基准测试
GitHub (opens new window)
  • Java 新特性

  • Java8 新特性

    • Java 8 Lambda 和 Comparator 排序
    • Java 8 Optional 介绍
    • Java 8 Lambda 表达式介绍
    • Java 8 Stream 流式操作
    • Java 8 LocalDate、LocalDateTime 时间处理介绍
      • 1. 时间处理类
      • 2. 时间获取
      • 3. 时间创建
      • 4. 时间转换
      • 5. 时间格式化
      • 6. 时间比较
      • 7. 时间加减
      • 8. 时间扩展方法
    • Java 8 List 转 Map
    • Java 8 Function 函数接口
    • Java 8 Supplier 函数接口
    • Java 8 Consumer 函数接口
    • Java 8 Predicate 函数接口
    • Java 8 forEach 遍历
    • Java 8 BiFunction 函数接口
    • Java 8 BiPredicate 函数接口
    • Java 8 UnaryOperator 函数接口
  • Java 新特性
  • Java8 新特性
程序猿阿朗
2019-10-24

Java 8 LocalDate、LocalDateTime 时间处理介绍

当前时间:2019年10月24日。距离 JDK 14 发布时间(2020年3月17日)还有多少天?

// 距离JDK 14 发布还有多少天?
LocalDate jdk14 = LocalDate.of(2020, 3, 17);
LocalDate nowDate = LocalDate.now();
System.out.println("距离JDK 14 发布还有:"+nowDate.until(jdk14,ChronoUnit.DAYS)+"天");
1
2
3
4

JDK 8 已经在 2014年 3月 18日正式可用 ,距离现在已经 5年多时间过去了。5年时间里很多企业也都换上了 JDK 8,明年 3月份 Jdk14 也要来了,那么 Jdk 8 的新特性你真的用起来了吗?我准备写一个 Jdk 8开始的新特性介绍以及使用的系列文章,后续 Jdk 也会继续更新,你如果需要的话不妨关注下博客或者公众号。

# 1. 时间处理类

Jdk8 带来了全新的时间处理工具类,用于代替之前存在缺陷的时间处理。新的时间处理相比之前更加简单好用。

Jdk8 时间处理类

常用的类有以下几个类。

时间相关类 介绍
LocalDateTime 时间处理类,最高精确到纳秒
LocalDate 时间处理类,最高精确到天
DateTimeFormatter 时间格式化
ZoneId 时区设置类

# 2. 时间获取

使用不同的类可以获取不同精度的时间。

/**
 * 时间获取
*/
@Test
public void nowTimeTest() {
    // 当前精确时间
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前精确时间:" + now);
    System.out.println("当前精确时间:" + now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + " " + now.getHour() + "-" + now.getMinute() + "-" + now.getSecond());

    // 获取当前日期
    LocalDate localDate = LocalDate.now();
    System.out.println("当前日期:" + localDate);
    System.out.println("当前日期:" + localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());

    // 获取当天时间
    LocalTime localTime = LocalTime.now();
    System.out.println("当天时间:" + localTime);
    System.out.println("当天时间:" + localTime.getHour() + ":" + localTime.getMinute() + ":" + localTime.getSecond());

    // 有时区的当前精确时间
    ZonedDateTime nowZone = LocalDateTime.now().atZone(ZoneId.systemDefault());
    System.out.println("当前精确时间(有时区):" + nowZone);
    System.out.println("当前精确时间(有时区):" + nowZone.getYear() + "-" + nowZone.getMonthValue() + "-" + nowZone.getDayOfMonth() + " " + nowZone.getHour() + "-" + nowZone.getMinute() + "-" + nowZone.getSecond());
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

获取到的时间:

当前精确时间:2019-10-24T00:26:41.724
当前精确时间:2019-10-24 0-26-41
当前日期:2019-10-24
当前日期:2019-10-24
当前精确时间(有时区):2019-10-24T00:26:41.725+08:00[GMT+08:00]
当前精确时间(有时区):2019-10-24 0-26-41
当天时间:00:26:41.725
当天时间:0:26:41
1
2
3
4
5
6
7
8

# 3. 时间创建

可以指定年月日时分秒创建一个时间类,也可以使用字符串直接转换成时间。

/**
 * 时间创建
 */
@Test
public void createTime() {
    LocalDateTime ofTime = LocalDateTime.of(2019, 10, 1, 8, 8, 8);
    System.out.println("当前精确时间:" + ofTime);

    LocalDate localDate = LocalDate.of(2019, 10, 01);
    System.out.println("当前日期:" + localDate);

    LocalTime localTime = LocalTime.of(12, 01, 01);
    System.out.println("当天时间:" + localTime);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

创建的时间:

当前精确时间:2019-10-01T08:08:08
当前日期:2019-10-01
当天时间:12:01:01
1
2
3

# 4. 时间转换

/**
* 日期转换
*/
@Test
public void convertTimeTest() {
    LocalDateTime parseTime = LocalDateTime.parse("2019-10-01T22:22:22.222");
    System.out.println("字符串时间转换:" + parseTime);

    LocalDate formatted = LocalDate.parse("20190101", DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println("字符串时间转换-指定格式:" + formatted);

    // Date 转换成 LocalDateTime
    Date date = new Date();
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println("Date 转换成 LocalDateTime:" + LocalDateTime.ofInstant(date.toInstant(), zoneId));

    // LocalDateTime 转换成 Date
    LocalDateTime localDateTime = LocalDateTime.now();
    Date toDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    System.out.println("LocalDateTime 转换成 Date:" + toDate);\
        
    // 当前时间转时间戳
    long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    System.out.println("当前时间转时间戳:" + epochMilli);
    // 时间戳转换成时间
    LocalDateTime epochMilliTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    System.out.println("时间戳转换成时间:" + epochMilliTime);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

转换结果:

字符串时间转换:2019-10-01T22:22:22.222
字符串时间转换-指定格式:2019-01-01
Date 转换成 LocalDateTime:2019-10-24T00:46:41.251
LocalDateTime 转换成 Date:Thu Oct 24 00:46:41 GMT+08:00 2019
当前时间转时间戳:1571849201258
时间戳转换成时间:2019-10-24T00:46:41.258
1
2
3
4
5
6

# 5. 时间格式化

/**
 * 日期格式化
 */
@Test
public void formatTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println("格式化后:" + now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss")));
}
1
2
3
4
5
6
7
8
9
10
11
12

格式化后:

当前时间:2019-10-24T00:37:44.867
格式化后:2019-10-24T00:37:44.867
格式化后:2019-10-24
格式化后:00:37:44.867
格式化后:2019-10-24 12:37:44
1
2
3
4
5

# 6. 时间比较

/**
 * 时间比较
 */
@Test
public void diffTest() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime yestory = now.minusDays(1);
    System.out.println(now + "在" + yestory + "之后吗?" + now.isAfter(yestory));
    System.out.println(now + "在" + yestory + "之前吗?" + now.isBefore(yestory));

    // 时间差
    long day = yestory.until(now, ChronoUnit.DAYS);
    long month = yestory.until(now, ChronoUnit.MONTHS);
    long hours = yestory.until(now, ChronoUnit.HOURS);
    long minutes = yestory.until(now, ChronoUnit.MINUTES);
    System.out.println("相差月份" + month);
    System.out.println("相差天数" + day);
    System.out.println("相差小时" + hours);
    System.out.println("相差分钟" + minutes);

    // 距离JDK 14 发布还有多少天?
    LocalDate jdk14 = LocalDate.of(2020, 3, 17);
    LocalDate nowDate = LocalDate.now();
    System.out.println("距离JDK 14 发布还有:" + nowDate.until(jdk14, ChronoUnit.DAYS) + "天");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

比较结果:

2019-10-24T00:39:01.589在2019-10-23T00:39:01.589之后吗?true
2019-10-24T00:39:01.589在2019-10-23T00:39:01.589之前吗?false
相差月份0
相差天数1
相差小时24
相差分钟1440
距离JDK 14 发布还有:145天
1
2
3
4
5
6
7

# 7. 时间加减

/**
 * 日期加减
 */
@Test
public void calcTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:"+now);
    LocalDateTime plusTime = now.plusMonths(1).plusDays(1).plusHours(1).plusMinutes(1).plusSeconds(1);
    System.out.println("增加1月1天1小时1分钟1秒时间后:" + plusTime);
    LocalDateTime minusTime = now.minusMonths(2);
    System.out.println("减少2个月时间后:" + minusTime);
}
1
2
3
4
5
6
7
8
9
10
11
12

操作结果:

当前时间:2019-10-24T00:41:08.877
增加1月1天1小时1分钟1秒时间后:2019-11-25T01:42:09.877
减少2个月时间后:2019-08-24T00:41:08.877	
1
2
3

# 8. 时间扩展方法

/**
 * 时间方法
 */
@Test
public void timeFunctionTest() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    // 第一天
    LocalDateTime firstDay = now.withDayOfMonth(1);
    System.out.println("本月第一天:" + firstDay);
    // 当天最后一秒
    LocalDateTime lastSecondOfDay = now.withHour(23).withMinute(59).withSecond(59);
    System.out.println("当天最后一秒:" + lastSecondOfDay);
    // 最后一天
    LocalDateTime lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("本月最后一天:" + lastDay);
    // 是否闰年
    System.out.println("今年是否闰年:" + Year.isLeap(now.getYear()));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

输出结果:

当前时间:2019-10-24T00:43:28.296
本月第一天:2019-10-01T00:43:28.296
当天最后一秒:2019-10-24T23:59:59.296
本月最后一天:2019-10-31T00:43:28.296
今年是否闰年:false
1
2
3
4
5

Jdk 8 新的时间类使用起来相比之前显得更加方便简单。

JDK8 之前时间处理

Jdk 8 也把时间处理成独立成一个包,并且使用不同的类名加以区分。而不是像之前相同的类名不同的包。这样的方式使用起来也更加清晰。

🚀 代码已经上传到 Github(https://github.com/niumoo/jdk-feature) (opens new window) 。

订阅

文章持续更新,订阅可以关注「 程序猿阿朗 」公众号或者未读代码博客。

文章作者: 程序猿阿朗
文章链接:https://www.wdbyte.com/2019/10/jdk/jdk8-time/
版权声明:本网站当前文章采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 未读代码!
#Java8#LocalDateTime#LocalDate
上次更新: 2022/12/05, 08:18:32
Java 8 Stream 流式操作
Java 8 List 转 Map

← Java 8 Stream 流式操作 Java 8 List 转 Map→

最近更新
01
如何搭建一个自己的音乐服务器
12-04
02
JUnit 5 单元测试教程
11-17
03
使用 StringUtils.split 的坑
11-02
更多文章>

提示:评论前请刷新页面,否则评论的可能不是当前文章。

Theme by Vdoing | Copyright © 2018-2022 程序猿阿朗 | MIT License | 皖ICP备20000567号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式