0

I want to convert the "H:mm" value of time that i am getting from my Firestore database to "h:mm a" but i am not able to, getting "Unparseable date" and sometimes just plain it's not working.

    val strTime = studentAttendanceList[position].Time.toString() //time from firebase store in "H:mm" format
    val dateFormat: DateFormat = SimpleDateFormat("h:mm")
    val date = dateFormat.parse(strTime)
    val f2 = SimpleDateFormat("HH:mm a")
    f2.format(date).lowercase()

I want to get this time in

h:mm a

For example -

16:00 to 4:00 pm
Shubham Kumar
  • 37
  • 1
  • 9

1 Answers1

0

java.time.LocalTime might be the answer to your problems.

  1. If you have your time represented as a string use this:
    val result = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm")).format(DateTimeFormatter.ofPattern("hh:mm a"));

  2. If you have your data as a date time use this where localTimeInstance is your time represented as a LocalTim:
    val result = localTimeInstance.format(DateTimeFormatter.ofPattern("hh:mm a"));

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • It worked, TY but i wanted something that can work on API 23 and up. This Required API 26 and up, is there a way i can make this work on API 23? – Shubham Kumar Jan 02 '22 at 06:43
  • https://stackoverflow.com/a/16190056/13533028 This should work on any api level. Also don't forget to accept the answer so that it helps other people as well. Welcome to StackOverflow .:) – Narendra_Nath Jan 02 '22 at 07:26