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
This returns the data in xml, with various measures of the stock (Symbol, name, last price, change, time stamp, etc).
I am having trouble creating a DateTimeFormatter in Java 8 to make the timestamp.
One example of a 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.
From the Documentation:
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'.
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.
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);
This works, but I am curious why (in place of the 'UTC'XXX) OOOO
doesn't work.
StringIndexOutOfBoundsException
when trying to parse a string where offset is given asGMT+02:00
at the end of the string. In Java 9 the same parsing works smoothly, which is why I suspect a bug in Java 8. – KistlerGMT-04:00
but I still cannot make it work withUTC-04:00
. Don’t know why. – Kistler