【问题标题】:Parse Timestamp in Java在 Java 中解析时间戳
【发布时间】:2021-11-05 03:00:32
【问题描述】:

我得到一个格式为“20210908094049.884Z”的时间戳。这是 LDAP 对象的最后修改时间戳。我使用 Spring Boot Ldap。我不知道如何在像 dd.MM.yyyy HH:mm 这样的日期时间解析这个字符串。

谁能帮帮我?

【问题讨论】:

  • 格式好像是YYYYmmddHHMMSS.sssZ
  • @AndyTurner 更像yyyyMMddHHmmss.SSS
  • @SenNoRikyu 您在 UTC+2 时区吗?
  • 这能回答你的问题吗? Parsing and formatting LocalDate with unnecessary time and timezone。我知道这个问题并不相同,但它是关于解析 LDAP 时间戳的,所以也许你可以从那里的答案中得到你需要的东西并根据你的需要进行修改。
  • 我建议你不要使用SimpleDateFormatDate。这些类设计不佳且早已过时,尤其是前者,尤其是出了名的麻烦。而是使用OffsetDateTime 和/或InstantDateTimeFormatter,均来自java.time, the modern Java date and time API

标签: java spring-boot date parsing timestamp


【解决方案1】:

这是一个例子:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // Creating new simple date formatter with the format you've given
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.SSS");

        // Defining the input date
        String inputDate = "20210908094049.884Z";

        // Parsing the date, catching the parse exception if date is malformatted
        Date date = null;
        try {
            // Date ends on a Z, we remove this Z (Z is for timezone UTC +0:00)
            date = format.parse(inputDate.replace("Z", ""));
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

给出以下输出:

Wed Sep 08 09:40:49 CEST 2021

编辑: 这是 Ole V.V. 的另一个更好的解决方案

import java.time.Instant;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        Instant instant = DateTimeFormatter
                // Defining pattern to parse
                .ofPattern("yyyyMMddHHmmss.SSSXX")
                // Defining input to parse with pattern
                .parse("20210908094049.884Z", Instant::from);
        System.out.println(instant);
    }
}

输出是具有价值的瞬间:

2021-09-08T09:40:49.884Z

【讨论】:

  • 请不要教年轻人使用早已过时且臭名昭著的SimpleDateFormat类。至少不是第一选择。而且不是没有任何保留。我们在java.time, the modern Java date and time API, 和它的DateTimeFormatter 中做得更好。
  • 哦,那我很抱歉。我就是这么学的。
  • 在 java.time 于 2014 年问世并有效替换旧类之前,我们都是这样学习的(不过,在 Java 7 中删除 Z 也是错误的)。跨度>
  • DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSSXX").parse("20210908094049.884Z", Instant::from)。产生Instant2021-09-08T09:40:49.884Z
  • Ty,我已将您的解决方案添加到我的答案中
【解决方案2】:

java.time

我建议您使用现代 Java 日期和时间 API java.time 来处理时间戳。

LDAP 时间戳格式有许多允许的变体(请参阅底部的链接)。以下格式化程序考虑了其中的许多,而不是全部。

private static final DateTimeFormatter LDAP_PARSER = new DateTimeFormatterBuilder()
        .appendPattern("uuuuMMddHHmmss")
        .optionalStart()
        .appendPattern("[.][,]")
        .appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, false)
        .optionalEnd()
        .appendPattern("[XX][X]")
        .toFormatter(Locale.ROOT);

使用此格式化程序,我们可以将您的字符串解析为OffsetDateTime

    String ldapTimestampString = "20210908094049.884Z";
    OffsetDateTime timestamp = OffsetDateTime.parse(ldapTimestampString, LDAP_PARSER);
    System.out.println(timestamp);

输出是:

2021-09-08T09:40:49.884Z

格式化

要将时间戳转换为包含日期和时间的字符串,您需要为此确定一个时区,因为它在所有时区都不是相同的日期或相同的时间。

使用此格式化程序:

private static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

然后做:

    ZoneId zone = ZoneId.of("Pacific/Tarawa");
    ZonedDateTime dateTime = timestamp.atZoneSameInstant(zone);
    String formattedDateTime = dateTime.format(FORMATTER);
    System.out.println(formattedDateTime);

08.09.2021 21:40

链接

【讨论】:

  • 非常感谢您的回答 :)
猜你喜欢
  • 2013-11-13
  • 1970-01-01
  • 2018-07-02
  • 2017-11-22
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
相关资源
最近更新 更多