java日期常用操作

java 复制代码
 @Test
    public void validateDateUtils(){
        // 1 字符串转换日期
        Date result = DateUtil.parse("2023-08-01", com.alibaba.excel.util.DateUtils.DATE_FORMAT_10);
        log.info("result : [{}]" , result);

        // 2 日期转换字符串
        final Date date = new Date();
        String formatStr = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_19);
        log.info("formatStr : [{}]" , formatStr);

        // 3 获取当前时间,格式为yyyy-MM-dd,yyyy-MM-dd HH:mm:ss,yyyyMMddHHmmss
        final String formatYMR = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_10);
        log.info("formatYMR : [{}]" , formatYMR);
        final String formatYMRSFM = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_19);
        log.info("formatYMRSFM : [{}]" , formatYMRSFM);
        final String formatYMRSFMS = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_14);
        log.info("formatYMRSFMS : [{}]" , formatYMRSFMS);

        // 4 日期相减
        final long resultTime = result.getTime();
        final long dateTIme = date.getTime();
        final long diffInDays = (dateTIme - resultTime) / (1000 * 60 * 60 * 24);
        log.info("diffInDays : [{}]" , diffInDays);

        // 5 日期加减操作
        final int compareDays = cn.hutool.core.date.DateUtil.compare(date, result);
        log.info("compareDays : [{}]" , compareDays);
        final Date addDate = DateUtils.addDays(new Date(), 1);
        log.info("addDate : [{}]" , addDate);

        // 6 日期转换时间戳
        final long current = cn.hutool.core.date.DateUtil.current();
        final long currentTimeMillis = System.currentTimeMillis();
        log.info("currentTimeMillis : [{}]" , currentTimeMillis);
        log.info("current : [{}]" , current);
        // 7 util.date转换sql.date
        Timestamp timestamp = new Timestamp(currentTimeMillis);
        log.info("timestamp : [{}]" , timestamp);

    }

22:49:41.918 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - result : [Tue Aug 01 00:00:00 CST 2023]

22:49:42.102 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatStr : [2023-08-09 22:49:41]

22:49:42.103 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMR : [2023-08-09]

22:49:42.103 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMRSFM : [2023-08-09 22:49:41]

22:49:42.106 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMRSFMS : [20230809224941]

22:49:42.106 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - diffInDays : [8]

22:49:42.121 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - compareDays : [1]

22:49:42.121 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - addDate : [Thu Aug 10 22:49:42 CST 2023]

22:49:42.122 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - currentTimeMillis : [1691592582122]

22:49:42.122 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - current : [1691592582122]

22:49:42.123 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - timestamp : [2023-08-09 22:49:42.122]

相关推荐
monkey_meng11 分钟前
【Rust中的项目管理】
开发语言·rust·源代码管理
喜欢打篮球的普通人13 分钟前
rust高级特征
开发语言·后端·rust
Ling_suu29 分钟前
Spring——单元测试
java·spring·单元测试
ModelBulider31 分钟前
十三、注解配置SpringMVC
java·开发语言·数据库·sql·mysql
V搜xhliang024640 分钟前
基于深度学习的地物类型的提取
开发语言·人工智能·python·深度学习·神经网络·学习·conda
moneyxjj40 分钟前
Linux各种解压命令汇总
linux·运维·服务器
DK七七42 分钟前
多端校园圈子论坛小程序,多个学校同时代理,校园小程序分展示后台管理源码
开发语言·前端·微信小程序·小程序·php
苹果酱05671 小时前
C语言 char 字符串 - C语言零基础入门教程
java·开发语言·spring boot·mysql·中间件
csucoderlee1 小时前
eclipse mat leak suspects report和 component report的区别
java·ide·eclipse
白白♛~1 小时前
网络管理之---3种网络模式配置
linux·服务器·网络