不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • Java基础

  • 面向对象

  • Java进阶

    • 01.文件处理
    • 02.异常处理
    • 03.反射
    • 04.注解
    • 05.泛型
    • 06.日期与时间
      • 01.ZonedDateTime
        • 0、说明
        • 1、时间转换
        • 2、时间差
      • 02.LocalDateTime
        • 0、说明
        • 1、时间转换
        • 2、时间差
      • 03.LocalTime
        • 1、时间转换
        • 2、时间差
      • 04.LocalDate
        • 1、时间转换
        • 2、时间差
      • 05.Date
    • 07.多线程基础
    • 08.多线程进阶
    • 10.设计模式
  • Web基础

  • Spring框架

  • 微服务

  • Java
  • Java进阶
xiaonaiqiang
2024-04-29
目录

06.日期与时间

# 01.ZonedDateTime

# 0、说明

  • LocalDateTime 表示本地日期时间信息,不包含时区信息
  • ZonedDateTime 表示带有时区信息的日期时间信息
  • 在使用 ZonedDateTime 时,我们需要确保时区信息的正确性
  • 例如,在计算两个 ZonedDateTime 之间的时间差时,需要确保它们的时区信息相同,否则计算结果将会不正确

# 1、时间转换

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.Instant;
import java.time.ZoneOffset;
public class Test {
    public static void main(String[] args) {
        printStrDate();  // 打印格式化时间
        printTimestamp();  // 打印时间戳
        strToObj();  // 字符串转时间对象
        timestampToObj();  // 时间戳转时间对象
    }
    public static void printStrDate() {
        ZonedDateTime now = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String strDate = now.format(formatter);
        // 格式化时间字符串:2024-04-29 10:40:37
        System.out.println("格式化时间字符串:" + strDate);
    }
    public static void printTimestamp() {
        ZonedDateTime now = ZonedDateTime.now();
        long timestamp = now.toInstant().toEpochMilli();
        // 当前时间戳:1714358437142
        System.out.println("当前时间戳:" + timestamp);
    }
    public static void strToObj() {
        String strDate = "2022-03-16 10:23:45";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZonedDateTime dateTime = ZonedDateTime.parse(strDate, formatter.withZone(ZoneOffset.of("+8")));
        // 字符串转时间对象:2022-03-16T10:23:45+08:00[Asia/Shanghai]
        System.out.println("字符串转时间对象:" + dateTime);
    }
    public static void timestampToObj() {
        long timestamp = 1647418245000L;
        ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.of("+8"));
        // 时间戳转:2022-03-15T16:10:45+08:00[Asia/Shanghai]
        System.out.println("时间戳转:" + dateTime);
    }
}
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
29
30
31
32
33
34
35
36
37
38

# 2、时间差

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
public class Test {
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        // 一秒前的时间
        ZonedDateTime oneSecondAgo = now.minusSeconds(1);
        System.out.println("一秒前的时间:" + oneSecondAgo);
        // 一分钟前的时间
        ZonedDateTime oneMinuteAgo = now.minusMinutes(1);
        System.out.println("一分钟前的时间:" + oneMinuteAgo);
        // 一小时前的时间
        ZonedDateTime oneHourAgo = now.minusHours(1);
        System.out.println("一小时前的时间:" + oneHourAgo);
        // 一天前的时间
        ZonedDateTime oneDayAgo = now.minusDays(1);
        System.out.println("一天前的时间:" + oneDayAgo);
        // 两个时间的时间差(秒)
        ZonedDateTime start = ZonedDateTime.of(2022, 3, 16, 10, 0, 0, 0, ZoneOffset.ofHours(8));
        ZonedDateTime end = ZonedDateTime.of(2022, 3, 16, 11, 30, 0, 0, ZoneOffset.ofHours(8));
        Duration duration = Duration.between(start, end);
        long seconds = duration.get(ChronoUnit.SECONDS);
        System.out.println("两个时间的时间差(秒):" + seconds);
    }
}
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

# 02.LocalDateTime

# 0、说明

  • 如果只需要获取当前时间戳并进行基本的日期时间操作,可以使用 java.util.Date
  • 如果需要更加精确的时间戳或者需要进行时区转换等操作,可以使用 java.time.LocalDateTime
  • 两者对比
    • 类型不同:Date 时间戳 long 类型,而 LocalDateTime 时间戳是 Instant 类型
    • 精度不同:Date 时间戳精确到毫秒级别,而 LocalDateTime 时间戳精确到纳秒级别
    • 时区不同:Date 时间戳是相对于默认时区的,而 LocalDateTime 时间戳需要通过 ZoneOffset 指定时区偏移量来计算

# 1、时间转换

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        printStrDate();  // 打印格式化时间
        printTimestamp();  // 打印时间戳
        strToObj();  // 字符串转时间对象
        timestampToObj();  // 时间戳转时间对象

    }

    public static void printStrDate() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String strDate = now.format(formatter);
        // 格式化时间字符串:2024-04-29 10:40:37
        System.out.println("格式化时间字符串:" + strDate);
    }

    public static void printTimestamp() {
        LocalDateTime now = LocalDateTime.now();
        long timestamp = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        // 当前时间戳:1714358437142
        System.out.println("当前时间戳:" + timestamp);
    }

    public static void strToObj() {
        String strDate = "2022-03-16 10:23:45";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(strDate, formatter);
        // 字符串转时间对象:2022-03-16T10:23:45
        System.out.println("字符串转时间对象:" + dateTime);
    }

    public static void timestampToObj() {
        long timestamp = 1647418245000L;
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
        // 时间戳转:2022-03-16T16:10:45
        System.out.println("时间戳转:" + dateTime);
    }
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 2、时间差

import java.time.LocalDateTime;
import java.time.Duration;

public class Test {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        // 一秒前的时间
        LocalDateTime oneSecondAgo = now.minusSeconds(1);
        System.out.println("一秒前的时间:" + oneSecondAgo);

        // 一分钟前的时间
        LocalDateTime oneMinuteAgo = now.minusMinutes(1);
        System.out.println("一分钟前的时间:" + oneMinuteAgo);

        // 一小时前的时间
        LocalDateTime oneHourAgo = now.minusHours(1);
        System.out.println("一小时前的时间:" + oneHourAgo);

        // 一天前的时间
        LocalDateTime oneDayAgo = now.minusDays(1);
        System.out.println("一天前的时间:" + oneDayAgo);

        // 两个时间的时间差(秒)
        LocalDateTime start = LocalDateTime.of(2022, 3, 16, 10, 0, 0);
        LocalDateTime end = LocalDateTime.of(2022, 3, 16, 11, 30, 0);
        Duration duration = Duration.between(start, end);
        long seconds = duration.getSeconds();
        System.out.println("两个时间的时间差(秒):" + seconds);
    }
}
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
29
30
31

# 03.LocalTime

# 1、时间转换

import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        printStrDate();  // 打印格式化时间
        printTimestamp();  // 打印时间戳
        strToObj();  // 字符串转时间对象
        timestampToObj();  // 时间戳转时间对象
    }

    public static void printStrDate() {
        LocalTime now = LocalTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String strTime = now.format(formatter);
        // 格式化后的时间字符串:11:06:10
        System.out.println("格式化后的时间字符串:" + strTime);
    }

    public static void printTimestamp() {
        LocalTime now = LocalTime.now();
        long timestamp = now.toNanoOfDay() / 1000000;
        // 当前时间戳:39970994
        System.out.println("当前时间戳:" + timestamp);
    }

    public static void strToObj() {
        String strTime = "10:23:45";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime time = LocalTime.parse(strTime, formatter);
        // 格式化后的时间字符串转换为时间对象:10:23:45
        System.out.println("格式化后的时间字符串转换为时间对象:" + time);
    }

    public static void timestampToObj() {
        long timestamp = 1647418245000L;
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalTime time = instant.atOffset(ZoneOffset.of("+8")).toLocalTime();
        // 时间戳对应的时间对象:16:10:45
        System.out.println("时间戳对应的时间对象:" + time);
    }
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 2、时间差

import java.time.LocalTime;
import java.time.Duration;
public class Test {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        // 一秒前的时间:11:14:27.582276
        LocalTime oneSecondAgo = now.minusSeconds(1);
        System.out.println("一秒前的时间:" + oneSecondAgo);
        // 一分钟前的时间:11:13:28.582276
        LocalTime oneMinuteAgo = now.minusMinutes(1);
        System.out.println("一分钟前的时间:" + oneMinuteAgo);
        // 一小时前的时间:10:14:28.582276
        LocalTime oneHourAgo = now.minusHours(1);
        System.out.println("一小时前的时间:" + oneHourAgo);
        // 两个时间的时间差(秒)两个时间的时间差(秒):5400
        LocalTime start = LocalTime.of(10, 0, 0);
        LocalTime end = LocalTime.of(11, 30, 0);
        Duration duration = Duration.between(start, end);
        long seconds = duration.getSeconds();
        System.out.println("两个时间的时间差(秒):" + seconds);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 04.LocalDate

# 1、时间转换

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.Instant;
public class Test {
    public static void main(String[] args) {
        printStrDate();  // 打印格式化时间
        printTimestamp();  // 打印时间戳
        strToObj();  // 字符串转时间对象
        timestampToObj();  // 时间戳转时间对象
    }
    public static void printStrDate() {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String strDate = now.format(formatter);
        // 格式化时间字符串:2024-04-29
        System.out.println("格式化时间字符串:" + strDate);
    }
    public static void printTimestamp() {
        LocalDate now = LocalDate.now();
        Instant instant = now.atStartOfDay().toInstant(ZoneOffset.of("+8"));
        long timestamp = instant.toEpochMilli();
        // 当前时间戳:1714358437142
        System.out.println("当前时间戳:" + timestamp);
    }
    public static void strToObj() {
        String strDate = "2022-03-16";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(strDate, formatter);
        // 字符串转时间对象:2022-03-16
        System.out.println("字符串转时间对象:" + date);
    }
    public static void timestampToObj() {
        long timestamp = 1647418245000L;
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDate date = instant.atOffset(ZoneOffset.of("+8")).toLocalDate();
        // 时间戳转:2022-03-16
        System.out.println("时间戳转:" + date);
    }
}

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
29
30
31
32
33
34
35
36
37
38
39
40
41

# 2、时间差

import java.time.LocalDate;
import java.time.Duration;
public class Test {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();

        // 一天前的日期:2024-04-28
        LocalDate oneDayAgo = now.minusDays(1);
        System.out.println("一天前的日期:" + oneDayAgo);

        // 两个日期的日期差(天数):2
        LocalDate start = LocalDate.of(2022, 3, 16);
        LocalDate end = LocalDate.of(2022, 3, 18);
        Duration duration = Duration.between(start.atStartOfDay(), end.atStartOfDay());
        long days = duration.toDays();
        System.out.println("两个日期的日期差(天数):" + days);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 05.Date

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        printStrDate();  // 打印格式化时间
        printTimestamp();  // 打印时间戳
        strToObj();  // 字符串转时间对象
        timestampToObj();  // 时间戳转时间对象

    }

    public static void printStrDate() {
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = formatter.format(now);
        // 格式化后的日期时间字符串:2024-04-29 09:54:23
        System.out.println("格式化后的日期时间字符串:" + strDate);
    }

    public static void printTimestamp() {
        Date now = new Date();
        long timestamp = now.getTime();
        // 当前时间戳:1714356160629
        System.out.println("当前时间戳:" + timestamp);
    }

    public static void strToObj() {
        String strDate = "2022-03-16 10:23:45";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = formatter.parse(strDate);
            // 解析后的日期时间对象:Wed Mar 16 10:23:45 CST 2022
            System.out.println("解析后的日期时间对象:" + date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void timestampToObj() {
        long timestamp = 1647418245000L;
        Date date = new Date(timestamp);
        // 时间戳对应的日期时间对象:Wed Mar 16 16:10:45 CST 2022
        System.out.println("时间戳对应的日期时间对象:" + date);
    }
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
上次更新: 2024/5/31 11:18:42
05.泛型
07.多线程基础

← 05.泛型 07.多线程基础→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式