0

In Java 6, I have a date string that looks like

2011-11-28T21:00:00Z

How would I get a java.util.Date out of the above String, given the "T" and "Z" characters don't really mean anything? (You can assume the timezone is the default time zone of the machine that is running this Java code).

arserbin3
  • 6,010
  • 8
  • 36
  • 52
Dave
  • 15,639
  • 133
  • 442
  • 830
  • 2
    Did you try simpledateformat? – kosa Feb 28 '12 at 22:19
  • But the Z character *does* mean something. It means that it's a [UTC time](http://en.wikipedia.org/wiki/ISO_8601#UTC). – Mark Byers Feb 28 '12 at 22:19
  • possible duplicate of [Converting ISO8601-compliant String to java.util.Date](http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date) – Tomasz Nurkiewicz Feb 28 '12 at 22:22
  • See http://en.wikipedia.org/wiki/ISO_8601#UTC, The 'Z' time means zero UTC offset. This is a duplicate. – The Nail Feb 28 '12 at 22:34

3 Answers3

1

Assuming you want to ignore Z, do this:

String s ="2011-11-28T21:00:00Z";
Date d = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")).parse(s);
Andre
  • 3,874
  • 3
  • 35
  • 50
0

You can use SimpleDateFormat to do this (for parsing or formatting) - see the Javadocs, which give a case similar to yours as an example:

pattern: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 
result: 2001-07-04T12:08:56.235-0700
DNA
  • 42,007
  • 12
  • 107
  • 146
0

Assuming T and Z do not mean anything. You can remove them from T and Z from given string then you can use following code

String sampleDate = "2011-11-28 21:00:00";

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat.parse(sampleDate);
        System.out.println("Date: "+date);
JProgrammer
  • 1,135
  • 1
  • 10
  • 27