日期对象和日期字符串的相互转换

时间:2022-02-14 17:58:34
1.DateUtil.parseDate(posTime, "yyyy-MM-dd HH:mm:ss")将string编程date

 Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2005-01-01");//将日期字符串转为日期对象,结果为
Fri Jan 23 00:00:00 CST 2015

public static void main(String[] args) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
Date d = s.parse("2015-01-23");
System.out.println(d);//Fri Jan 23 00:00:00 CST 2015
}


2.DateUtil.format(new date(),"yyyy-MM-dd HH:mm:ss")返回string


SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String t = s.format(new Date());
System.out.println(t);//2017-06-06 11:24:52