1

The current date and year shows correctly but is not shown properly The time should show properly The code is in kotlin. Could anyone help me out with the solution ?

object DateTimeHelper {

    private fun formatDate(date: String): Date? {
        val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
        sdf.timeZone = TimeZone.getTimeZone("GMT")
        var dateIn: Date?
        dateIn = try {
            sdf.parse(date)
        } catch (e: Exception) {
            Timber.e(e)
            null
        }

        return dateIn
    }


    fun getDate(date: String): String {
        val sdf = SimpleDateFormat(" MMM dd, yyyy")
        val tz = TimeZone.getDefault()
        sdf.timeZone = tz
        return sdf.format(formatDate(date))
    }

    fun getTime(date: String): String {
        val sdf = SimpleDateFormat("hh:mm aaa")
        val tz = TimeZone.getDefault()
        sdf.timeZone = tz
        return sdf.format(formatDate(date))
    }
}
forpas
  • 160,666
  • 10
  • 38
  • 76

0 Answers0