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?

1
  • Please try using SimpleDateFormat. With SimpleDateFormat you can convert dates to different formats with just 3 lines of code. Commented Sep 14, 2020 at 16:52

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
Can paste some code snippet? Can you ensure that format is correct? @Codist
As I can see that the issue is not with the Kotlin code, please find my post here. stackoverflow.com/q/75273959/13935956

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.