java—如何将多个日期字符串格式转换为时区格式的时间戳

ghg1uchk  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(304)

这个问题在这里已经有答案了

向hh:mm:ss.ssss格式的时间戳字符串添加尾随零(2个答案)
24天前关门。
我想将多个字符串日期格式转换为时间戳
例如,我有如下一串日期

String date1 = "2020-01-01 12:23:30.345"
String date2 = "2020-01-01 12:23:30"
String date3 = "2020-01-01 12:23"
String date4 = "2020-01-01 12"

我想把上面所有的字符串格式转换成时间戳,

For date1, Timestamp should be 2020-01-01 12:23:30.345 UTC
For date2, Timestamp should be 2020-01-01 12:23:30.000 UTC
For date3, Timestamp should be 2020-01-01 12:23:00.000 UTC
For date4, Timestamp should be 2020-01-01 12:00:00.000 UTC

你能帮我做这个吗?任何信息都会非常有用。我不能为解析器定义单一的字符串格式,因为在我的场景中字符串格式是不可预测的。

ar5n3qh5

ar5n3qh51#

您可以使用两个格式化程序一个进行转换 StringLocalDateTime 另一个要转换 LocalDateTime 达到预期效果 String :

String[] dates = {date1, date2, date3, date4};
DateTimeFormatter formatterIn = DateTimeFormatter.ofPattern("yyyy-MM-dd HH[:mm][:ss][.SSS]");
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

for (String date : dates) {
    LocalDateTime ldtIn = LocalDateTime.parse(date, formatterIn);
    String ldtOut = ldtIn.format(formatterOut);
    System.out.println(ldtOut);
}

输出

2020-01-01 12:23:30.345
2020-01-01 12:23:30.000
2020-01-01 12:23:00.000
2020-01-01 12:00:00.000
mdfafbf1

mdfafbf12#

对于缺少的零件,可以使用可选的图案(内方括号)。你也可以使用 DateTimeFormatterBuilder#parseDefaulting 将时间的缺失部分默认为 0 或任何其他值。

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);

        // Tests
        System.out.println(getLocalDateTime("2020-01-01 12:23:30.345"));
        System.out.println(getLocalDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));

        System.out.println(getLocalDateTime("2020-01-01 12:23:30"));
        System.out.println(getLocalDateTime("2020-01-01 12:23:30").format(dtfForFormating));

        System.out.println(getLocalDateTime("2020-01-01 12:23"));
        System.out.println(getLocalDateTime("2020-01-01 12:23").format(dtfForFormating));

        System.out.println(getLocalDateTime("2020-01-01 12"));
        System.out.println(getLocalDateTime("2020-01-01 12").format(dtfForFormating));
    }

    static LocalDateTime getLocalDateTime(String text) {
        DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
                                            .appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
                                            .parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
                                            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                                            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)                                         
                                            .toFormatter(Locale.ENGLISH);

        return LocalDateTime.parse(text, multiFormatter);
    }
}

输出:

2020-01-01T12:23:30.345
2020-01-01 12:23:30.345
2020-01-01T12:23:30
2020-01-01 12:23:30.000
2020-01-01T12:23
2020-01-01 12:23:00.000
2020-01-01T12:00
2020-01-01 12:00:00.000

更新

这是一个更新的基础上,op的要求有时区的日期和时间。 LocalDateTime 没有时区信息。为了获得时区信息,您需要将其转换为 ZonedDateTime .
演示:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS z", Locale.ENGLISH);

        // Tests
        System.out.println(getZonedDateTime("2020-01-01 12:23:30.345"));
        System.out.println(getZonedDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));

        System.out.println(getZonedDateTime("2020-01-01 12:23:30"));
        System.out.println(getZonedDateTime("2020-01-01 12:23:30").format(dtfForFormating));

        System.out.println(getZonedDateTime("2020-01-01 12:23"));
        System.out.println(getZonedDateTime("2020-01-01 12:23").format(dtfForFormating));

        System.out.println(getZonedDateTime("2020-01-01 12"));
        System.out.println(getZonedDateTime("2020-01-01 12").format(dtfForFormating));
    }

    static ZonedDateTime getZonedDateTime(String text) {
        DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
                                            .appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
                                            .parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
                                            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                                            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                                            .toFormatter(Locale.ENGLISH);

        return LocalDateTime.parse(text, multiFormatter).atZone(ZoneId.of("Etc/UTC"));
    }
}

输出:

2020-01-01T12:23:30.345Z[Etc/UTC]
2020-01-01 12:23:30.345 UTC
2020-01-01T12:23:30Z[Etc/UTC]
2020-01-01 12:23:30.000 UTC
2020-01-01T12:23Z[Etc/UTC]
2020-01-01 12:23:00.000 UTC
2020-01-01T12:00Z[Etc/UTC]
2020-01-01 12:00:00.000 UTC

相关问题