1

The JSON data I am returning is a string in numbered format (YYYY-MM-DD) and I am wanting my app to display this as ex.(January 1, 2020) depending on which JSON date I query.

Given the date formatting options in java and kotlin are based off integers, I am unsure of the most efficient way to convert the numbered string to a character string when queried. I do not want a function with 50 lines converting the strings. Is there a more efficient way to do this?

  • Please try using SimpleDateFormat. With SimpleDateFormat you can convert dates to different formats with just 3 lines of code. – Tarun Gupta Sep 14 '20 at 16:52

1 Answers1

1

First convert YYYY-MM-DD date string to Date

val date = SimpleDateFormat("yyyy-MM-dd", Locale.US).parse("2015-05-30")

Then convert the date to your preferred format

 val formattedDatesString = SimpleDateFormat("MMM dd, yyyy", Locale.US).format(date)

But instead of doing this in your adapter, you should use dedicated object to hold the data so that in onBindViewHolder would not need to calculate anything

crack_head
  • 236
  • 1
  • 11
  • After a few searches online I found this the easiest way to do it. But, I found a significant issue in my case. I am fetching data from Google Spreadsheet and the dates in the spreadsheet are shown as 01-Jan-23. When I show this in the `recyclerView` it shows 31-Dec-2022 for 01-Jan-23, 01-Jan-23 for 02-Jan-23 and so on (one day earlier). I do not have time in the spreadsheet, by the way. How can I fix it? – Codist Jan 28 '23 at 08:43
  • Can paste some code snippet? Can you ensure that format is correct? @Codist – crack_head Jan 29 '23 at 09:08
  • As I can see that the issue is not with the Kotlin code, please find my post here. https://stackoverflow.com/q/75273959/13935956 – Codist Jan 29 '23 at 09:45