【发布时间】:2020-03-05 19:39:39
【问题描述】:
我正在尝试将 PST 时间戳转换为纪元时间。
我尝试的第一种方法是通过将美国/洛杉矶作为输入时区来使用分区日期时间。
public static void changeStringDateFormatToEpoch(String oldDate, String format) throws ParseException {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
LocalDateTime dt = LocalDateTime.parse(oldDate, dtf);
ZonedDateTime zdtzone = dt.atZone(ZoneId.of("America/Los_Angeles"));
System.out.println("When date: "+ oldDate + " is in format "+ format + " --> " + zdtzone.toEpochSecond());
}
在此之后,我尝试运行下面的代码,它使用 SimpleDateFormat 来做同样的事情
public static void changeStringDateFormatToEpochSimpleDate(String oldDate, String format) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dt = sdf.parse(oldDate);
long epoch = dt. getTime();
System.out.println("When date: "+ oldDate + " is in format "+ format + " --> " + epoch);
}
两个输出是如何相似的,在第二种情况下,我什至没有指定输入日期的时区。
由于输入时区是 PST(America/Los Angeles),纪元时间不应该受到影响吗?
样本输入
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
class Scratch {
public static void main(String[] args) {
String date = "2019-11-27 04:32:41.000-0800"; //yyyy-MM-dd HH:mm:ss.SSSZ
String date2 = "2019-11-27 04:32:41"; // yyyy-MM-dd HH:mm:ss
try {
changeStringDateFormatToEpoch(date, "yyyy-MM-dd HH:mm:ss.SSSZ");
changeStringDateFormatToEpoch(date2, "yyyy-MM-dd HH:mm:ss");
changeStringDateFormatToEpochSimpleDate(date, "yyyy-MM-dd HH:mm:ss.SSSZ");
changeStringDateFormatToEpochSimpleDate(date2, "yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void changeStringDateFormatToEpoch(String oldDate, String format) throws ParseException {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
LocalDateTime dt = LocalDateTime.parse(oldDate, dtf);
ZonedDateTime zdtzone = dt.atZone(ZoneId.of("America/Los_Angeles"));
System.out.println("When date: "+ oldDate + " is in format "+ format + " --> " + zdtzone.toEpochSecond());
}
public static void changeStringDateFormatToEpochSimpleDate(String oldDate, String format) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dt = sdf.parse(oldDate);
long epoch = dt. getTime();
System.out.println("SimpleDate : When date: "+ oldDate + " is in format "+ format + " --> " + epoch);
}
}
输出
When date: 2019-11-27 04:32:41.000-0800 is in format yyyy-MM-dd HH:mm:ss.SSSZ --> 1574857961
When date: 2019-11-27 04:32:41 is in format yyyy-MM-dd HH:mm:ss --> 1574857961
SimpleDate : When date: 2019-11-27 04:32:41.000-0800 is in format yyyy-MM-dd HH:mm:ss.SSSZ --> 1574857961000
SimpleDate : When date: 2019-11-27 04:32:41 is in format yyyy-MM-dd HH:mm:ss --> 1574857961000
【问题讨论】:
-
您将什么
format值传递给这两种方法? JVM的默认时区是什么?请提供Minimal, Reproducible Example,例如而不是只显示String date2 = "2019-11-27 04:32:41";,而是向我们展示实际调用changeStringDateFormatToEpoch("2019-11-27 04:32:41", "...")以及你得到的输出。 -
您无法将
"2019-11-27 04:32:41"解析为ZonedDateTime,因此您的问题毫无意义。 -
我已经添加了示例函数调用和我得到的输出。 @OleV.V.我在太平洋标准时间工作。这就是为什么它在不指定时区的情况下转换为正确的纪元时间的原因吗?
-
@Andreas 你能解释一下为什么 ZonedDateTime 无法解析吗?
-
@MhnCodes 我认为错误信息说明了一切:
ZonedDateTime.parse("2019-11-27 04:32:41", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))throws DateTimeException: 无法获取 ZoneId,因为没有时区在输入字符串中指定,ZonedDateTime需要时区(因此得名)。 --- 我复制/粘贴了问题 as-is 中的代码以及方法调用,我得到了那个错误。您未能为您的输出提供 reproducible 示例。
标签: java java-8 simpledateformat java-time