import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class String转化成Date {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = new String("2008-3-8");
try {
Date d1 = df.parse(date);
System.out.println(d1);
System.out.println(d1.getYear());
System.out.println(d1.getMonth()); //返回值:0--11
System.out.println(d1.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出结果:
Sat Mar 08 00:00:00 CST 2008
108
2
8
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class String转化成Date {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = new String("2008-3-8");
try {
Date d1 = df.parse(date);
System.out.println(d1);
System.out.println(d1.getYear());
System.out.println(d1.getMonth()); //返回值:0--11
System.out.println(d1.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出结果:
Sat Mar 08 00:00:00 CST 2008
108
2
8