日期格式化跨年bug,是否与你不期而遇?
public class DateFormatBug { public static void main(String[] args) throws ParseException { // 示例一 printBugDate(); } private static void printBugDate() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); Date date = sdf.parse("2020-1-1 13:12:12"); System.out.println(date); String dateStr = sdf.format(date); System.out.println(dateStr); } } 打印日志为: Sun Dec 29 13:12:12 CST 2019 2020-12-29 13:12:12 示例二,延伸示例: private static void printBugDateExtend() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); Date date = sdf