java112-simpledateformat进行格式化


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 block
                e.printStackTrace();
            }
       
        emp.setName(name);
        emp.setSex(sex);
        emp.setBirth(birth);
        emp.showme();
    }
}
运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值