8

I have the following date string: 2011-09-06T22:02:57-04:00. The problem is the timezone, -04:00. The Java7 docs say I can use XXX to magically match this timezone string: http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

The problem is that Groovy does not support the X character, presumably because it's not using JDK7 yet. The z character doesn't work because it isn't GMT-07:00, only -07:00 What is the easiest way to parse this timezone?

-tjw

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • What exactly are you trying to do? Get groovy to parse the date and pass it back to java? Or something within the groovy? Not clear exactly why you are trying to do what your doing – nuzz Sep 07 '11 at 04:27
  • 1
    The OP wants to parse an ISO8601 datetime string in Groovy. This is a good question, because the time zone suffix is not directly supported in the `SimpleDateFormat` class in the jar in the Groovy installation, I think. – Ray Toal Sep 07 '11 at 04:31
  • @Ray the `Z` and `z` patterns are supported, but not `X`, hence my problem. I know of no other straightforward way to parse this string. To be clear, any pre-7 Java programs would have this issue as well. – Travis Webb Sep 07 '11 at 04:35
  • 1
    @Travis, yes, that's true; I was trying to say that (by suffix I meant the ISO8601 offset suffix, not the Z). Anyway, answer is probably here: http://stackoverflow.com/questions/2375222/java-simpledateformat-for-time-zone-with-a-colon-seperator, if you are willing to go with Joda. – Ray Toal Sep 07 '11 at 04:53

2 Answers2

1

you can use DatatypeConverter.parseDateTime (jaxb support) to parse this date format.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

The simplest answer I can think of, is just to use 'Z'. The issue here is that -04:00 isn't recognised by the parser. So why not just run a regex prior to trying to convert it, looking for the final : and removing it.

nuzz
  • 647
  • 7
  • 18