1

I have done some Googling and Stacking, but cannot find the necessary resources for any methods that can convert ISO-8601 format to Unix time in Java.

I am parsing NOAA's (National Oceanic Atmospheric Administration) predictions, and using XPath I am able to successfully parse that data. I would, however, like to convert the ISO-8601 i.e. 2013-10-31T14:00:00-07:00 date format to Unix time.

Are there any Java libraries that can do this?

What my code does:

for (int i = 0; i < tempHourlyResult.getLength(); i++) {
                writer.append(
                        hourlyResult.item(i).getNodeValue() + ","
                        + tempHourlyResult.item(i).getNodeValue()+ "," 
                        + dewPointResult.item(i).getNodeValue() + ","
                        + windSpeedResult.item(i).getNodeValue() +","
                        + relHumResult.item(i).getNodeValue() + "\n");
            }
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • "The ISO-8601 date format" actually narrows it down much less than you'd think! Do you mean you want a parser that understands anything that is compliant with the standard, or you just need to read a specific format? – Affe Oct 31 '13 at 18:32
  • Edited my original post to clarify =). Provided an example. – theGreenCabbage Oct 31 '13 at 18:36

1 Answers1

2

The java.util.Date class store time internally in a format very close to the POSIX time format (it uses milliseconds instead of seconds). So assuming you have your date in a String object called stringDate:

SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
long dateInMilli = dateParser.parse(stringDate).getTime();
long posixDate = dateInMilli/1000
Francis
  • 1,090
  • 7
  • 12
  • Excuse - what is `dateParser`? What I have currently is in the format of `relativeHumidityResult.item(i).getNodeValue()` which outputs to the ISO-8601 format. – theGreenCabbage Oct 31 '13 at 18:37
  • I edited for clarity, I admit it was not explicit enough – Francis Oct 31 '13 at 18:38
  • Thanks @Francis, however, what do I need to add to include the timezone as well? The format is -7 in my example. – theGreenCabbage Oct 31 '13 at 18:40
  • 1
    `yyyy-MM-ddTHH:mm:ssXXX` would include the timezone in all its full 8601 glory. – dcsohl Oct 31 '13 at 18:41
  • 2
    `Z` doesn't include the colon that the OP has in the example date. Shouldn't it be `XXX` per that API doc? – dcsohl Oct 31 '13 at 18:44
  • Oh you guys =). This is the one that worked `yyyy-MM-dd'T'HH:mm:ssXXX`. Using `Z` gave me an `unparseable` error. – theGreenCabbage Oct 31 '13 at 18:46
  • I confirm, just tested on my computer. However `SimpleDateFormat` does not seem to document `XXX`. In anycase I updated the answer. – Francis Oct 31 '13 at 18:51
  • 1
    Don't forget that `.getTime()` gives milliseconds, while POSIX is seconds. – Dmitri Oct 31 '13 at 18:52
  • Is it alright that I don't cast it to the `long` type? – theGreenCabbage Oct 31 '13 at 18:54
  • Very true Dmitri, thanks for the observation. I updated accordingly. – Francis Oct 31 '13 at 18:57
  • As I am doing something that requires an `i`, and I cannot cast to `long` every time the loop runs again, is it alright if I don't cast it to `long`? – theGreenCabbage Oct 31 '13 at 18:57
  • 1
    @Francis, it looks like `XXX` was added to the API in JRE 7 and is doc'd [here](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). – dcsohl Oct 31 '13 at 19:09
  • Francis and dcsohl you two are too cute! – theGreenCabbage Oct 31 '13 at 19:09
  • I don't understand what you mean by requiring an `i` and not casting to a `long`. Your `i` is separate from the result of this parsing business, isn't it? At any rate, if you do the division by 1000 to get seconds you should be OK casting it to an `int` until 2038. But I wouldn't advise doing this. – dcsohl Oct 31 '13 at 19:13