Java 时间戳字符串转换为 “DD.MM.YYYY” 格式的实用指南

java 时间戳字符串转换为

本文旨在指导开发者如何将不同格式的时间戳字符串转换为 “DD.MM.YYYY” 格式。文章详细介绍了使用 java.time 包中的 DateTimeFormatter 类进行日期解析和格式化的方法,并通过代码示例展示了如何处理包含不同日期和时间信息的字符串,包括处理包含星期、月份名称以及时区信息的字符串,并提供了简洁高效的解决方案,同时强调了使用正确日期格式的重要性。

在 Java 中,将时间戳字符串转换为特定格式需要使用 java.time 包提供的类,特别是 DateTimeFormatter。这个类允许你定义输入和输出的日期时间格式。以下是详细的步骤和示例代码,帮助你完成转换。

使用 java.time.format.DateTimeFormatter

java.time 包是 Java 8 引入的日期和时间 API,它提供了比旧的 SimpleDateFormat 更强大和灵活的日期时间处理能力。

步骤 1:定义输入字符串和期望的输出格式

立即学习“Java免费学习笔记(深入)”;

首先,你需要定义要转换的输入字符串以及期望的输出格式 “DD.MM.YYYY”。

步骤 2:创建 DateTimeFormatter 对象

为输入字符串和输出格式分别创建 DateTimeFormatter 对象。确保输入格式与输入字符串的实际格式相匹配。

步骤 3:解析输入字符串

使用输入格式的 DateTimeFormatter 对象解析输入字符串,将其转换为 LocalDate, LocalDateTime 或 OffsetDateTime 对象,具体取决于输入字符串包含的信息。

步骤 4:格式化输出

使用输出格式的 DateTimeFormatter 对象格式化日期对象,将其转换为目标格式的字符串。

示例代码

以下代码示例展示了如何将两种不同格式的时间戳字符串转换为 “DD.MM.YYYY” 格式:

import java.time.LocalDate;import java.time.LocalDateTime;import java.time.OffsetDateTime;import java.time.format.DateTimeFormatter;import java.util.Locale;public class TimestampConverter {    public static void main(String[] args) {        // 两个示例输入        String first = "Thu, 3 Nov 2022 06:00:00 +0100";        String second = "01.11.2022 20:00:00";        // 为每个模式准备一个formatter来解析字符串        DateTimeFormatter dtfInFirst = DateTimeFormatter.ofPattern(                "EEE, d MMM uuuu HH:mm:ss x",                Locale.ENGLISH        );        // (第二个没有UTC偏移量,所以结果类不同)        DateTimeFormatter dtfInSecond = DateTimeFormatter.ofPattern("dd.MM.uuuu HH:mm:ss");        // 使用formatter解析字符串        OffsetDateTime odt = OffsetDateTime.parse(first, dtfInFirst);        LocalDateTime ldt = LocalDateTime.parse(second, dtfInSecond);        // 准备一个formatter,这次用于输出格式化        DateTimeFormatter dtfDateOnlySeparatedByDots = DateTimeFormatter.ofPattern("dd.MM.uuuu");        // 提取每个解析结果的日期部分        LocalDate firstResult = odt.toLocalDate();        LocalDate secondResult = ldt.toLocalDate();        // 并使用输出formatter格式化后打印        System.out.println(first + " ---> "                + firstResult.format(dtfDateOnlySeparatedByDots));        System.out.println(second + " ---> "                + secondResult.format(dtfDateOnlySeparatedByDots));    }}

代码解释

DateTimeFormatter.ofPattern(): 创建 DateTimeFormatter 对象,需要指定日期时间模式。模式字符串中的字母代表不同的日期时间字段,例如 dd 代表日,MM 代表月,uuuu 代表年。Locale.ENGLISH: 由于第一个输入字符串包含英文的星期和月份名称,因此需要指定 Locale.ENGLISH,确保正确解析。OffsetDateTime.parse() 和 LocalDateTime.parse(): 使用 DateTimeFormatter 对象解析字符串,将其转换为 OffsetDateTime 或 LocalDateTime 对象。odt.toLocalDate() 和 ldt.toLocalDate(): 从 OffsetDateTime 和 LocalDateTime 对象中提取日期部分,得到 LocalDate 对象。firstResult.format(dtfDateOnlySeparatedByDots) 和 secondResult.format(dtfDateOnlySeparatedByDots): 使用输出格式的 DateTimeFormatter 对象格式化 LocalDate 对象,将其转换为目标格式的字符串。

输出结果

Thu, 3 Nov 2022 06:00:00 +0100 ---> 03.11.202201.11.2022 20:00:00 ---> 01.11.2022

简短版本

import java.time.LocalDateTime;import java.time.OffsetDateTime;import java.time.format.DateTimeFormatter;public class TimestampConverterShort {    public static void main(String[] args) {        // 两个示例输入        String first = "Thu, 3 Nov 2022 06:00:00 +0100";        String second = "01.11.2022 20:00:00";        // 为第二个模式准备一个自定义的formatter        DateTimeFormatter dtfInSecond = DateTimeFormatter                .ofPattern("dd.MM.uuuu HH:mm:ss");        // 通过内置的RFC formatter解析第一个字符串        OffsetDateTime odt = OffsetDateTime.parse(                first,                DateTimeFormatter.RFC_1123_DATE_TIME);        // 使用自定义的formatter解析第二个字符串        LocalDateTime ldt = LocalDateTime.parse(second, dtfInSecond);        // 准备一个formatter,这次用于输出格式化        DateTimeFormatter dtfDateOnlySeparatedByDots = DateTimeFormatter                .ofPattern("dd.MM.uuuu");        // 并使用输出formatter格式化后打印        System.out.println(first + " ---> "                + odt.format(dtfDateOnlySeparatedByDots));        System.out.println(second + " ---> "                + ldt.format(dtfDateOnlySeparatedByDots));    }}

这个简短版本使用了 DateTimeFormatter.RFC_1123_DATE_TIME 来解析第一个字符串,这是一个内置的 RFC 1123 日期时间格式的 formatter,可以简化代码。

处理其他格式

如果遇到其他格式的日期字符串,你需要相应地调整输入格式的模式字符串。例如,如果输入字符串是 “9.28.2022 6:30:00″,你需要使用模式 “M.d.uuuu H:mm:ss”。

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class TimestampConverterOtherFormat {    public static void main(String[] args) {        String third = "9.28.2022 6:30:00";        DateTimeFormatter dtfDateOnlySeparatedByDots = DateTimeFormatter                .ofPattern("dd.MM.uuuu");        DateTimeFormatter dtfInThird = DateTimeFormatter                .ofPattern("M.d.uuuu H:mm:ss");        LocalDateTime ldtThird = LocalDateTime.parse(third, dtfInThird);        System.out.println(third + " ---> "                + ldtThird.format(dtfDateOnlySeparatedByDots));    }}

输出结果

9.28.2022 6:30:00 ---> 28.09.2022

注意事项

确保输入格式的模式字符串与输入字符串的实际格式完全匹配,否则会抛出 DateTimeParseException 异常。如果日期字符串包含月份名称或星期名称,需要指定正确的 Locale,确保正确解析。java.time 包是线程安全的,可以安全地在多线程环境中使用。在模式字符串中,uuuu 代表年,而 yyyy 代表 calendar year,在大多数情况下,它们是相同的,但在某些特殊情况下(例如 week-based year),它们可能不同。建议使用 uuuu,因为它更通用。

通过以上步骤和示例代码,你可以轻松地将各种格式的时间戳字符串转换为 “DD.MM.YYYY” 格式。 掌握 DateTimeFormatter 的使用是 Java 日期时间处理的关键。

以上就是Java 时间戳字符串转换为 “DD.MM.YYYY” 格式的实用指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/95176.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月19日 15:04:07
下一篇 2025年11月19日 15:35:41

相关推荐

发表回复

登录后才能评论
关注微信