0

I am getting date time from rest api as like this "2020-02-13T16:57:13.04 . How can i convert in java for android? So that i can use only date or time separately?

I have tried by this way

String dateStr = rowsArrayList.get(position).getDisplayDate();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
try {
    date = df.parse(dateStr);
    df.setTimeZone(TimeZone.getDefault());
    String formattedDate = df.format(date);
    Log.d("testDate",formattedDate);

} catch (ParseException e) {
    e.printStackTrace();
    Log.d("testDate",e.toString());
}

But getting the error

java.text.ParseException: Unparseable date: "2020-02-13T16:57:13.04"

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
arifhasnat
  • 93
  • 1
  • 2
  • 10
  • Have you tried anything in code? – Md. Asaduzzaman Feb 18 '20 at 08:17
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Feb 18 '20 at 16:50

4 Answers4

2

Use yyyy-MM-dd'T'HH:mm:ss.SS as format instead of yyyy-MM-dd'T'HH:mm:ss'Z'

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS", Locale.ENGLISH);

java.time

You can also achieve this with java.time, the modern Java date and time API because the old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues:

String sourceDateTime = "2020-02-13T16:57:13.04";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS").withZone(DateTimeZone.UTC);

LocalDateTime localDateTime = LocalDateTime.parse(sourceDateTime, dateTimeFormatter);

LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();

System.out.println(localDate.toString() + " -> " + localTime.toString());
//Output should be: 2020-02-13 -> 16:57:13.040

Question: Can I use java.time on Android?

  • From Android 8.0 (API level 26) the modern API comes built-in.
  • For older android version, you can use ThreeTenABP, Android edition of ThreeTen Backport
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
2
  1. SimpleDateFormat cannot parse your date-time string correctly.
  2. SimpleDateFormat is notoriously troublesome and long outdated. Neither for this nor for any other purpose should you use it. Instead just use the LocalDateTime class from java.time, the modern Java date and time API. It parses your string wothout any explicit formatter.

You can’t with SimpleDateFormat

Your string has two decimals on the second of minute, .04, signifying 4 hundredths of a second. SimpleDateFormat only supports exactly three decimals on the seconds, not two or four or any other number. So there is no way that it can parse your string correctly.

java.time

It seems that you are assuming that the string you parse is in UTC and you want to convert it to the default time zone of your device. Your string is in ISO 8601 format, the format that the classes of java.time parse as their default, so we don’t need to specify any formatter.

    String dateStr = "2020-02-13T16:57:13.04";
    LocalDateTime dateTime = LocalDateTime.parse(dateStr);
    ZonedDateTime inDefaultTimeZone = dateTime.atOffset(ZoneOffset.UTC)
            .atZoneSameInstant(ZoneId.systemDefault());
    System.out.println(inDefaultTimeZone);

On my computer in Europe/Copenhagen time zone the output from this snippet is:

2020-02-13T17:57:13.040+01:00[Europe/Copenhagen]

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern 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
1

Take out the 'Z' on your format string

This should work:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);

Or if you want to keep the milliseconds then use SS:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS", Locale.ENGLISH);
string.Empty
  • 10,393
  • 4
  • 39
  • 67
0
String dateStr = "Jul 16, 2013 12:08:59 AM";
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse(dateStr);
df.setTimeZone(TimeZone.getDefault());
String formattedDate = df.format(date);
  • It’s easy to answer the question in the title and not notice that you aren’t really answering the question in the body text. I’ve done that a few time too. Your code does answer the question in the title, but does not add anything new compared to the code in the question. The question in the question body was about a `java.text.ParseException: Unparseable date: "2020-02-13T16:57:13.04"`, and your answer doesn’t address that. – Ole V.V. Feb 19 '20 at 15:45