0

I run follow code :

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm zzz");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
System.out.println(date);
System.out.println(sdf.format(date));

and I got :

Mon Jan 25 18:32:52 CST 2016
2016-01-25 06:32 CST

I am in China . why ?

zh18
  • 154
  • 2
  • 13

1 Answers1

4

In SimpleDateFormat, the hh specifier formats the date for a 12 hour clock (usually used with am/pm notation). If you need a 24 hour clock, use HH or kk:

...
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm zzz");
...

See also https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html:

H Hour in day (0-23)

k Hour in day (1-24)

h Hour in am/pm (1-12)

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123