-1

I am facing issue while formatting the date and time of the type "2020-03-30T07:37:02.282+01:00" and i am getting date from my server in these two formats "2020-03-30T07:37:02.282+01:00" and “2020-03-30T05:44:02.397Z” I think i am using the correct time but not sure while parsing i am getting parsable expection for the date type "2020-03-30T07:37:02.282+01:00" but i am able to parse the date of type “2020-03-30T05:44:02.397Z”.Code i am using for formatting the date is

    fun formatToYesterdayOrToday(responseTime: String?): String? {
    val dateTime: Date =
        SimpleDateFormat(
            "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'",
            Locale.getDefault()
        ).parse(responseTime)
    val calendar: Calendar =
        Calendar.getInstance()
    calendar.time = dateTime
    val today: Calendar =
        Calendar.getInstance()
    val yesterday: Calendar =
        Calendar.getInstance()
    yesterday.add(Calendar.DATE, -1)
    val timeFormatter: DateFormat =
        SimpleDateFormat("hh:mma", Locale.getDefault())
    val dateFormatter: DateFormat =
        SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
    return if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) &&
        calendar.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH) &&
        calendar.get(Calendar.MONTH) == today.get(Calendar.MONTH)
    ) {
        "Updated: " + setAmPmToLowercase(timeFormatter.format(dateTime)) + " today"
    } else if (calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) &&
        calendar.get(Calendar.DAY_OF_MONTH) == yesterday.get(Calendar.DAY_OF_MONTH) &&
        calendar.get(Calendar.MONTH) == yesterday.get(Calendar.MONTH)
    ) {
        "Updated: " + setAmPmToLowercase(timeFormatter.format(dateTime) + " yesterday")
    } else {
        "Updated on: " + dateFormatter.format(dateTime) + " at " + setAmPmToLowercase(
            timeFormatter.format(dateTime)
        )
    }
}

Can any one please help me with what's wrong with my code ?

Ankita Singh
  • 304
  • 3
  • 17
  • 1
    I recommend you don’t use `SimpleDateFormat`, `Date` and `Calendar`. All of those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `OffsetDateTime`, `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 02 '20 at 18:23
  • 1
    You may also want to tag your question with the language you are using. – Ole V.V. Apr 02 '20 at 18:39

1 Answers1

1

Those two strings — 2020-03-30T07:37:02.282+01:00 and 2020-03-30T05:44:02.397Z — have the same format. The format is ISO 8601 (see the link at the bottom). In ISO 8601, an offset from UTC (or GMT) may be given as either Z for zero or for example +01:00 for 1 hour 0 minutes (more variants exist).

You should prefer to use java.time, the modern Java date and time API, for your date and time work. Also java.time.OffsetDateTime parses ISO 8601 as its default, that is, without any explicit formatter. So we don’t need to bother thinking about a format pattern string. In Java:

    OffsetDateTime dateTime
            = OffsetDateTime.parse("2020-03-30T07:37:02.282+01:00");
    System.out.println(dateTime);
    dateTime = OffsetDateTime.parse("2020-03-30T05:44:02.397Z");
    System.out.println(dateTime);

Output:

2020-03-30T07:37:02.282+01:00
2020-03-30T05:44:02.397Z

You notice that OffsetDateTime.toString() (implicitly called when we print the object) is returning ISO 8601 format too.

Even if we had needed to write a format pattern string for a formatter, you should never hardcode Z as a literal (by putting it in single quotes). As I said, Z is an offset from UTC and needs to be parsed as such, or either you will not be able to parse the string or — worse — you risk getting incorrect results from doing it. In all likelihood you were getting an incorrect result when you said you were able to parse 2020-03-30T05:44:02.397Z.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161