将时间戳记日期转换为Unix时间戳记

时间:2020-06-29 06:09:53

标签: java timestamp unix-timestamp

我有这样的数据 我从UTC时间戳记得到的日期是:2020-06-29 05:31:58.153

 LocalDateTime timestampasstring = message.getHeader().getUtcTimeStamp( SendingTime.FIELD);
 Timestamp timestamp = Timestamp.from(timestampasstring.toInstant(ZoneOffset.UTC));
 System.out.println(timestamp);
 String timestampstrings = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
 String timestampstrings2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);

我需要获取类似2020-06-29 05:31:58的时间戳记 并转换成这样的unix时间戳1593408718 如何转换?

3 个答案:

答案 0 :(得分:0)

阅读了您的问题后,我假设您的方法message.getHeader().getUtcTimeStamp(SendingTime.FIELD)返回了表示String或{格式的UTC时间戳的LocalDateTime(不是2020-06-29 05:31:58.153) {1}}。

由于(显示的)日期时间的格式不同(分别,日期时间的精度不同),因此您必须使用能够处理可选毫秒({ {1}}。

您可以按以下方式使用它:

2020-06-29 05:31:58

对于仅等于秒的日期时间,此输出当然是不同的:

  1. DateTimeFormatter纪元
  2. yyyy-MM-dd HH:mm:ss[.SSS]纪元

如果您调用public static void main(String[] args) { // receive the result from your message, this is just an example String utcTimestamp = "2020-06-29 05:31:58"; // create a ZonedDateTime by parsing the String to a LocalDateTime and adding a time zone ZonedDateTime zdt = LocalDateTime.parse(utcTimestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]")) .atZone(ZoneId.of("UTC")); // then get the epoch milliseconds / unix timestamp long millis = zdt.toInstant().toEpochMilli(); // and print the result System.out.println(zdt + " ==> " + millis + " epoch millis"); } 而不是转换2020-06-29T05:31:58Z[UTC] ==> 1593408718000(并稍微调整输出),则两个示例将获得相同的值:

2020-06-29T05:31:58.153Z[UTC] ==> 1593408718153

输出:

  1. long seconds = zdt.toEpochSeconds()
  2. toInstant().toEpochMillis

如果您的方法确实返回了public static void main(String[] args) { // receive the result from your message, this is just an example String utcTimestamp = "2020-06-29 05:31:58.153"; // create a ZonedDateTime by parsing the String to a LocalDateTime and adding a time zone ZonedDateTime zdt = LocalDateTime.parse(utcTimestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS]")) .atZone(ZoneId.of("UTC")); // then get the epoch seconds long seconds = zdt.toEpochSecond(); // and print the result System.out.println(zdt + "\t==>\t" + seconds + " epoch seconds"); } ,则可以简单跳过转换并编写

2020-06-29T05:31:58Z[UTC] ==> 1593408718 epoch seconds

答案 1 :(得分:0)

如果您的getUtcTimeStamp方法足以返回一个LocalDateTime,那么您就已经准备就绪了。使用Instant转换为ldt.atOffset(ZoneOffset.UTC)(如果您的getUtcTimeStamp应该已经知道您使用的是UTC,那么实际上应该为您执行此操作),然后只需致电toEpochSecond()(或{{1 }}(如果需要毫秒部分,但只显示了整秒)。

答案 2 :(得分:0)

其他答案都很好。这是我的变体。首先声明用于解析的格式化程序:

private DateTimeFormatter timestampFormatter = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .toFormatter();

请考虑声明格式化程序static和/或final。使用此格式化程序,我可以做到:

    String timestampAsString = "2020-06-29 05:31:58.153";
    
    OffsetDateTime dateTime = LocalDateTime
            .parse(timestampAsString, timestampFormatter)
            .atOffset(ZoneOffset.UTC);
    long unixTimestamp = dateTime.toEpochSecond();
    
    System.out.println(unixTimestamp);

输出是您要的:

1593408718

格式化程序的优点是它既可以接受2020-06-29 05:31:58.153也可以接受2020-06-29 05:31:58,也就是说,既可以接受时间,也可以不包含秒,也可以是1到9个小数。在格式化程序中重复使用ISO_LOCAL_TIME可以为我们买这个。