3

I am trying to parse/validate the date 2013-06-19T12:00-05:00 using Java 6

I have tried several patterns, including following:

yyyy-MM-dd'T'HH:mmz
yyyy-MM-dd'T'HH:mmZ
yyyy-MM-dd'T'HH:mm'Z'
yyyy-MM-dd'T'HH:mm Z
yyyy-MM-dd'T'HH:mm z
yyyy-MM-dd'T'HH:mm'z'
yyyy-MM-dd'T'HH:mm-Z
yyyy-MM-dd'T'HH:mm-z
yyyy-MM-dd'T'hh:mm:ssZ
yyyy-mm-DD'T'hh:mm:ssZ
yyyy-MM-DD'T'hh:mm:ssZ
yyyy-MM-dd'T'HH:mm:ssZ
yyyy-MM-dd'T'HH:mm:ssz

but keep getting ParseException.

What would be the appropriate format/pattern for parsing 2013-06-19T12:00-05:00?

Thanks.

Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37
  • If you don't know the format of date/time representation you have, how can you expect to parse it? None of your patterns seems to match with the example. As K.C. said it can be a interval. – Diego C Nascimento Dec 23 '13 at 12:58
  • Please read [this](http://stackoverflow.com/a/2202300/1237297) as well. – jagmohan Dec 23 '13 at 13:19

5 Answers5

2
    String dateString = "2013-06-19T12:00-05:00";
    String pattern = "yyyy-MM-dd'T'HH:mmZ";
    DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
    DateTime dateTime = dtf.parseDateTime(dateString);
    Date ans=dateTime.toDate();
    System.out.println(ans);
Dima
  • 8,586
  • 4
  • 28
  • 57
  • 1
    I believe this is only available for Java 7. – jagmohan Dec 23 '13 at 13:27
  • yes, its the same in java 6 http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Dima Dec 23 '13 at 13:42
  • I already tried `X` but it gives `IllegalArgumentException`. As @singh.jagmohan said `X` is only available in Java 7 – Mugoma J. Okomba Dec 23 '13 at 14:14
  • I am still getting `java.lang.IllegalArgumentException: Illegal pattern character 'X'`. This is my VM: `java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.6) (6b22-1.10.6-0ubuntu1) OpenJDK Client VM (build 20.0-b11, mixed mode, sharing)` – Mugoma J. Okomba Dec 24 '13 at 02:25
  • Down voted because the solution does not work and should not work per the documentation. Java 6 docs show Z is for time zone in format of "-0800" but your example has time zone "-05:00" <-- note the colon between the 5 and 0. – Benrobot Nov 02 '17 at 15:43
2

I would suggest that you use the excellent Joda-Time library to do this, specifically the parse(String str) method of the DateTime class, which will parse your example date using the default ISODateTimeFormat.dateTimeParser()

The JavaDoc for DateTime.parse(String str) is at http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#parse%28java.lang.String%29 and you can read more about Joda-Time at http://www.joda.org/joda-time/

azordi
  • 176
  • 3
  • Using `Joda-Time` seems to be an overkill. In `Java 6` the problem can be resolved simply by adding `timezone`. Please see my answer using `regex` to do this. – Mugoma J. Okomba Dec 29 '13 at 23:09
  • Joda-Time provides a **much** nicer API to use than the standard Java 6 date/time API, so I would always use it in preference to the standard API. Joda-Time also has further benefits such as the ability to control what the 'current' time is, which is very useful for unit testing time-sensitive code. – azordi Dec 30 '13 at 08:31
1

Use simpledateformat with pattern like "yyyy-MM-dd'T'HH-mm:ss:SS"

JCRaja
  • 21
  • 4
0

The Joda-Time 2.3 library is already built to handle that variation of ISO 8601 format. No need to create a formatter.

I arbitrarily chose Montréal Québec as a time zone because of your -05:00 time zone offset. You may rather work with UTC/GMT date-times in which case you can pass a pre-defined instance: DateTimeZone.UTC.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

String dateTimeString = "2013-06-19T12:00-05:00";
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" ); // Or for UTC/GMT, use: DateTimeZone.UTC

DateTime dateTime = new DateTime( dateTimeString, timeZone );

Dump to console…

System.out.println( "dateTimeString: " + dateTimeString );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime in UTC/GMT: " + dateTime.toDateTime( DateTimeZone.UTC ) );

When run…

dateTimeString: 2013-06-19T12:00-05:00
dateTime: 2013-06-19T13:00:00.000-04:00
dateTime in UTC/GMT: 2013-06-19T17:00:00.000Z

Note the one hour difference because Montréal Québec was in Daylight Saving Time (DST) on that date.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

The date 2013-06-19T12:00-05:00 can't be parsed under Java 6 in current form. On other hand using Joda-Time seems to be an overkill.

To solve problem I added timezone to the date:

public Date extractDate(String dateStr) {

    Pattern aPattern = Pattern.compile("\\d+([+-]\\d+:\\d+)$", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
    Matcher matcher = aPattern.matcher(dateStr);

    if(matcher.find()){
        String timezone = matcher.group(1);
        System.out.println("timezone:" + timezone);
        dateStr = StringUtils.replace(dateStr, timezone, "GMT" + timezone);
    }

    Date date = null;
    String [] datePatterns = {"yyyy-MM-dd'T'HH:mmZ"};
    try {
        date = DateUtils.parseDateStrictly(dateStr, datePatterns);
    }
    catch (Exception except) {          
        except.printStackTrace();           
    }
    return date;
}
Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37
  • How can you say that a one-liner with Joda-Time is overkill and then propose a hand-written function with over a dozen lines? `DateUtils.parseDateStrictly()` is part of an external library - Apache Commons - why do you feel that making use of that is not overkill but that using Joda-Time is? With Joda-Time your entire function can be replaced with just `Date date = DateTime.parse(dateStr).toDate();` – azordi Dec 30 '13 at 08:48
  • @azordi apache commons is a library that I use extensively. It's not limited to handling dates. I didn't want to pull a whole 3rd party library that only deals with date. I would have done that if I was writing a calendar application. – Mugoma J. Okomba Dec 30 '13 at 09:39