alamkanak / Android-Week-View

Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.
Apache License 2.0
3.42k stars 1.23k forks source link

Bug: side hours aren't formatted to 24 hours #497

Open AndroidDeveloperLB opened 6 years ago

AndroidDeveloperLB commented 6 years ago

Events get formatted fine, but not side hours:

image

Also, I've noticed that the library uses some constant ways to show the time formatting, by checking if the system uses 24 hours or not. That's not the best way to do it.

The correct way to do it is by getting the OS format alone :

/**
 * gets the default time formatter of the device
 */
@JvmStatic
fun getFormatTimeUsingDeviceSettings(context: Context, useUtc: Boolean): java.text.DateFormat {
    return (DateFormat.getTimeFormat(context)
            ?: SimpleDateFormat("HH:mm", Locale.getDefault()))
}
AndroidDeveloperLB commented 6 years ago

Seems it's a part of the sample itself, in BaseActivity. Here's how it should be there:

private fun setupDateTimeInterpreter(shortDate: Boolean) {
    val calendar = Calendar.getInstance()
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    calendar.set(Calendar.MILLISECOND, 0)
    val dateFormat = DateFormat.getTimeFormat(this@BaseActivity)
            ?: SimpleDateFormat("HH:mm", Locale.getDefault())

    weekView.dateTimeInterpreter = object : DateTimeInterpreter {
        ...
        override fun interpretTime(hour: Int): String {
            calendar.set(Calendar.HOUR_OF_DAY, hour)
            return dateFormat.format(calendar.time)
        }
    }
}
AndroidDeveloperLB commented 6 years ago

Fixed it here: https://github.com/alamkanak/Android-Week-View/pull/496