Java 将 yyyy-MM-dd HH:mm:ss.SSS zzz 格式的 Strin 转换为日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18350065/
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
Convert yyyy-MM-dd HH:mm:ss.SSS zzz format Strin to date object
提问by Aswathy
I have a GMT
formatted String value in yyyy-MM-dd HH:mm:ss.SSS zzz
我有一个GMT
格式化的字符串值yyyy-MM-dd HH:mm:ss.SSS zzz
eg: 2013-07-29 06:35:40:622 GMT.
I want to get it as a date object and convert it into IST
time zone.
eg: 2013-07-29 06:35:40:622 GMT.
我想将其作为日期对象并将其转换IST
为时区。
I did smthng like this... **
我确实是这样的...... **
String GMT = "2013-07-29 06:35:40:622 GMT";
DateFormat utcDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
utcDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
utcDateFormat.parse(GMT);
} catch (ParseException e) {
e.printStackTrace();
}**
bt gettng error as java.text.ParseException: Unparseable date: "2013-07-29 06:35:40:622 GMT"
bt gettng 错误为 java.text.ParseException:无法解析的日期:“2013-07-29 06:35:40:622 GMT”
回答by Vimal Bera
Try This.
尝试这个。
Date d=new Date("2013-07-29 06:35:40:622");
Date newDate=new Date(d.getYear(),d.getMonth(),d.getDate());
Time t=new Time(d.getHours()+5, d.getMinutes()+30, d.getSeconds());
回答by Net Dawg
Joda Time supports timezone conversion. http://www.joda.org/joda-time/userguide.html#Changing_TimeZone
Joda Time 支持时区转换。 http://www.joda.org/joda-time/userguide.html#Changing_TimeZone
回答by MadProgrammer
The main (problems) are that SimpleDateFormat
doesn't recognize the text GMT
, it's looking for a different format for the time zone.
主要(问题)是SimpleDateFormat
无法识别 text GMT
,它正在寻找不同的时区格式。
The other problem is your format doesn't match your text...
另一个问题是您的格式与您的文本不匹配...
Your String
is in the form of
你String
的形式是
2013-07-29 06:35:40:622 GMT
And your format is in the form of
你的格式是
yyyy-MM-dd HH:mm:ss.SSS zzz
^--------This is a naughty character...
The seconds and milliseconds are separated by different characters, this isn't going to help
秒和毫秒由不同的字符分隔,这无济于事
Now, if we fix the expected format with the actual format, this won't fix the issue with the time zone...but, fourtantly for us, DateFormat
provides a lenient
method, this allows us to "bend" the format rules a bit...
现在,如果我们用实际格式修复预期格式,这不会解决时区问题……但是,对于我们来说,DateFormat
提供了一种lenient
方法,这允许我们稍微“弯曲”格式规则。 ..
So, with all that in hand, you can try something like...
因此,有了所有这些,您可以尝试诸如...
String text = "2013-07-29 06:35:40:622 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
TimeZone gmt = TimeZone.getTimeZone("GMT");
sdf.setTimeZone(gmt);
sdf.setLenient(false);
try {
Date date = sdf.parse(text);
System.out.println(date);
System.out.println(sdf.format(date));
} catch (Exception e) {
e.printStackTrace();
}
Which outputs...
哪个输出...
Mon Jul 29 16:35:40 EST 2013
2013-07-29 06:35:40:622