通过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. python lambda 三元表达式

    python lambda 三元表达式 python中的lambda函数用法 通常定义的函数 def sum(x,y): return x+y print(sum(4,6)) 用lambda来实现: ...

  2. web3 产品介绍 MyEtherWallet 方便和智能合约交互的钱包

    MyEtherWallet(简称MEW)是一款流行的去中心化以太坊钱包,它允许用户在安全且简单的界面中管理自己的以太坊资产.在本文中,我们将介绍MyEtherWallet的主要特点.功能以及如何使用它 ...

  3. 【Vue】使用iframe解决多应用整合问题(微前端)

    一.需求背景 有老系统需要重构,新做的系统需要做一个大一统的整合,类似一个分类栏目 在菜单位置罗列出有什么子系统应用,点击对应的应用菜单,展示区跳转到相应的子系统应用中 我用Excel简单描述了下系统 ...

  4. 网友提问:分层A*算法 —— 大规模寻路算法优化

    网友提问: 相关: https://www.cnblogs.com/devilmaycry812839668/p/10525727.html

  5. How to evaluate the Messi Hong Kong fraud incident?

    Who is Lionel Messi? URL: https://en.wikipedia.org/wiki/Lionel_Messi As a famous football player, Me ...

  6. 解决CGLib动态代理测试不通过-Unable to load cache item

    1.背景 在学习aop底层时遇到的问题,做个小结 2.现象 动态代理代码如下: package com.ldp.proxy; import net.sf.cglib.proxy.Enhancer; i ...

  7. Apache DolphinScheduler 支持使用 OceanBase 作为元数据库啦!

    DolphinScheduler是一个开源的分布式任务调度系统,拥有分布式架构.多任务类型.可视化操作.分布式调度和高可用等特性,适用于大规模分布式任务调度的场景.目前DolphinScheduler ...

  8. 【问题解决】git status中文文件名乱码

    问题复现 解决办法 在git bash中直接执行如下命令 git config --global core.quotepath false 原因 通过 git config --help 可以查看到以 ...

  9. Notes for uc/OS-III User Guide

    1. Architecture F2-1(1) The application code consists of project or product files. For convenience, ...

  10. 22张图详解浏览器请求数据包如何到达web服务器(搞懂网络可以毕业了)

    浏览器的请求数据包如何到达web服务器? 很多读者对于其中的完整流程不是特别的了解,下面一口君通过这22张图,详细的讲解我们点击浏览器的网址之后,数据包是如何经过重重险阻到达web server的. ...