JavaSE进阶

10.5.5 java.text.DateFormat和SimpleDateFormat

完成字符串和时间对象的转化:

  • String format(date)
  • Date parse(string)

public static void main(String[] args) {

Date date = new Date();

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 是本年的第几D");

System.out.println(sf.format(date));

String s = "2016-12-01 14:12:23";

SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

Date d = sf2.parse(s);

System.out.println(d);

} catch (ParseException e) {

e.printStackTrace();

}

}