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.
DateTimeFormatter itself has numerous static constants like this. It is even recommended to store the (immutable) formatter in a static constant (for performance reasons because constructing a formatter can be quite expensive).
DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.
Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.
The DateTimeFormatter class is used to both parse and format dates according to specified Date and Time Patterns. Use parse(...) method to convert from String to Date/Time classes, use format(...) method to convert from Date/Time into String.
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
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]
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With