String to Date, Date to String

1. Date to String

		Date dt = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		String dtString = sdf.format(dt);
		System.out.println(dt);
		System.out.println(dtString);

    Sat May 04 19:16:49 CST 2013

    2013-05-04 

 

2. String to Date

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");          
        String newDate = "1998-12-11";
        Date dt = sdf.parse(newDate);
        System.out.println(dt);

    Fri Dec 11 00:00:00 CST 1998

 

3. Calendar to Date

		Calendar cal = Calendar.getInstance();
		cal.set(cal.YEAR, 2012);
		cal.set(cal.MONTH, 11);
		Date newdt = cal.getTime();
		System.out.println(newdt);

   Tue Dec 04 19:23:25 CST 2012

 

4. Date to Calendar

String newDate = "2009-12-11";
Date dtValue = sdf.parse(newDate); 
Calendar cal3 = Calendar.getInstance();
cal3.setTime(dtValue);
System.out.println(cal3.getTime());

 Fri Dec 11 00:00:00 CST 2009

你可能感兴趣的:(Date,String,calendar)