datetime-local数据类型和Date数据类型转化(前端到后端,后端到前端)

前端的datetime-local传递到后端为Date类型

前端的input输入框

<input type="datetime-local" name="startTtime" placeholder="请输入会议起始时间">

后端接收

@PostMapping("/meetingShow")
	//接受前端传递的值
    public void meetings(String startTtime) throws ParseException {
        //处理传递的时间
        //传过来的时间格式为 “2022-02-09T10:21” 中间有一个T,所以我们要把T去掉
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startTime = dateFormat.parse(startTtime.replace("T"," "));
        //此时,时间格式转化完毕
        //然后进行你想要的操作
    }

后端为Date类型传递到前端的datetime-local

后端先获得了Date类型数据

    @GetMapping("/meetingUpdate/{id}")
    public String meetingUpdate(@PathVariable Integer id, Model model){
        //查询到某个具体的对象
        Meetings m = meetingService.getMeetingById(id);
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //获取数据库的时间“2022-02-09 10:21”,转化为前端需要的字符串格式的时间“2022-02-09T10:21”
        String startTtime = dateFormat.format(m.getStarttime());
        //此时,时间格式化数据完毕
        model.addAttribute("startTtime",startTtime.replace(" ","T"));
        return "collegeadmin/meetinginput";
    }

前端的input输入框利用th:value="${startTtime}"接收

<input type="datetime-local" th:value="${startTtime}" name="startTtime" >
  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值