用Java解析日期格式 [英] Parsing Date Formats in Java

查看:909
本文介绍了用Java解析日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert Dates into Unix Timestamps in Java. Examples for my dates are: "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"

什么是正确的模式?

推荐答案

你需要设置区域设置,如果您未设置可能遇到此类问题的区域设置,则有些情况。

You need to set Locale, Some cases if you are not set the locale you may face this kind of issues.

例如:

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

来自 Java doc

SimpleDateFormat还支持本地化的日期和时间模式
字符串。在这些字符串中,上面描述的模式字母可以是
,用其他依赖于语言环境的模式字母替换。
SimpleDateFormat不处理其他
的文本本地化而不是模式字母;这取决于班级的客户。

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解析日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆