package example;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//实体类
class Bean {
?public int id;
?public String name;
?public Date date;
?public int getId() {
??return id;
?}
?public void setId(int id) {
??this.id = id;
?}
?public Date getDate() {
??return date;
?}
?public void setDate(Date date) {
??this.date = date;
?}
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}
}
// 测试类
public class DateString {
?public static void main(String[] args) {
??Bean bean = new Bean();
??// TODO Auto-generated method stub
??// String类型转date类型
??String dString = "1990-04-05 13:20:30";
??SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
??Date date;// 不是sql的date
??try {
???date = sdf1.parse(dString);
???bean.setDate(date);
???System.out.println(sdf1.format(bean.getDate()));
??} catch (ParseException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??// date 转String类型
??Date timeDate = bean.getDate();
??SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
??bean.setName(sdf2.format(timeDate));
??System.out.println(bean.getName());
?}
}