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

相关推荐
xingpanvip几秒前
星盘接口开发文档:马盘次限盘接口指南
android·开发语言·python·php·lua
FBI HackerHarry浩2 分钟前
第二阶段Day07【Python生成器、yield关键字、property、正则表达式】
开发语言·python·正则表达式
好好风格5 分钟前
【一行代码】查看本机公网 IP
linux·命令行
iiiiyu9 分钟前
IO流(二)
java·开发语言·数据结构·编程语言
白露与泡影9 分钟前
牛客网大厂Java面试题全集(2026版,附答案)
java·开发语言
_Evan_Yao10 分钟前
一文搞懂:Git分支管理与团队协作规范——从GitFlow到GitHub Flow,从rebase到merge,打造高效协作流
java·git·后端·github
零点一顿微胖14 分钟前
[Agent]实现获取系统基本信息接口 Rust版
开发语言·rust
未若君雅裁15 分钟前
AQS 与 ReentrantLock:队列同步器与可重入锁
java
码语智行17 分钟前
系统启动时初始化数据功能分析
java·spring boot
得物技术23 分钟前
用 LLM Agent 重构告警排查流程|得物技术
java·人工智能·后端