DateTimeFormatter模式有问题 [英] DateTimeFormatter trouble with a pattern

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

问题描述

我正在编写一个股票程序(到目前为止)通过以下请求从Markit on Demand获取数据:

I am writing a stock program that (so far) gets the data from "Markit on Demand" through a request such as this:

http://dev.markitondemand.com/Api/v2/Quote/xml?symbol=aapl

这将返回xml中的数据,其中包含各种库存量(符号,名称,最后价格,更改,时间戳等)。

This returns the data in xml, with various measures of the stock (Symbol, name, last price, change, time stamp, etc).

我在Java 8中创建一个DateTimeFormatter来制作时间戳时遇到了麻烦。

I am having trouble creating a DateTimeFormatter in Java 8 to make the timestamp.

时间戳的一个例子:

Fri Jul 18 15:59:00 UTC-04:00 2014

到目前为止我的模式如下:

So far the pattern I have is as follows:

EEE MMM d HH:mm:ss OOOO yyyy

因为我相信你们中的一些人可以发现,我在抵消时遇到了麻烦。

As I'm sure some of you can spot, I am having trouble with the offset.

来自文档:


偏移X和x:这会格式化偏移基于模式字母的数量。一个字母仅输出小时,例如'+01',除非分钟非零,在这种情况下分钟也输出,例如'+0130'。两个字母输出小时和分钟,没有冒号,例如'+0130'。三个字母输出小时和分钟,带有冒号,例如'+01:30'。四个字母输出小时和分钟以及可选秒,没有冒号,例如'+013015'。五个字母输出小时和分钟,可选秒输出冒号,例如'+01:30:15'。六个或更多字母抛出IllegalArgumentException。当要输出的偏移量为零时,模式字母X(大写)将输出Z,而模式字母x(小写)将输出+00,+ 0000或+00 :00'。

Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.

偏移O:根据模式字母的数量格式化本地化偏移。一个字母输出本地化偏移的缩写形式,即本地化偏移文本,例如GMT,小时不带前导零,可选2位数分钟,第二个非零,和冒号,例如'GMT + 8 。四个字母输出完整形式,这是本地化的偏移文本,例如'GMT,具有2位小时和分钟字段,可选的第二字段(如果非零)和冒号,例如'GMT + 08:00'。任何其他字母数量都会抛出IllegalArgumentException。

Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'. Four letters outputs the full form, which is localized offset text, such as 'GMT, with 2-digit hour and minute field, optional second field if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters throws IllegalArgumentException.

偏移Z:这会根据模式字母的数量格式化偏移量。一个,两个或三个字母输出小时和分钟,没有冒号,例如'+0130'。当偏移量为零时,输出将为+0000。四个字母输出完整形式的局部偏移,相当于Offset-O的四个字母。如果偏移为零,则输出将是相应的本地化偏移文本。五个字母输出小时,分钟,可选第二个,如果非零,则使用冒号。如果偏移量为零,则输出Z。六个或更多字母抛出IllegalArgumentException。

Offset Z: This formats the offset based on the number of pattern letters. One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset text if the offset is zero. Five letters outputs the hour, minute, with optional second if non-zero, with colon. It outputs 'Z' if the offset is zero. Six or more letters throws IllegalArgumentException.



// String rawDate = Fri Jul 18 15:59:00 UTC-04:00 2014   
DateTimeFormatter PARSER_PATTERN = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss 'UTC'XXX yyyy");
ZonedDateTime timeStamp = ZonedDateTime.parse(rawDate, PARSER_PATTERN);

这有效,但我很好奇为什么(代替'UTC'XXX) OOOO 不起作用。

This works, but I am curious why (in place of the 'UTC'XXX) OOOO doesn't work.

推荐答案

我决定使用字符串'UTC'因为时间戳总是以UTC + 00:00的形式给出。

I decided to use a String 'UTC' because the timestamp is always given in the form of "UTC+00:00".

我想出的最终模式是为了匹配分区日期时间:

The final pattern I came up with to match the zoned date time:

Fri Jul 18 15:59:00 UTC-04:00 2014

EEE MMM d HH:mm:ss'UTC'XXX yyyy

这篇关于DateTimeFormatter模式有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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