Set time before 00:00:00 does not work - java

I want to set a string containing the new current date and time 00:00:00. I wrote the following code, but the time is set to12:00:00

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
String today1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").
                  format(calendar.getTime())

I would really like to know why the code does not work or, alternatively, get another way to set the time to 00:00:00

+4
source share
1 answer

You used format charactershh , which is a 12-hour clock or am / pm.

h Hour at am / pm (1-12) Number 12

You typed 12:00 without "am". The time of day is set until midnight, but with your format, the "yyyy-MM-dd hh:mm:ss"output is confused at best.

You can do one of the following:

"H", 24- :

H (0-23) 0

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

:

2016-02-18 00:00:00

"a", am/pm.

a am/pm marker PM

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")

:

2016-02-18 12:00:00 AM
+9

Source: https://habr.com/ru/post/1629403/


All Articles