在 Joda 中将毫秒转换为 DateTime 格式时出现解析错误

Parsing error when converting milliseconds to a DateTime format in Joda

我有以下代码段,它根据格式将毫秒转换为日期。我正在使用 Joda-time 2.3。

final Long currentMs = new DateTime().getMillis());
DateTimeFormatter currentMsTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
DateTime dt = currentMsTimeFormat.parseDateTime(String.valueOf(currentMs));
System.out.println(dt.toString());

当我 运行 这个程序时,我得到以下错误:

java.lang.IllegalArgumentException: Invalid format: "1455711149006" is malformed at "9006"

其中 1455711149006currentMs 的值。

有一个 constructor for DateTime 从 EPOCH 开始需要几毫秒。所以你可能想要:

new DateTime(currentMs);