将前端传过来的String的日期数据转换成LocalDateTime类型的数据存入数据库

04-13 阅读 0评论

String类型的xxxx年x月xx日与LocalDateTime类型的 yyyy-mm-dd相互转化

1.导入DataUtil工具类

public class DataUtil {
    /**
     * 将String类型的xxxx年x月xx日 ——>  LocalDateTime yyyy-mm-dd
     * @param strData
     * @return
     * @throws
     */
    public static LocalDateTime sTd(String strData) throws ParseException {
//        String strData = "2023年5月12日";
        Date sData = new SimpleDateFormat("yyyy年MM月dd日").parse(strData);
        LocalDateTime localDateTime = sData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }
    /**
     * 将LocalDateTime yyyy-mm-dd ——>  String类型的xxxx年x月xx日
     * @param localDateTime
     * @return
     */
    public static String dTs(LocalDateTime localDateTime){
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年M月dd日");
        String strData = localDateTime.format(pattern);
        return strData;
    }
}

2.调用工具类

    @Test
    public void dataTest() throws ParseException {
        LocalDateTime dateTime = DataUtil.sTd("2023年5月12日");
        System.out.println(dateTime);
        String strData = DataUtil.dTs(dateTime);
        System.out.println(strData);
    }

3.输出结果

将前端传过来的String的日期数据转换成LocalDateTime类型的数据存入数据库


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]