Reputation: 280
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.
Upvotes: 6
Views: 2964
Reputation: 79425
Do not use a fixed text (e.g. 'UTC'
) for the timezone because that approach may fail for other locales.
You can parse your Date-Time string using the pattern, E MMM d H:m:s VV u
.
Demo:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Fri Jul 18 15:59:00 UTC-04:00 2014";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d H:m:s VV u", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
System.out.println(zdt);
}
}
Output:
2014-07-18T15:59-04:00[UTC-04:00]
Learn more about the modern Date-Time API from Trail: Date Time.
Upvotes: 1
Reputation: 280
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
is EEE MMM d HH:mm:ss 'UTC'XXX yyyy
Upvotes: 1