0

I need to group the times together like 02:10 PM, 02:30 PM should come in group 02:00 PM - 03:00 PM. I am getting the all times in yyyy-MM-dd'T'HH:mm:ss.SSS+00:00 format. How to group the time together in 02:10 PM format?

Currently I converted them in hh:mm aa format and got the list, How to group them in Arraylists and finally store them in Map, So that I can list the times in Recyclerview?

Siraj Sumra
  • 783
  • 1
  • 7
  • 24

1 Answers1

5

Try something like this (given items with the time values stored in timeString):

val format = SimpleDateFormat("hh:mm aa")
val calendar = GregorianCalendar.getInstance()

val groups = items.groupBy { item ->
    val date = format.parse(item.timeString)
    calendar.setTime(date)
    calendar.get(Calendar.HOUR_OF_DAY)
}

(runnable demo)

See:

hotkey
  • 111,884
  • 27
  • 298
  • 285
  • How to get 03:00 PM also in the current 02:00 - 03:00 PM group? Any idea @hotkey? – Siraj Sumra Sep 08 '17 at 12:15
  • @SirajSumra, see [another demo](https://try.kotlinlang.org/#/UserProjects/c3e46gmrb4nokp3nbdghrua2l4/f0rut7oncb0ppkalcrfbnh1jdp), there I used the formatted strings as group keys. You might want to adjust the logic that sets the hour, though. – hotkey Sep 08 '17 at 12:30
  • Your solution really worked. But there is an issue in selection of the items. Can you please look into this issue: https://stackoverflow.com/questions/46217375/android-issue-in-selection-and-deselection-of-items-in-an-adapter-kotlin – Siraj Sumra Sep 18 '17 at 05:32