Search code examples
androidkotlinsimpledateformatdate-formattingdate-conversion

SimpleDateFormat("dd/MM/yy kk:mm") showing time as 24:30 instead of 00:30


I have to convert the date from API in the 24-hour format but SimpleDateFormat("dd/MM/yy kk:mm") is showing 24:30 instead of 00:30.

I am receiving the date in this format: "2020-05-25 10:34:27"

I am converting the date as follows:

      val newDate: Date =  SimpleDateFormat("yyyy-MM-dd hh:mm:ss", 
    Locale.getDefault()).parse(vehiculos[b].fecha)
    val d = SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.getDefault())
            .parse(SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 
    Locale.getDefault()).format(newDate))
    val newFecha = SimpleDateFormat("dd/MM/yy kk:mm", Locale.getDefault()).format(d)

Solution

  • You should use the uppercase letter K

    SimpleDateFormat("dd/MM/yy KK:mm", Locale.getDefault())
    

    k: Hour in day (1-24)

    K: Hour in am/pm (0-11)

    Check this site to test it out: http://www.sdfonlinetester.info/