我编写了如下的代码以供测试:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
SimpleDateFormat MMdd = new SimpleDateFormat("MM-dd");
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
SimpleDateFormat full = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = null;
try {
date = MMdd.parse("10-01");
System.out.println(full.format(date));
date = yyyy.parse("2020");
System.out.println(full.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
得到如下的输出:
1970-10-01 00:00 2020-01-01 00:00
看起来SimpleDateFormat的parse()方法是以1970年1月1日零点的时间为基础,将我们提供的日期直接“加上去”然后再返回给我们的。
XD