Home > Enterprise >  Convert date and time to Iso 8601 in Kotlin
Convert date and time to Iso 8601 in Kotlin

Time:03-05

I have a picker in my app that I can select the Year, Month, Day, Hour, and minute. After changing each of them I have some numbers like this:

2022 -> for year
3 -> for month
20 -> for day
16 -> hour
58 -> minute

How can I convert these values to Iso 8601 date format?

I need like this format: "2022-03-20T16:58:00.288Z"

CodePudding user response:

Try with this code:

val date = Calendar.getInstance().apply {
    set(Calendar.YEAR, year)
    set(Calendar.MONTH, month)
    set(Calendar.DAY_OF_MONTH, day)
    set(Calendar.HOUR_OF_DAY, hour)
    set(Calendar.MINUTE, minute)
}.time 
val formattedDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(date)
  • Related