String Calendar Date之间的转换
1.Calendar 转化 String
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
System.out.println(dateStr);
2.String 转化Calendar
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
3.Date 转化String
Date date = new Date();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(date);
System.out.println(dateStr);
4.String 转化Date
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);
System.out.println(date);
5.Date 转化Calendar
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
System.out.println(calendar);
6.Calendar转化Date
Calendar calendar = new GregorianCalendar();
Date date =calendar.getTime();
System.out.println(date);