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]

相关推荐
西猫雷婶6 分钟前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila6 分钟前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
初晴~7 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
小_太_阳32 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
Jackey_Song_Odd33 分钟前
解决Ubuntu下无法装载 Windows D盘的问题
linux·ubuntu
直裾35 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
乔巴不是狸猫1 小时前
第11周作业
linux
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
唐 城1 小时前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust