When it comes to the date formatting tool class, I have been building my own wheel. Recently, a brother of the project team used apache @ dateutils and found a bug in this class during the test.
Let's take a look at my wheels:
public static String date2String(Date date, String style) { SimpleDateFormat sdf = new SimpleDateFormat(style); //TimeZone gmt = TimeZone.getTimeZone("GMT+08:00"); //sdf.setTimeZone(gmt); if (date != null) { return sdf.format(date); } return null; }
Under normal circumstances, the two lines of code commented out in the above Code do not need to be added, because simpledateformat will find the time zone of the local server by default, and then set it, of course, the premise is that the time zone of the server is set correctly.
The Apache dateutils # format () method code is as follows:
public static String format(Date date, String pattern) { DateFormat df = createDateFormat(pattern); return df.format(date); }
private static DateFormat createDateFormat(String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); TimeZone gmt = TimeZone.getTimeZone("GMT"); sdf.setTimeZone(gmt); sdf.setLenient(true); return sdf; }
Note: The time zone is GMT, so the formatted time is biased.
To sum up, pay attention to this bug when using Apache @ dateutils.