Kotlin DateFormat 24h 12h (KD2412)


KD2412:   SOLD       




Hi dev, on this occasion I would like to share an experience that just happened. Related to SimpleDateFormat Android.

First, I have a User Interface that requires the user to input the start time and end time (Hours: Minutes), of course I also validate this type of input, because I don't want any users to be careless or have human error so that the start time and end time are reversed.

Unit testing has been done for almost 2 months, but luckily I found this bug accidentally. Several times inputting data for this time span, we all did not realize that the data we always input never crossed the shift between morning and afternoon, coincidentally our data input pattern was between 06:00 to 11:00 or 12:00 to 18:00. The pattern ran smoothly so that it made us feel safe.

Well, this morning, for some reason, coincidentally without any intention, I entered the time data between 11:00 and 12:00, and suddenly my validator threw an INVALID result, even though with the naked eye I saw that there was nothing wrong with the data I entered.

My debugging process started from the material time picker component, the results were normal, I had set the time mode to 24 hours. After that I continued to the validator, I also saw that there were no significant anomalies, I felt that my setup was correct.

While daydreaming, doubts still haunted me about the validator, finally I decided to use the google fu technique, after finding it and trying it out straight away.. it turned out to be true.. just because my time format was like this, it turned out that this affected the time format too.

Return 12 hours

SimpleDateFormat("hh:mm:")

Return 24 hours

SimpleDateFormat("HH:mm:")

Previously I used the format  hh:mm and it turns out this is the reason why my validator considered it invalid because the active mode is 12 hours. Meanwhile, I set the time picker widget to 24 hours.

Done!


Post a Comment

Previous Next

نموذج الاتصال