JAVA时间类型转换处理方式

 更新时间:2026年02月06日 11:27:48   作者:丿BAIKAL巛  
本文详细介绍了Java中LocalDate与Date类型之间的转换方法,以及字符串格式化与解析技巧,同时,还涵盖了LocalDateTime和LocalTime与Date类型的转换,感兴趣的朋友跟随小编一起看看吧

LocalDateDate

Java时间类型详解:LocalDate与Date互转及字符串格式化

在Java中,时间处理是一个常见的需求。随着Java 8引入了新的日期时间API(java.time包),开发者可以更方便地处理日期和时间。本文将详细介绍 LocalDateDate 类型的互转、字符串格式化以及其他常见时间类型的转换。

一、LocalDate与Date类型互转

1.Date转LocalDate

Date 是Java早期的日期时间类,而 LocalDate 是Java 8引入的日期类。我们可以通过以下方式将 Date 转换为 LocalDate

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDate {
    public static void main(String[] args) {
        // 获取当前Date对象
        Date nowDate = new Date();
        // 将Date转换为LocalDate
        LocalDate localDate = nowDate.toInstant()          // 将Date转换为Instant
                                    .atZone(ZoneId.systemDefault())  // 指定时区
                                    .toLocalDate();        // 转换为LocalDate
        System.out.println("Date: " + nowDate);
        System.out.println("LocalDate: " + localDate);
    }
}

输出示例:

Date: Mon Oct 09 12:34:56 CST 2023
LocalDate: 2023-10-09

2.LocalDate转Date

LocalDate 转换为 Date 需要借助 ZonedDateTimeInstant

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateToDate {
    public static void main(String[] args) {
        // 获取当前LocalDate对象
        LocalDate localDate = LocalDate.now();
        // 将LocalDate转换为Date
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDate: " + localDate);
        System.out.println("Date: " + date);
    }
}

输出示例:

LocalDate: 2023-10-09
Date: Mon Oct 09 00:00:00 CST 2023

二、字符串格式化与解析

1.LocalDate与字符串互转

(1)LocalDate转字符串

使用 DateTimeFormatter 可以将 LocalDate 格式化为字符串:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateToString {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化LocalDate为字符串
        String dateString = localDate.format(formatter);
        System.out.println("LocalDate: " + localDate);
        System.out.println("Formatted String: " + dateString);
    }
}

输出示例:

LocalDate: 2023-10-09
Formatted String: 2023-10-09

(2)字符串转LocalDate

将字符串解析为 LocalDate

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
    public static void main(String[] args) {
        String dateString = "2023-10-09";
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 解析字符串为LocalDate
        LocalDate localDate = LocalDate.parse(dateString, formatter);
        System.out.println("String: " + dateString);
        System.out.println("LocalDate: " + localDate);
    }
}

输出示例:

String: 2023-10-09
LocalDate: 2023-10-09

2.Date与字符串互转

(1)Date转字符串

使用 SimpleDateFormat 可以将 Date 格式化为字符串:

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 定义格式化器
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化Date为字符串
        String dateString = formatter.format(nowDate);
        System.out.println("Date: " + nowDate);
        System.out.println("Formatted String: " + dateString);
    }
}

输出示例:

Date: Mon Oct 09 12:34:56 CST 2023
Formatted String: 2023-10-09 12:34:56

(2)字符串转Date

将字符串解析为 Date

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
    public static void main(String[] args) throws ParseException {
        String dateString = "2023-10-09 12:34:56";
        // 定义格式化器
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 解析字符串为Date
        Date date = formatter.parse(dateString);
        System.out.println("String: " + dateString);
        System.out.println("Date: " + date);
    }
}

输出示例:

String: 2023-10-09 12:34:56
Date: Mon Oct 09 12:34:56 CST 2023

三、其他常见时间类型转换

1.LocalDateTime与Date互转

(1)Date转LocalDateTime

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDateTime {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 将Date转换为LocalDateTime
        LocalDateTime localDateTime = nowDate.toInstant()
                                             .atZone(ZoneId.systemDefault())
                                             .toLocalDateTime();
        System.out.println("Date: " + nowDate);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}

(2)LocalDateTime转Date

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateTimeToDate {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        // 将LocalDateTime转换为Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("Date: " + date);
    }
}

2.LocalTime与Date互转

(1)Date转LocalTime

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalTime {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 将Date转换为LocalTime
        LocalTime localTime = nowDate.toInstant()
                                     .atZone(ZoneId.systemDefault())
                                     .toLocalTime();
        System.out.println("Date: " + nowDate);
        System.out.println("LocalTime: " + localTime);
    }
}

(2)LocalTime转Date

由于 LocalTime 只包含时间信息,转换为 Date 时需要结合日期:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalTimeToDate {
    public static void main(String[] args) {
        LocalTime localTime = LocalTime.now();
        // 将LocalTime转换为Date(需要结合日期)
        Date date = Date.from(LocalDate.now()
                                       .atTime(localTime)
                                       .atZone(ZoneId.systemDefault())
                                       .toInstant());
        System.out.println("LocalTime: " + localTime);
        System.out.println("Date: " + date);
    }
}

到此这篇关于JAVA时间类型转换处理方式的文章就介绍到这了,更多相关java时间类型转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java构造函数与普通函数用法详解

    Java构造函数与普通函数用法详解

    本篇文章给大家详细讲述了Java构造函数与普通函数用法以及相关知识点,对此有兴趣的朋友可以参考学习下。
    2018-03-03
  • Java SpringMVC的自定义异常类

    Java SpringMVC的自定义异常类

    这篇文章主要为大家详细介绍了SpringMVC的自定义异常类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java JSON全解析6种主流方案深度对比与实战指南

    Java JSON全解析6种主流方案深度对比与实战指南

    本文介绍了Java中六种主流的JSON解析库:Jackson、Gson、FastJSON、JsonPath、org.json和手动解析,每种库都有其适用的场景和特点,文章还提供了每种库的基础转换和高级功能的实战代码示例,感兴趣的朋友跟随小编一起看看吧
    2026-01-01
  • RabbitMQ中的Channel和Exchange详解

    RabbitMQ中的Channel和Exchange详解

    这篇文章主要介绍了RabbitMQ中的Channel和Exchange详解,创建和销毁TCP连接很耗时,打开太多TCP连接,耗操作系统资源,并发量大到一定程度,系统的吞吐量会降低,使用一个connection多channel的方式,可以提升连接的利用率,需要的朋友可以参考下
    2023-08-08
  • SpringBoot图片上传和访问路径映射

    SpringBoot图片上传和访问路径映射

    这篇文章主要为大家详细介绍了SpringBoot图片上传和访问路径映射,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • 解决Spring国际化文案占位符失效问题的方法

    解决Spring国际化文案占位符失效问题的方法

    本篇文章主要介绍了解决Spring国际化文案占位符失效问题的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Java开发环境jdk 1.8安装配置方法(Win7 64位系统/windows server 2008)

    Java开发环境jdk 1.8安装配置方法(Win7 64位系统/windows server 2008)

    这篇文章主要介绍了Java开发环境配置方法(Win7 64位系统/windows server 2008),需要的朋友可以参考下
    2016-10-10
  • mybatis中resulthandler的用法

    mybatis中resulthandler的用法

    这篇文章主要介绍了mybatis中resulthandler的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java之IO流面试题案例讲解

    Java之IO流面试题案例讲解

    这篇文章主要介绍了Java之IO流案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • Spring Boot 3.x GraalVM原生镜像构建内存溢出问题解决方案

    Spring Boot 3.x GraalVM原生镜像构建内存溢出问题解决方案

    文章解析了Spring Boot 3.x与GraalVM Native Image构建过程中出现的内存溢出问题,从问题概述、根本原因、诊断工具、解决方案到高级调优技巧和应急解决方案,全面覆盖了构建优化的各个方面,本文给大家介绍的非常详细,感兴趣的朋友一起学习下吧
    2026-01-01

最新评论