简体   繁体   中英

Display time in 24 hour format

I wanted to get date and time in this format. "2017-02-05 20:20:03".
I used the following code to get the result.
But it didn't work out.

System.out.println(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date()));

Your immediate problem is that you used mm twice. MM is a month.

This works in Java 8.

String dt = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").format(LocalDateTime.now);

2017-02-10 23:20:11

Check the date and time patterns table; Month is capital M and minute is lower m . So your date part should look like this: yyyy-MM-dd .

The SimpleDateFormat in your code should look like this:

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

As you can see from the table in the link above, H is 24 hour time so this is correct. Have you checked what timezone your jvm is running in?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM