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 ?