2020-10-23

查询日期转换思路

需求:根据前端传的string 类型日期 转成 需要的 ‘/’分隔的日期 String类型的日期

public Message dateFormat(String s) {
        String[] split = s.split("/");
        // =1 表示不是 "-" 分隔
        if (split.length == 1) {
            String[] sp = s.split("-");
            if (sp.length == 1 || sp.length > 3) {
                return Message.failure("日期格式不正确");
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-d");
            Date parse = null;
            try {
                parse = simpleDateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String format = dateFormat.format(parse);
            return Message.success(format,"日期转换成功");
        } else if (split.length > 3) {
            return Message.failure("日期格式不正确");
        } else {
            return Message.success(s,"日期转换成功");
        }
    }

调用的 地方 判断 转换 状态,返回 或者继续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值