import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest01 {
public static void main(String[] args) throws Exception {
Date nowTime =new Date();
//将日期转换为字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String nowTimeStr = sdf.format(nowTime);
System.out.println(nowTimeStr);
//将字符串(String)转换为日期
String time ="2008-08-08 08:08:08 888";
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date dateTime =sdf2.parse(time);
System.out.println(dateTime);
}
}
字符串的日期格式与SimpleDateFormat的日期格式要一致,不然会出现异常:java.text.ParseException