java中字符串和日期的转换

将字符串转换成日期:
dateStr = "2011-11-30 03:01:37";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateStr);
System.out.println("date =: " + sdf.format(date).toString());

将日期转换为字符串:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar tempCal = Calendar.getInstance();
Date cc = tempCal.getTime();
String time = sdf.format(cc);
System.out.println("time  =: " + time);

你可能感兴趣的:(java中字符串和日期的转换)