1hutool实战:DateUtil(时间工具类) |
您所在的位置:网站首页 › 时间戳转换java › 1hutool实战:DateUtil(时间工具类) |
技术活,该赏
关注+一键三连(点赞,评论,收藏)再看,养成好习惯
hutool实战(带你掌握里面的各种工具)目录 万字博文教你搞懂java源码的日期和时间相关用法 用途:获取当前时间 使用场景当前时间,当前时间戳获取的多种方式 项目引用此博文的依据:hutool-5.6.5版本源码 cn.hutool hutool-core 5.6.5 方法摘要 方法描述cn.hutool.core.date.DateUtil.date() 当前时间,转换为{@link DateTime}对象 cn.hutool.core.date.DateUtil.dateSecond() 当前时间,转换为{@link DateTime}对象,忽略毫秒部分 cn.hutool.core.date.DateUtil.current() 当前时间的时间戳 cn.hutool.core.date.DateUtil.currentSeconds() 当前时间的时间戳(秒) cn.hutool.core.date.DateUtil.now() 当前时间,格式 yyyy-MM-dd HH:mm:ss cn.hutool.core.date.DateUtil.today() 当前日期,格式 yyyy-MM-dd 方法明细 方法名称:cn.hutool.core.date.DateUtil.date() 方法描述当前时间,转换为{@link DateTime}对象 支持版本及以上 参数描述: 参数名描述 返回值:当前时间 参考案例: // 当前时间 DateTime date = DateUtil.date(); System.out.println(date); System.out.println(date.toString()); Assert.assertNotNull(date);要探索JDK的核心底层源码,那必须掌握native用法 1源码分析:DateUtil(时间工具类)-当前时间和当前时间戳 方法明细 方法名称:cn.hutool.core.date.DateUtil.dateSecond() 方法描述当前时间,转换为{@link DateTime}对象,忽略毫秒部分 支持版本及以上4.6.2 参数描述: 参数名描述 返回值:当前时间 参考案例: // 当前时间 DateTime date = DateUtil.date(); System.out.println("long时间:"+date.getTime()); //当前时间 忽略毫秒 System.out.println("long时间忽略毫秒:"+DateUtil.dateSecond().getTime()); //当前时间 忽略毫秒部分 Date date4 = DateUtil.dateSecond(); Assert.assertNotNull(date4);1源码分析:DateUtil(时间工具类)-当前时间和当前时间戳 方法明细 方法名称:cn.hutool.core.date.DateUtil.current() 方法描述当前时间的时间戳 支持版本及以上 参数描述: 参数名描述 返回值:时间 参考案例: long current = DateUtil.current(); String currentStr = String.valueOf(current); Assert.assertEquals(13, currentStr.length()); long currentNano = DateUtil.current(); String currentNanoStr = String.valueOf(currentNano); Assert.assertNotNull(currentNanoStr); 源码解析: /** * 当前时间的时间戳 * * @return 时间 */ public static long current() { return System.currentTimeMillis(); } 方法明细 方法名称:cn.hutool.core.date.DateUtil.currentSeconds() 方法描述当前时间的时间戳(秒) 支持版本及以上4.0.0 参数描述: 参数名描述 返回值:当前时间秒数 参考案例: //当前时间的时间戳(秒) long dateSeconds = DateUtil.currentSeconds(); System.out.println(dateSeconds); Assert.assertNotNull(dateSeconds); 源码解析: /** * 当前时间的时间戳(秒) * * @return 当前时间秒数 * @since 4.0.0 */ public static long currentSeconds() { return System.currentTimeMillis() / 1000; } 方法明细 方法名称:cn.hutool.core.date.DateUtil.now() 方法描述当前时间,格式 yyyy-MM-dd HH:mm:ss 支持版本及以上 参数描述: 参数名描述 返回值:当前时间的标准形式字符串 参考案例: // 当前日期字符串,格式:yyyy-MM-dd HH:mm:ss String now = DateUtil.now(); Assert.assertNotNull(now); 源码解析:1源码分析:DateUtil(时间工具类)-当前时间和当前时间戳 方法明细 方法名称:cn.hutool.core.date.DateUtil.today() 方法描述当前日期,格式 yyyy-MM-dd 支持版本及以上 参数描述: 参数名描述 返回值:当前日期的标准形式字符串 参考案例: // 当前日期字符串,格式:yyyy-MM-dd String today = DateUtil.today(); Assert.assertNotNull(today); 源码解析:1源码分析:DateUtil(时间工具类)-当前时间和当前时间戳 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |