53

I have a string in standard ISO 8601 format that contains the date/time returned from a web service like so:

String dtStart = "2010-10-15T09:27:37Z"

How do I get this into an object such as Time or Date? I initially want to output it in a different format, but will need to do other stuff with it later (i.e. maybe use in a different format).

Cheers

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
neildeadman
  • 3,974
  • 13
  • 42
  • 55

4 Answers4

131
String dtStart = "2010-10-15T09:27:37Z";  
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    e.printStackTrace();  
}

This is what you are looking for. There is existing post about this problem.

serge.s
  • 21
  • 6
Seitaridis
  • 4,459
  • 9
  • 53
  • 85
  • 1
    Thanks very much, I did look on here and via Google, but never found anything that did what I wanted – neildeadman Oct 15 '10 at 10:59
  • 1
    To all, supported date formats are: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – JWL Aug 28 '14 at 06:50
  • 3
    If you are having milliseconds too then this format worked for me `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'` – Manohar Nov 03 '21 at 08:37
7

This question was asked in 2010, and back then it was correct that either SimpleDateFormat or Joda-Time would be the tools you should use. It’s quite a while ago now. Today use

    Instant iStart = Instant.parse(dtStart);

Yes, it’s this simple. Your string is in ISO 8601 format, and the classes from java.time, the modern Java date and time API, parse ISO 8601 without any explicit formatter. Instant is just one of those classes.

Edit: Question: requires android API 26 - what about supporting older versions?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • requires android API 26 - what about supporting older versions? – TootsieRockNRoll Dec 19 '19 at 09:51
  • @ElJazouli Thank you for the question. I tried to cover that already, but I am aware that many readers miss this part. How can I make it clearer? I edited the relevant header, but maybe that’s not enough. – Ole V.V. Dec 19 '19 at 17:03
2

You can use Java's SimpleDateFormat parse method or use JodaTime's DateTimeFormat to create a DateTimeFormatter and parse to a DateTime object accordingly

Jeroen Rosenberg
  • 4,552
  • 3
  • 27
  • 39
0

If you use current java.time or threetenbp simply parse it like this:

ZonedDateTime dt = ZonedDateTime.parse(dtStart);

Now you can access date and time values such as year, month, hour, etc.

pram
  • 1,484
  • 14
  • 17