jAVA时间 SimpleDateFormat Date 和 String 类型时间互换

/*
    Java时间设为二十四小时制和十二小时制的区别:
    1) 二十四小时制: “yyyy-MM-dd HH:mm:ss”
    2)十二小时制: “"yyyy-MM-dd hh:mm:ss"”
    */
    //获取时间 格式自定
    //例如:type  =  "yyyy-MM-dd hh:mm:ss"
    public static String getDate(String type){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        String time = simpleDateFormat.format(new Date());
        return time;
    }
	public static void main(String[] args) {
        String HHdate = getDate("yyyy-MM-dd HH:mm:ss");
        System.err.println("HH:"+HHdate);
        String hhdate = getDate("yyyy-MM-dd hh:mm:ss");
        System.err.println("hh:"+hhdate);
    }

HH:2022-09-30 08:57:45
hh:2022-09-30 08:57:45

public static Date getStringtoData(String time){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = format.parse(time);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static void main(String[] args) {
        String str = "2022-08-02 23:52:34";
        Date stringtoData = getStringtoData(str);
        System.err.println("stringtoData:"+stringtoData);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(stringtoData);
        System.err.println("format:"+format);
    }

stringtoData:Tue Aug 02 23:52:34 CST 2022
format:2022-08-02 23:52:34

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值