DateFormat和SimpleDateFormat

import java.text.DateFormat;

import java.text.SimpleDateFormat;

-----------------------------------------------------------------------------------------------

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Date d = new Date(12436432737L);

String s = df.format(d);  //将时间对象转换成字符串,按照("yyyy-MM-dd hh:mm:ss")格式

System.out.println(s);

-----------------------------------------------------------------------------------------------

String s2 = "1994-8-29";

DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

try {

  Date d2 = df2.parse(s2);  //将字符串转换成时间对象,字符串格式必须和("yyyy-MM-dd")相同,否则出现异常

  System.out.println(d2);

} catch (ParseException e) {

  e.printStackTrace();

}

猜你喜欢

转载自www.cnblogs.com/ss-123/p/8974867.html