2

I get this date string from my API : "2015-12-07T14:11:15.596Z"

But this date is in UTC format and I want to convert it in local time, how can I do it ?

I tried this :

try
{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    return simpleDateFormat.parse(this.created_at);
}
catch (ParseException e)
{
    Log.e("Error Date at Whisp", e.getMessage());
    return null;
}

But it return me this error :

Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 10)
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
JohnyBro
  • 341
  • 3
  • 14
  • Please search StackOverflow before posting. These topics of parsing date-time strings, `ParseException`/”Unparseable“, and adjusting time zones, have been covered many hundreds of times already. – Basil Bourque Dec 08 '15 at 08:12
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 17 '17 at 01:07

2 Answers2

3

your Date Format pattern is wrong. Change to:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");

For more informations see the javadoc of SimpleDateFormat

Jens
  • 67,715
  • 15
  • 98
  • 113
  • I tried this but give me that "Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 11)", he don't like the "T" I think – JohnyBro Dec 08 '15 at 08:15
  • @JohnyBro Youhave the siglequotes arround the 'T'? – Jens Dec 08 '15 at 08:17
  • Yes, "yyyy-MM-dd'T' HH:mm:ss.S'Z'" – JohnyBro Dec 08 '15 at 08:18
  • @JohnyBro Sorry my fault. remove the blank after the second single quote – Jens Dec 08 '15 at 08:19
  • Thx it work, I got this "Mon Dec 07 09:11:15 EST 2015" but how can I get something like "07.12 09:11" ? – JohnyBro Dec 08 '15 at 08:23
  • FYI, the troublesome old date-time classes such as `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 17 '17 at 01:06
1

The T and the Z are not in your mask

Either

created_at = created_at.replace ("T", "").replace ("Z", "");

or modifiy your mask

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64