通过Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析 ,可以看出java8设计非常好,实现接口Temporal, TemporalAdjuster, ChronoLocalDate等,有非常丰富的方法。例如:LocalDateTime:的部分方法:

  

包含了获取年月日,时分秒纳秒。Date中如果要获取这些信息,必须使用Calendar才可以。现在通过将Date转换为LocalDateTime,就能非常方便,线程安全的获取年月日,时分秒等信息。

    public static int getYear(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getYear();
} public static int getYear(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getYear();
} public static int getMonth(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getMonthValue();
} public static int getMonth(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getMonthValue();
} public static int getDayOfMonth(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getDayOfMonth();
} public static int getDayOfMonth(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getDayOfMonth();
} public static int getHour(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getHour();
} public static int getHour(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getHour();
} public static int getMinute(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getMinute();
} public static int getMinute(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getMinute();
} public static int getSecond(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getSecond();
} public static int getSecond(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getSecond();
}

测试类:

    @Test
public void dateCalculatorGetTest(){
Date date = new Date();
System.out.println(date);
System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
System.out.println(DateTimeCalculatorUtil.getYear(date));
System.out.println(DateTimeCalculatorUtil.getMonth(date));
System.out.println(DateTimeCalculatorUtil.getDayOfMonth(date));
System.out.println(DateTimeCalculatorUtil.getHour(date));
System.out.println(DateTimeCalculatorUtil.getMinute(date));
System.out.println(DateTimeCalculatorUtil.getSecond(date));
}

输出:

Sun Jan 12 22:24:07 CST 2020
2020-01-12T22:24:07.681
2020
1
12
22
24
7

源代码地址:https://github.com/xkzhangsan/xk-time

Java日期时间API系列14-----Jdk8中java.time包中的新的日期时间API类,java日期计算1,获取年月日时分秒等的更多相关文章

  1. Sql 中获取年月日时分秒的函数

    getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...

  2. jquery获取年月日时分秒当前时间

    获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日  00:00:00格式的当前时间 function getCurrentDate(date){ var y ...

  3. 2018.2.2 java中的Date如何获取 年月日时分秒

    package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; pub ...

  4. java Date获取 年月日时分秒

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. python 获取年月日时分秒 获取当前时间 datetime函数

    import datetime#取当前时间print(datetime.datetime.now())#取年print(datetime.datetime.now().year)#取月print(da ...

  6. TEST===>Sqlserver中获取年月日时分秒

    可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...

  7. JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;

    学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...

  8. 在vue项目中显示实时时间(年月日时分秒)

    1.在data中定义一个变量,存储时间 data(){ return { nowTime:'' } }, 2.给定一个div <div>{{nowTime}}</div> 3. ...

  9. js获取当前时间的年月日时分秒以及时间的格式化

    1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...

  10. jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒

    sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...

随机推荐

  1. layui表格列添加超链接并传参

    1.表格渲染中对列添加templet属性  addlink为方法名 tableIns = table.render({                elem: '#Test'            ...

  2. 轻松搞定 Nginx 在 CentOS 和 Ubuntu 上的安装与配置

    注:这是对我以前博客进行优化后再次发布的,博客中的截图为以前的.原博客已删除. 如何安装nginx nginx是一款开源.高性能的Web和反向代理服务器,支持HTTP.HTTPS.SMTP.POP3和 ...

  3. 【ECharts】01 快速上手

    简单介绍: ECharts 是一个使用 JavaScript 实现的开源可视化库,涵盖各行业图表,满足各种需求. ECharts 遵循 Apache-2.0 开源协议,免费商用. ECharts 兼容 ...

  4. 如果美国断供中国所有的Intel和AMD芯片,国内各行各业会不会崩溃

    说一个我个人观点,我认为如果国内完全没有X86芯片的供应,那么各行各业的发展会明显进入发展迟缓阶段,首先受影响的就是软件开发领域,因为没有新的芯片也就意味着袋电脑性能停滞或者倒退,那么开发出新的更耗资 ...

  5. Python Pillow(PIL 第三方模块)和 cv2 (opencv第三方模块)对图片的 resize 操作 (缩放图片大小)

    PIL 模块的 resize  操作: 1.  从文件中读取图片,然后  resize  大小: import matplotlib.pyplot as plt import numpy as np ...

  6. Redis系列:使用Stream实现消息队列 (图文总结+Go案例)

    ★ Redis24篇集合 1 先导 我们在<Redis系列14:使用List实现消息队列>这一篇中详细讨论了如何使用List实现消息队列,但同时也看到很多局限性,比如: 不支持消息确认机制 ...

  7. Apache DolphinScheduler:深入了解大数据调度工具

    一.海豚调度介绍 Apache DolphinScheduler 是一个分布式易扩展的可视化DAG工作流任务调度开源系统.适用于企业级场景,提供了一个可视化操作任务.工作流和全生命周期数据处理过程的解 ...

  8. SMU Spring 2023 Trial Contest Round 11

    A. The Text Splitting 题意:给出字符串长度,给出p和q两种切割方式,任选其中一种,把字符串分割输出结果. 题解:先进行判断,p和q是否能整个的分割n,利用p和q的函数关系判断(见 ...

  9. 2022年第一天,体验了一把wan(皖)式服务

    在新的一年到来之际, 一口君首先祝各位粉丝朋友新年快乐! 心想事成! 事业顺利! 阖家欢乐! 财源广进! 2022年的第一天,一口君带着家人去了躺马鞍山,享受了一下马鞍山的洗浴. 马鞍山桑拿虽然比不上 ...

  10. C语言操作时间函数time.ctime,实现定时执行某个任务小例子

    时间操作函数在实际项目开发中会经常用到,最近做项目也正好用到就正好顺便整理一下. 时间概述 由上图可知: 通过系统调用函数time()可以从内核获得一个类型为time_t的1个值,该值叫calenda ...