gpt4 book ai didi

java - 在 Java 中解析日期格式

转载 作者:行者123 更新时间:2023-12-01 22:39:48 24 4
gpt4 key购买 nike

我正在尝试用 Java 将日期转换为 Unix 时间戳。我的日期示例为:“Fri Jun 05 15:53:07 PDT 2009”

我的代码是:

DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = dateFormat.parse(dateString );
long unixTime = (long) date.getTime()/1000;
System.out.println("Unix Time: " + unixTime );
double unixFloat = unixTime;

给定的错误是:

 Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Jun 05 15:53:07 PDT 2009"

什么是正确的模式?

最佳答案

您需要设置区域设置,某些情况下如果不设置区域设置您可能会遇到此类问题。

例如:

DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"
,Locale.US);

来自Java doc .

SimpleDateFormat also supports localized date and time pattern strings. In these strings, the pattern letters described above may be replaced with other, locale dependent, pattern letters. SimpleDateFormat does not deal with the localization of text other than the pattern letters; that's up to the client of the class.

关于java - 在 Java 中解析日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355650/

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