我有一个类似的方法
public static Long getTime(String date) throws ParseException {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
Date fd = f.parse(date);
long t = fd.getTime();
return t / 1000;
}
在使用 java 8 表示字符串“20279456456456--139900-17T11167:47:1488Z”时,它抛出异常,但在使用 java 17 时,它给出值“-6628509492807388”,知道发生了什么吗?
该类/方法已知有问题,已被弃用。 java.time 包引入了更精确的类。
在你的情况下,你可以使用这样的东西:
public static Long getTime(String date) throws ParseException {
Instant i = Instant.parse(date);
return i.getEpochSecond();
}