Java格式yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss [英] Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
问题描述
我想在yyyy-MM-dd'T'HH格式化日期:mm:ss.SSSz格式为yyyy-mm-dd HH:mm:ss,这应该很简单,但是我不能得到它的工作。
I'm trying to format a date in yyyy-MM-dd'T'HH:mm:ss.SSSz format to yyyy-mm-dd HH:mm:ss, which should be easy but I can't get it to work.
必须解析的日期格式为:2012-10-01T09:45:00.000 + 02:00
现在我使用这个简单的日期格式化程序进行格式化:
A date that has to be parsed is in the form of: 2012-10-01T09:45:00.000+02:00
Now i use this simple date formatter to format it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.FRANCE);
但这给出了类似于2012-10-01T09:45:00.000UTC + 00:00的输出
yet this gives an output similar to 2012-10-01T09:45:00.000UTC+00:00.
我还尝试使用yyyy-MM-dd'THH:mm:ss.SSSZ作为模式,yyyy-MM-ddHH: mm:ss后者以2012-10-01T09:45:00的形式返回日期,但是还没有。
I've also tried to use "yyyy-MM-dd'T'HH:mm:ss.SSSZ" as pattern and "yyyy-MM-ddHH:mm:ss". The latter returns a date in the form of 2012-10-01T09:45:00 close, but not there yet.
我认为子字符串T将会有点杂乱无章,没有理由创造开销,那么格式化这些日期的正确方法是什么?
I figured substringing the T away would be a bit messy and creates overhead for no reason, thus what would be the proper way to format these dates?
为了说明我想转换2012-10-01T09:45: 00.000 + 02:00进入2012-10-01 09:45:00
To illustrate I would like to convert 2012-10-01T09:45:00.000+02:00 into 2012-10-01 09:45:00
干杯!
推荐答案
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(time);
String formattedTime = output.format(d);
这个工作。你必须使用两个SimpleDateFormats,一个用于输入,另一个用于输出,但它会给你你想要的。
This works. You have to use two SimpleDateFormats, one for input and one for output, but it will give you just what you are wanting.
这篇关于Java格式yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!