带有冒号分隔符的时区的 Java SimpleDateFormat? [英] Java SimpleDateFormat for time zone with a colon separator?
问题描述
我的日期格式如下:2010-03-01T00:00:00-08:00
我已经抛出以下 SimpleDateFormats 来解析它:
I have thrown the following SimpleDateFormats at it to parse it:
private static final SimpleDateFormat[] FORMATS = {
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"), //ISO8601 long RFC822 zone
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"), //ISO8601 long long form zone
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), //ignore timezone
new SimpleDateFormat("yyyyMMddHHmmssZ"), //ISO8601 short
new SimpleDateFormat("yyyyMMddHHmm"),
new SimpleDateFormat("yyyyMMdd"), //birthdate from NIST IHE C32 sample
new SimpleDateFormat("yyyyMM"),
new SimpleDateFormat("yyyy") //just the year
};
我有一个使用这些格式的便捷方法,如下所示:
I have a convenience method that uses those formats like so:
public static Date figureOutTheDamnDate(String wtf) {
if (wtf == null) {
return null;
}
Date retval = null;
for (SimpleDateFormat sdf : FORMATS) {
try {
sdf.setLenient(false)
retval = sdf.parse(wtf);
System.out.println("Date:" + wtf + " hit on pattern:" + sdf.toPattern());
break;
} catch (ParseException ex) {
retval = null;
continue;
}
}
return retval;
}
它似乎符合 yyyyMMddHHmm
模式,但返回日期为 Thu Dec 03 00:01:00 PST 2009
.
It seems to hit on the pattern yyyyMMddHHmm
but returns the date as Thu Dec 03 00:01:00 PST 2009
.
解析这个日期的正确模式是什么?
What is the correct pattern to parse this date?
更新:我不需要时区解析.我不希望有时间敏感问题在区域之间移动,但是我将如何解析-08:00"区域格式????
UPDATE: I don't NEED the time zone parsing. I don't anticipate having time sensitive issues moving between zones, but how would I get the "-08:00" zone format to parse????
单元测试:
@Test
public void test_date_parser() {
System.out.println("
test_date_parser");
//month is zero based, are you effing kidding me
Calendar d = new GregorianCalendar(2000, 3, 6, 13, 00, 00);
assertEquals(d.getTime(), MyClass.figureOutTheDamnDate("200004061300"));
assertEquals(new GregorianCalendar(1950, 0, 1).getTime(), MyClass.figureOutTheDamnDate("1950"));
assertEquals(new GregorianCalendar(1997, 0, 1).getTime(), MyClass.figureOutTheDamnDate("199701"));
assertEquals(new GregorianCalendar(2010, 1, 25, 15, 19, 44).getTime(), MyClass.figureOutTheDamnDate("20100225151944-0800"));
//my machine happens to be in GMT-0800
assertEquals(new GregorianCalendar(2010, 1, 15, 13, 15, 00).getTime(),MyClass.figureOutTheDamnDate("2010-02-15T13:15:00-05:00"));
assertEquals(new GregorianCalendar(2010, 1, 15, 18, 15, 00).getTime(), MyClass.figureOutTheDamnDate("2010-02-15T18:15:00-05:00"));
assertEquals(new GregorianCalendar(2010, 2, 1).getTime(), MyClass.figureOutTheDamnDate("2010-03-01T00:00:00-08:00"));
assertEquals(new GregorianCalendar(2010, 2, 1, 17, 0, 0).getTime(), MyClass.figureOutTheDamnDate("2010-03-01T17:00:00-05:00"));
}
单元测试的输出:
test_date_parser
Date:200004061300 hit on pattern:yyyyMMddHHmm
Date:1950 hit on pattern:yyyy
Date:199701 hit on pattern:yyyyMM
Date:20100225151944-0800 hit on pattern:yyyyMMddHHmmssZ
Date:2010-02-15T13:15:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-02-15T18:15:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-03-01T00:00:00-08:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
Date:2010-03-01T17:00:00-05:00 hit on pattern:yyyy-MM-dd'T'HH:mm:ss
推荐答案
JodaTime 的 DateTimeFormat
到救援:
JodaTime's DateTimeFormat
to rescue:
String dateString = "2010-03-01T00:00:00-08:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime); // 2010-03-01T04:00:00.000-04:00
(toString()
中的时间和时区差异只是因为我在 GMT-4 并且没有明确设置语言环境)
(time and timezone difference in toString()
is just because I'm at GMT-4 and didn't set locale explicitly)
如果你想以 java.util.Date
结束,只需使用 DateTime#toDate()
:
If you want to end up with java.util.Date
just use DateTime#toDate()
:
Date date = dateTime.toDate();
等待 JDK7 (JSR-310) JSR-310,引用实现被称为ThreeTen(希望它能让如果您想在标准 Java SE API 中使用更好的格式化程序,请将其转换为 Java 8.当前的 SimpleDateFormat
确实不吃时区符号中的冒号.
Wait for JDK7 (JSR-310) JSR-310, the referrence implementation is called ThreeTen (hopefully it will make it into Java 8) if you want a better formatter in the standard Java SE API. The current SimpleDateFormat
indeed doesn't eat the colon in the timezone notation.
更新:根据更新,您显然不需要时区.这应该适用于 SimpleDateFormat
.只需在模式中省略它(Z
)即可.
Update: as per the update, you apparently don't need the timezone. This should work with SimpleDateFormat
. Just omit it (the Z
) in the pattern.
String dateString = "2010-03-01T00:00:00-08:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateString);
System.out.println(date); // Mon Mar 01 00:00:00 BOT 2010
(根据我的时区是正确的)
(which is correct as per my timezone)
这篇关于带有冒号分隔符的时区的 Java SimpleDateFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!