0

I have a string 2014-05-05T09:14:52.150Z received from server. I have to convert it to get the current system time How can i do that any suggestions. What the 'T' and 'Z' in the date string signifies.

  • 2
    The 'T' is just a separator, the 'Z' indicates UTC time zone. Have a look at SimpleDateFormat. – Henry May 05 '14 at 09:30
  • 1
    This is standard http://en.wikipedia.org/wiki/ISO_8601 date time format. I would use the Java 8 `ZonedDateTime.parse("2014-05-05T09:14:52.150Z")` – Peter Lawrey May 05 '14 at 09:47
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Oleg Estekhin May 05 '14 at 09:55

2 Answers2

1

This is XML Schema dateTime format http://www.w3schools.com/schema/schema_dtypes_date.asp you can parse it like this

Date date = DatatypeFactory.newInstance().newXMLGregorianCalendar(str)
.toGregorianCalendar().getTime();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
-1

If you just need to get rid of the 'T' and 'Z' charachters you can use

String serverString = "2014-05-05T09:14:52.150Z";
serverString = serverString.replaceAll("T", "");
serverString = serverString.replaceAll("Z", "");

Check the String Java API.

Gliptal
  • 350
  • 1
  • 10
  • From what I gathered in the question he needs to operate on the string externally from the server. You are right on the "not changed" part though, my bad. – Gliptal May 05 '14 at 09:52
  • Still not clear on how removing those letters helps. – Peter Lawrey May 05 '14 at 09:57
  • How will you separate the days from the hours if you remove the T? How will you deal with the timezone correctly if you remove the Z? How will you actually make this into a Date (or Calendar, or any instance of any other date-like class) using just the String API? – Dawood ibn Kareem May 05 '14 at 10:03