Home > database >  Correct way to parse Date in Kotlin Android (Minimum Android Version 21). My parse is not working
Correct way to parse Date in Kotlin Android (Minimum Android Version 21). My parse is not working

Time:11-16

I want to parse this date in this format 2021-11-03T14:09:31.135Z (message.created_at)

My code is this:

val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")
var convertedDate = Date()
try {
    convertedDate = dateFormat.parse(message.created_at)
} catch (e: ParseException) {
    e.printStackTrace()
}

It is failing to parse

CodePudding user response:

Don't use SimpleDateFormat it's long outdated and troublesome.
Use DateTimeFormatter to parse the date.

 fun parseDate() {
        var formatter: DateTimeFormatter? = null
        val date = "2021-11-03T14:09:31.135Z" // your date string
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") // formatter
            val dateTime: LocalDateTime = LocalDateTime.parse(date, formatter) // date object
            val formatter2: DateTimeFormatter =
                DateTimeFormatter.ofPattern("EEEE, MMM d : HH:mm") // if you want to convert it any other format
            Log.e("Date", ""   dateTime.format(formatter2))
        }
    }

Output: Wednesday, Nov 3 : 14:09

To use this below android 8 , use desugaring

CodePudding user response:

Well, the format not entirely what the string looks like:

  • You have a space instead of the T literal between the date and the time
  • You have no offset notation at the end
  • You are using hh, which is 12-hour format. Use HH instead.

This format should do it:

yyyy-MM-dd'T'HH:mm:ss.SSSX

However, note that Date and SimpleDateFormat are obsolete and troublesome. Use java.time instead. If your Android API level appears to be too low, you could use ThreeTen Backport.

CodePudding user response:

If your minimum API level is 21, you can use API Desugaring, find some nice explanations about it here.

As soon as you have enabled API Desugaring, you can directly parse your ISO String to an OffsetDateTime:

val convertedDate = OffsetDateTime.parse(message.created_at)

CodePudding user response:

try it


fun main() {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS")
    var convertedDate = Date()
    try {
        convertedDate = dateFormat.parse("2021-11-03T14:09:31.135Z")
        println(convertedDate)
    } catch (e: ParseException) {
        e.printStackTrace()
    }
}

CodePudding user response:

You have just missed "T" in the date format string. use this solution for your date parsing.

fun formatDate(inputDate: String) {
    var convertedDate = Date()
    val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ", Locale.getDefault())
    try {
        convertedDate = dateFormat.parse(inputDate)
        print("Parsed date $convertedDate")
    } catch (ignored: ParseException) {
    }

    //if you wish to change the parsed date into another formatted date
    val dfOutput = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault())
    val str :String = dfOutput.format(convertedDate)
    print("Formatted date $str")
}

Just pass your "message.created_at" as an input parameter. For more date time formats check out this official documentation of Android Developers site. SimpleDateFormat | Android Developers you will get all the possible date formats here.

Cheers..!

  • Related