Date类学习总结
1. Calendar和Date的转化
(1) Calendar转化为Date
Calendar cal=Calendar.getInstance();
Date date=cal.getTime();
(2) Date转化为Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
2. 把字符串转化为java.util.Date
方法一:
String str1="1988-10-20 13:20:20";
DateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=format1.parse(str1);
System.out.println(date); //结果是:Thu Oct 20 13:20:20 CST 1988
System.out.println(format1.format(date4));//结果是:1988-10-20 13:20:20
System.out.println(date4.getSeconds()); //结果是:20
方法二:
java.util.Date dt=null;
DateFormat df=DateFormat.getDateInstance();
dt=df.parse("2005-12-19");
System.out.println(dt); //输出结果为:Mon Dec 19 00:00:00 CST 2005
System.out.println(df.format(dt)); //输出结果为:2005-2-19
参考链接:http://www.blogjava.net/jerry-zhaoj/archive/2008/10/08/233228.html 写的比较具体