1.string格式转化为Date对象
public Date format(String date){
DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
Date date1=null;
try {
//String转date
date1 = fmt.parse(date);
} catch (ParseException e) {
// e.printStackTrace();
}
return date1;
}
2.Date格式转化为String对象
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String startTime = sdf.format( date.getTime());
System.out.println(startTime);
该代码展示了如何在Java中将字符串转换为Date对象以及将Date对象转换为字符串。使用SimpleDateFormat进行格式化,并处理可能的ParseException。
2831

被折叠的 条评论
为什么被折叠?



