java - 如何将时间戳字符串更改为 "DD.MM.YYYY"?

标签 java string date datetime

我需要将不同格式的字符串转换为“DD.MM.YYYY”。

“Thu, 3 Nov 2022 06:00:00 +0100” 必须更改为 “03.11.2022”

“01.11.2022 20:00:00”“01.11.2022”

所有格式都在String中。

我试过

String pattern="DD.MM.YYYY";
DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern);

new SimpleDateFormat(pattern).parse("01.11.2022 20:00:00")

我也试过做以下事情

java.time.LocalDateTime.parse(
        item.getStartdatum(),
        DateTimeFormatter.ofPattern( "DDMMYYYY" )
    ).format(
        DateTimeFormatter.ofPattern("DD.MM.YYYY")
    )

但是得到了错误:

Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Sun, 30 Oct 2022 00:30:00 +0200' could not be parsed at index 0

我也尝试过以下操作

String pattern="DD.MM.YYYY";
DateFormat format = new SimpleDateFormat(pattern);
Date date = format.parse(01.11.2022 20:00:00);

但是,我没有得到正确的输出。我怎样才能得到我想要的结果?

最佳答案

几件事……

  • 如果您可以使用 java.time,请尽可能单独使用它(不要使用 SimpleDateFormat 或类似的遗留内容)
  • 一个DateTimeFormatter可以用来解析格式代表一个日期时间的String,如果输入和输出格式不同,你将需要两个不同的 DateTimeFormatter
  • 文本“Sun,2022 年 10 月 30 日 00:30:00 +0200”无法在索引 0 处进行解析,因为您尝试使用模式 “DD.MM”对其进行解析.YYYY",这在几个层面上是错误的:
    • 该模式似乎期望 String 以表示月份日期的数字表示开头,但它以 Thu 开头,这是日期名称的缩写一周
    • 符号 D 表示一年中的第几天,一个介于 1 和 366 之间的数字(在闰年,否则为 365)
    • 符号 Y 表示基于周的年份

阅读有关这些符号的更多信息 in the JavaDocs of DateTimeFormatter

您可以改为执行以下操作:

public static void main(String[] args) {
    // two example inputs
    String first = "Thu, 3 Nov 2022 06:00:00 +0100";
    String second = "01.11.2022 20:00:00";
    // prepare a formatter for each pattern in order to parse the Strings
    DateTimeFormatter dtfInFirst = DateTimeFormatter.ofPattern(
                                        "EEE, d MMM uuuu HH:mm:ss x",
                                        Locale.ENGLISH
                                   );
    // (second one does not have an offset from UTC, so the resulting class is different)
    DateTimeFormatter dtfInSecond = DateTimeFormatter.ofPattern("dd.MM.uuuu HH:mm:ss");
    // parse the Strings using the formatters
    OffsetDateTime odt = OffsetDateTime.parse(first, dtfInFirst);
    LocalDateTime ldt = LocalDateTime.parse(second, dtfInSecond);
    // prepare a formatter, this time for output formatting
    DateTimeFormatter dtfDateOnlySeparatedByDots = DateTimeFormatter.ofPattern("dd.MM.uuuu");
    // extract the date part of each result of the parsing
    LocalDate firstResult = odt.toLocalDate();
    LocalDate secondResult = ldt.toLocalDate();
    // and print it formatted using the output formatter
    System.out.println(first + " ---> "
                             + firstResult.format(dtfDateOnlySeparatedByDots));
    System.out.println(second + " ---> " 
                              + secondResult.format(dtfDateOnlySeparatedByDots));
}

会输出如下转换结果:

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

第一个格式化程序将需要一个 Locale 因为存在名称(星期几和月份)。您不能使用任何专门的数字解析器来解析它,并且语言/文化必须匹配。<


精简版

public static void main(String[] args) {
    // two example inputs
    String first = "Thu, 3 Nov 2022 06:00:00 +0100";
    String second = "01.11.2022 20:00:00";
    // prepare a custom formatter for the second pattern
    DateTimeFormatter dtfInSecond = DateTimeFormatter
                                        .ofPattern("dd.MM.uuuu HH:mm:ss");
    // parse the first String by means of a built-in RFC formatter
    OffsetDateTime odt = OffsetDateTime.parse(
                                first,
                                DateTimeFormatter.RFC_1123_DATE_TIME);
    // parse the second String using the custom formatter
    LocalDateTime ldt = LocalDateTime.parse(second, dtfInSecond);
    // prepare a formatter, this time for output formatting
    DateTimeFormatter dtfDateOnlySeparatedByDots = DateTimeFormatter
                                                        .ofPattern("dd.MM.uuuu");
    // and print it formatted using the output formatter
    System.out.println(first + " ---> "
                             + odt.format(dtfDateOnlySeparatedByDots));
    System.out.println(second + " ---> " 
                              + ldt.format(dtfDateOnlySeparatedByDots));
}

提示:

对于您评论中提到的日期......

Text '9.28.2022 6:30:00' could not be parsed at index 0

如果像 9.8.2022 那样可能的话,您将不得不使用具有个位数的月中日和日中小时的模式,甚至可能是年中的月份。但是,您肯定需要切换月份中的日期和年份中的月份,因为没有月份。一年 28 次。

简短示例:

String third = "9.28.2022 6:30:00";
DateTimeFormatter dtfInThird = DateTimeFormatter
                                    .ofPattern("M.d.uuuu H:mm:ss");
LocalDateTime ldtThird = LocalDateTime.parse(third, dtfInThird);
System.out.println(third + " ---> " 
                         + ldtThird.format(dtfDateOnlySeparatedByDots));

main 中执行,这将输出

9.28.2022 6:30:00 ---> 28.09.2022

关于java - 如何将时间戳字符串更改为 "DD.MM.YYYY"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74318525/

相关文章:

iPhone SDK NSString 转 NSDate

java - 如何更改Java Post上传文件的最大大小

java - 如何在我的 Android 应用程序项目中包含新的 .java 文件?

python - 尝试根据大小写将一个字符串解析为两个单独的字符串

c - 如何从C中的文件中获取未知数量的字符串?

java - 日历的奇怪行为

Java线程问题

java - 如何在Android应用程序中发现文章文字大小发生变化

Android:有没有一种简单的方法可以找到我项目中的所有字符串?

r - 为每个 ID 查找重叠日期并为重叠创建一个新行