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]

相关推荐
漫漫进阶路2 小时前
VS C++ 配置OPENCV环境
开发语言·c++·opencv
bohu832 小时前
亚博microros小车-原生ubuntu支持系列:8-脸部检测与人脸特效
linux·opencv·ubuntu·dlib·microros·亚博
陈平安Java and C2 小时前
MyBatisPlus
java
秋野酱2 小时前
如何在 Spring Boot 中实现自定义属性
java·数据库·spring boot
Bunny02123 小时前
SpringMVC笔记
java·redis·笔记
BinaryBardC3 小时前
Swift语言的网络编程
开发语言·后端·golang
feng_blog66883 小时前
【docker-1】快速入门docker
java·docker·eureka
code_shenbing3 小时前
基于 WPF 平台使用纯 C# 制作流体动画
开发语言·c#·wpf
邓熙榆3 小时前
Haskell语言的正则表达式
开发语言·后端·golang
ac-er88884 小时前
Yii框架中的队列:如何实现异步操作
android·开发语言·php