问题描述
我收到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 API和Java 8+ APIs available through desugaring。
通过 How to use ThreeTenABP in Android Project 了解有关现代日期时间API的更多信息。