package com.zx;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
//员工信息类
//date类
public class test50 {private String name;private String sex;private Date birth;public void showme(){System.out.println(this.getName());System.out.println(this.getSex());// System.out.println(DateFormat.getDateInstance().format(this.getBirth()));// System.out.println(this.getBirth());//使用simpledateformat进行格式化SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");String data=sdf.format(birth);System.out.println(data);}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setSex(String sex) {this.sex = sex;}public String getSex() {return sex;}public void setBirth(Date birth) {this.birth = birth;}public Date getBirth() {return birth;}
}
测试类
package com.zx;import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;public class test52 {public static void main(String[] args){Scanner in=new Scanner(System.in);System.out.println("请输入员工信息");test50 emp=new test50();System.out.println("请输入员工姓名");String name=in.next();System.out.println("请输入员工性别");String sex=in.next();//System.out.println("现在的时间");//Date birth=new Date();// System.out.println(birth.toString());System.out.println("请输入员工生日");System.out.println("请输入员工生日年份");int y=in.nextInt();System.out.println("请输入员工生日月份");int m=in.nextInt();System.out.println("请输入员工生日日期");int d=in.nextInt();DateFormat df=DateFormat.getDateInstance();Date birth=null;String dateStr=y+"-"+m+"-"+d;//连接为字符串类型符合时间日期的格式形式System.out.println(dateStr);try {birth = df.parse(dateStr);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}emp.setName(name);emp.setSex(sex);emp.setBirth(birth);emp.showme();}
}
运行结果