The problem of Java time conversion

d0 = "2018-01-01 00:00:00";
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = (Date) formate.parseObject(d0);
String result =  DateUtils.formatDate(d1, "yyyy-MM-dd hh:mm:ss");
System.out.println(result);

Why 2017-12-31 04:00:00
?

is returned.

add:

DateUtils.formatDate 

Yes

\apache\httpcomponents\httpclient\4.5.1\httpclient-4.5.1.jar
The method in

:

public static String formatDate(Date date, String pattern) {
    Args.notNull(date, "Date");
    Args.notNull(pattern, "Pattern");
    SimpleDateFormat formatter = DateUtils.DateFormatHolder.formatFor(pattern);
    return formatter.format(date);
}
Jun.13,2022

clipboard.png
as shown in the figure, DateUtils also uses SimpleFormat, but it sets the time zone and the time difference in China by 8 hours
as for the difference between hh and HH is a 12-hour system, one is a 24-hour system


if you post the DateUtils.formatDate method
on the surface, DateUtils.formatDate (D1, "yyyy-MM-dd hh:mm:ss"); hh should be capitalized.
in addition, formate.parseObject should be changed to formate.parse


take a good look at API and see what HhMm stands for

Menu