在Java中将ISO 8601时间戳字符串转换为纪元秒

问题描述

我收到ISO 8601日期时间格式2020-11-03T15:23:24.388Z的字符串。在Java中将其转换为时代秒的最佳方法是什么?

解决方法

使用see here for the example,您可以如下所示进行操作:

 List<Long>nex= new ArrayList<>();
for(int i=0;i<result.size();i++){
            if(Long.valueOf(result.get(i))>n){
                nex.add(Long.valueOf(result.get(i)));
            }
        }

输出:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2020-11-03T15:23:24.388Z");
        long seconds = instant.getEpochSecond();
        System.out.println(seconds);
    }
}

如果您正在为自己的Android项目执行此操作,并且您的Android API级别仍不符合Java-8,请选中modern date-time APIJava 8+ APIs available through desugaring

通过 How to use ThreeTenABP in Android Project 了解有关现代日期时间API的更多信息。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...