To 遗留类 和 From 遗留类 与 传统日期处理的转换(配有详细案例代码解析)

前言:

小编最近又要练科目三了

天天好多事情啊,

不知道大家放了假事情多不多

我们继续日更!!!

我们一直都是以这样的形式,让新手小白轻松理解复杂晦涩的概念,

把Java代码拆解的清清楚楚,每一步都知道他是怎么来的,

为什么用这串代码关键字,对比同类型的代码,

让大家真正看完以后融会贯通,举一反三,实践应用!!!!


①官方定义  和  大白话拆解对比

②举生活中常见贴合例子、图解辅助理解的形式

③对代码实例中关键部分进行详细拆解、总结



给小编一个赞或者关注吧,我们一起进步!!

官方语言解释


1、java.time.Instant 与 java.util.Date

  • Instant 是一个表示时间戳的类,它代表的是一个特定的时间点,不包含时区信息。
  • Date 是一个较旧的类,用于表示某个特定的时间点,包含了时区信息。

转换方法:

  • Date.from(Instant):将 Instant 对象转换为 Date 对象。
  • date.toInstant():将 Date 对象转换为 Instant 对象。


2、java.sql.Timestamp 与 java.time.Instant

  • Timestamp 是一个SQL数据库中使用的日期时间类型,它可以精确到毫秒。

转换方法:

  • Timestamp.from(Instant):将 Instant 对象转换为 Timestamp 对象。
  • timestamp.toInstant():将 Timestamp 对象转换为 Instant 对象。


3、java.time.ZonedDateTime 与 java.util.GregorianCalendar

  • ZonedDateTime 是一个表示带有时区的日期时间的类。
  • GregorianCalendar 是一个日历系统,可以用来存储和操作日期时间。

转换方法:

  • GregorianCalendar.from(ZonedDateTime):将 ZonedDateTime 对象转换为 GregorianCalendar 对象。
  • cal.toZonedDateTime():将 GregorianCalendar 对象转换为 ZonedDateTime 对象。

4、java.time.LocalDate 与 java.sql.Date

  • LocalDate 是一个表示日期的类,不包含时间部分。
  • Date 是一个SQL数据库中使用的日期类型。

转换方法:

  • Date.valueOf(LocalDate):将 LocalDate 对象转换为 Date 对象。
  • date.toLocalDate():将 Date 对象转换为 LocalDate 对象。


5、java.time.LocalDateTime 与 java.sql.Timestamp

  • LocalDateTime 是一个表示日期和时间的类,不包含时区信息。
  • Timestamp 是一个SQL数据库中使用的日期时间类型,它可以精确到毫秒。

转换方法:

  • Timestamp.valueOf(LocalDateTime):将 LocalDateTime 对象转换为 Timestamp 对象。
  • timestamp.toLocalDateTime():将 Timestamp 对象转换为 LocalDateTime 对象。


6、java.time.ZonedDateTime 与 java.util.TimeZone

  • ZonedDateTime 是一个表示带有时区的日期时间的类。
  • TimeZone 是一个表示时区的类。

转换方法:

  • TimeZone.getTimeZone(id):根据ID获取 TimeZone 对象。
  • timeZone.toZonedDateTime():将 TimeZone 对象转换为 ZonedDateTime 对象。


7、java.time.format.DateTimeFormatter 与 java.text.DateFormat

  • DateTimeFormatter 是一个用于格式化和解析日期时间的类。
  • DateFormat 是一个用于格式化和解析日期时间的类。

转换方法:

  • formatter.toFormat():将 DateTimeFormatter 对象转换为 DateFormat 对象。

大白话拆解:


1、java.time.Instant 与 java.util.Date

  • Instant 就像是一个没有时区的时间戳,比如“2023年10月5日14:30:00”。
  • Date 则是带有时区的时间戳,比如“2023年10月5日14:30:00,北京时间”。

转换方法:

  • 把 Instant 转换成 Date,就像是给时间戳加上了时区信息。
  • 把 Date 转换成 Instant,就像是把时间戳的时区信息去掉了。

2、java.sql.Timestamp 与 java.time.Instant

  • Timestamp 是数据库里用的时间戳,非常精确,可以精确到毫秒。

转换方法:

  • 把 Instant 转换成 Timestamp,就像是给时间戳加上了毫秒的精度。
  • 把 Timestamp 转换成 Instant,就像是把时间戳的毫秒精度去掉了。


3、java.time.ZonedDateTime 与 java.util.GregorianCalendar

  • ZonedDateTime 是一个带有具体时区的日期时间,比如“2023年10月5日14:30:00,北京时间”。
  • GregorianCalendar 是一个日历系统,可以用来存储和操作日期时间。

转换方法:

  • 把 ZonedDateTime 转换成 GregorianCalendar,就像是把日期时间信息放进了日历系统里。
  • 把 GregorianCalendar 转换成 ZonedDateTime,就像是从日历系统里取出了日期时间信息,并加上了具体的时区。

4、java.time.LocalDate 与 java.sql.Date

  • LocalDate 就像是一个只记录日期的日历,比如“2023年10月5日”。
  • Date 是数据库里用的日期,也只记录日期。

转换方法:

  • 把 LocalDate 转换成 Date,就像是把日历上的日期放进数据库里。
  • 把 Date 转换成 LocalDate,就像是从数据库里取出日期放到日历上。


5、java.time.LocalDateTime 与 java.sql.Timestamp

  • LocalDateTime 是一个记录日期和时间的类,比如“2023年10月5日14:30:00”。
  • Timestamp 是数据库里用的日期时间,可以精确到毫秒。

转换方法:

  • 把 LocalDateTime 转换成 Timestamp,就像是给日期时间加上了毫秒的精度。
  • 把 Timestamp 转换成 LocalDateTime,就像是去掉毫秒的精度。


6、java.time.ZonedDateTime 与 java.util.TimeZone

  • ZonedDateTime 是一个带有具体时区的日期时间,比如“2023年10月5日14:30:00,北京时间”。
  • TimeZone 是一个表示时区的类。

转换方法:

  • 根据ID获取 TimeZone 对象,就像是选择一个具体的时区。
  • 把 TimeZone 对象转换为 ZonedDateTime 对象,就像是给日期时间加上具体的时区。


7、java.time.format.DateTimeFormatter 与 java.text.DateFormat

  • DateTimeFormatter 是一个用于格式化和解析日期时间的类。
  • DateFormat 是一个用于格式化和解析日期时间的类。

转换方法:

  • 把 DateTimeFormatter 对象转换为 DateFormat 对象,就像是把一种格式化方式转换成另一种。

举个栗子:

假设上班老板需要你开发一个简单的应用程序,用于记录和管理个人会议安排。这个应用程序需要能够创建会议(包含日期、时间和时区信息),将其存储到数据库中,并且能从数据库中读取这些信息。

场景描述

  1. 创建一个新的会议安排。
  2. 将会议的时间转换为适合数据库存储的格式。
  3. 从数据库中读取会议的时间,并将其转换回程序可操作的格式。
  4. 显示会议的信息给用户。
import java.time.*;
import java.util.TimeZone;
import java.sql.Timestamp;

public class MeetingScheduler {

    public static void main(String[] args) {
        // 创建一个会议时间,使用LocalDateTime表示不带时区的日期时间
        LocalDateTime meetingTime = LocalDateTime.of(2025, 1, 18, 15, 0); // 假设会议时间为2025年1月18日15:00
        System.out.println("原始会议时间 (LocalDateTime): " + meetingTime);

        // 添加时区信息,使用ZonedDateTime
        ZonedDateTime zonedMeetingTime = meetingTime.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("带有时区的会议时间 (ZonedDateTime): " + zonedMeetingTime);

        // 转换为Instant,用于与Date进行交互
        Instant instantMeetingTime = zonedMeetingTime.toInstant();
        System.out.println("Instant形式的会议时间: " + instantMeetingTime);

        // 将Instant转换为java.util.Date对象,以便兼容旧系统
        Date utilDate = Date.from(instantMeetingTime);
        System.out.println("java.util.Date形式的会议时间: " + utilDate);

        // 将LocalDateTime转换为Timestamp,用于数据库存储
        Timestamp timestampMeetingTime = Timestamp.valueOf(meetingTime);
        System.out.println("Timestamp形式的会议时间: " + timestampMeetingTime);

        // 假设从数据库中读取了Timestamp数据
        Timestamp readTimestamp = Timestamp.valueOf("2025-01-18 15:00:00");
        LocalDateTime retrievedMeetingTime = readTimestamp.toLocalDateTime();
        System.out.println("从数据库读取并转换回LocalDateTime的会议时间: " + retrievedMeetingTime);

        // 将ZonedDateTime转换为GregorianCalendar,用于与其他API交互
        GregorianCalendar calendarMeetingTime = GregorianCalendar.from(zonedMeetingTime);
        System.out.println("GregorianCalendar形式的会议时间: " + calendarMeetingTime.getTime());

        // 将GregorianCalendar转换回ZonedDateTime,以恢复原有时区信息
        ZonedDateTime convertedBackZonedMeetingTime = calendarMeetingTime.toZonedDateTime();
        System.out.println("转换回ZonedDateTime后的会议时间: " + convertedBackZonedMeetingTime);
    }
}

代码解释和总结:

1、创建会议时间:

  • LocalDateTime meetingTime = LocalDateTime.of(2025, 1, 18, 15, 0);
  • 这行代码创建了一个不带时区的日期时间对象meetingTime,表示在2025年1月18日的下午3点(15:00)举行的一个会议。这里使用的是LocalDateTime类,它不会考虑任何时区信息。


2、添加时区信息:

  • ZonedDateTime zonedMeetingTime = meetingTime.atZone(ZoneId.of("Asia/Shanghai"));
  • 上一步我们创建的时间是没有时区的,这步将这个时间与特定的时区(这里是上海所在的“Asia/Shanghai”时区)关联起来,转换成一个带有时区信息的时间对象zonedMeetingTime。

3、转换为Instant:

  • Instant instantMeetingTime = zonedMeetingTime.toInstant();
  • Instant是一个用于表示时间戳的类,它可以用来记录一个精确到纳秒的时间点。这里我们将带有时区的时间转换为一个Instant对象,便于与其他系统或API交互。

4、转换为java.util.Date:

  • Date utilDate = Date.from(instantMeetingTime);
  • java.util.Date是旧版本Java中用于表示日期和时间的类。这里我们把Instant对象转换为Date对象,以便于兼容那些需要使用旧版日期时间处理方式的系统。

5、转换为Timestamp:

  • Timestamp timestampMeetingTime = Timestamp.valueOf(meetingTime);
  • Timestamp主要用于数据库操作,可以存储日期时间数据。这里将LocalDateTime对象转换为Timestamp对象,以便存储到数据库中。

6、从数据库读取并转换回LocalDateTime:

  • 假设有一个Timestamp对象代表了从数据库中读取的数据,这里将其转换回LocalDateTime格式,方便程序内部处理。

7、转换为GregorianCalendar:

  • 将ZonedDateTime对象转换为GregorianCalendar对象,这是为了能够与需要这种类型作为输入的其他API或库进行交互。

8、转换回ZonedDateTime:

  • 最后一步展示了如何将GregorianCalendar对象再转换回ZonedDateTime对象,以恢复其原始的时区信息。



我们今天就到这里,下次见吧!!!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值