java Twitter 日期无法解析?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4521715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter date unparseable?
提问by Andreas Bauer
I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!
我想将 Twitter 响应中的日期字符串转换为 Date 对象,但我总是收到 ParseException 并且我看不到错误!?!
Input string: Thu Dec 23 18:26:07 +0000 2010
输入字符串:2010 年 12 月 23 日星期四 18:26:07 +0000
SimpleDateFormat
Pattern:
SimpleDateFormat
图案:
EEE MMM dd HH:mm:ss ZZZZZ yyyy
Method:
方法:
public static Date getTwitterDate(String date) {
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
twitterDate = sf.parse(date);
} catch (Exception e) {}
return twitterDate;
}
I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZbut that gives the same result.
我也试过这个:http: //friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ但这给出了相同的结果。
I use Java 1.6 on Mac OS X.
我在 Mac OS X 上使用 Java 1.6。
Cheers,
干杯,
Andi
和我
回答by Michael Konietzka
Your format string works for me, see:
您的格式字符串对我有用,请参阅:
public static Date getTwitterDate(String date) throws ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
return sf.parse(date);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));
}
Output:
输出:
Fri Dec 03 18:26:07 GMT 2010
格林威治标准时间 2010 年 12 月 3 日星期五 18:26:07
UPDATE
更新
Roland Illig is right: SimpleDateFormat is Locale dependent, so
just use an explicit english Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
Roland Illig 是对的:SimpleDateFormat 是 Locale 相关的,所以只需使用明确的英文 Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
回答by David
This works for me ;)
这对我有用;)
public static Date getTwitterDate(String date) throws ParseException
{
final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
sf.setLenient(true);
return sf.parse(date);
}
回答by Roland Illig
Maybe you are in a locale where ‘Tue‘ is not a recognized day of week, for example German. Try to use the ‘SimpleDateFormat‘ constructor that accepts a ‘Locale‘ as a parameter, and pass it ‘Locale.ROOT‘.
也许您所在的区域设置“星期二”不是公认的星期几,例如德语。尝试使用接受“Locale”作为参数的“SimpleDateFormat”构造函数,并将其传递给“Locale.ROOT”。
回答by Guillaume
You should not have ZZZZZ
but only Z
for the timezone.
你不应该有ZZZZZ
但只Z
针对时区。
See samples in http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.htmlfor more information.
有关更多信息,请参阅http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html 中的示例。
EEE, d MMM yyyy HH:mm:ss Z
> Wed, 4 Jul 2001 12:08:56 -0700
EEE, d MMM yyyy HH:mm:ss Z
> Wed, 4 Jul 2001 12:08:56 -0700
回答by Mark Hansen
SimpleDateFormat is not thread safe. "EEE MMM dd HH:mm:ss ZZZZZ yyyy" was working in our application, but failing in a small percentage of cases. We finally realized that the issue was coming from multiple threads using the same instance of SimpleDateFormat.
SimpleDateFormat 不是线程安全的。“EEE MMM dd HH:mm:ss ZZZZZ yyyy”在我们的应用程序中工作,但在一小部分情况下失败。我们终于意识到问题来自使用相同 SimpleDateFormat 实例的多个线程。
Here is one workaround: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html
这是一种解决方法:http: //www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html
回答by Amal Dev S I
Function for convert Twitter Date :
转换 Twitter 日期的函数:
String old_date="Thu Jul 05 22:15:04 GMT+05:30 2012";
private String Convert_Twitter_Date(String old_date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
SimpleDateFormat old = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",Locale.ENGLISH);
old.setLenient(true);
Date date = null;
try {
date = old.parse(old_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sdf.format(date);
}
The output format like : 05-Jul-2012 11:54:30
输出格式如:05-Jul-2012 11:54:30
回答by Nithin Chandran
in C#
you can do like this
在C#
你可以做这样的
DateTime date = DateTime.ParseExact(dt, "ddd MMM dd HH:mm:ss +0000 yyyy", CultureInfo.InvariantCulture);
DateTime date = DateTime.ParseExact(dt, "ddd MMM dd HH:mm:ss +0000 yyyy", CultureInfo.InvariantCulture);