gpt4 book ai didi

java - 将datetimeString转换为毫秒,返回负值?

转载 作者:行者123 更新时间:2023-12-01 16:23:24 25 4
gpt4 key购买 nike

我不知道为什么会这样。我将这个“ 2020-05-05T09:30:00”日期时间字符串转换为毫秒,但转换后却得到负值。

我用于转换的方法:-

public static long getTimeInMilliSeconds(String dateTime) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
Date date = null;

try {
date = simpleDateFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}

return date.getTime();
}

最佳答案

您的问题是Getting negative value in date.gettime()的可能重复项

您可能需要设置TimeZone。

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));


希望这能为您解决。

关于java - 将datetimeString转换为毫秒,返回负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62209090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com