LorenzoBerti LorenzoBerti - 2 months ago 14
Android Question

Parse Date android 0-24 with 12:00

I have a String in my mySqldb: 2016-09-14 12:00:00, and in my app I would like get only hour so 12:00 but use 0-24h.
Example:

input > 2016-09-14 12:00:00 output > 12:00:00
input > 2016-09-14 14:00:00 output > 14:00:00
input > 2016-09-14 08:00:00 output > 08:00:00


So I tried with

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");


but return

output > 12:00
output > 02:00
output > 08:00


So with without 14:00
I tried with

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("kk:mm:ss");


but return

output > 24:00
output > 14:00
output > 08:00


so with 24:00 instead 12:00

And also with:

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");


but return

output > 00:00
output > 14:00
output > 08:00


so with 00:00 instead 12:00

How can have output

12:00
14:00
08:00

Answer

Change

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

To

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Your input is in 24 hour format, not 12 hour format.

Output should be:

DateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html