Java日期时间API系列14-----Jdk8中java.time包中的新的日期时间API类,java日期计算1,获取年月日时分秒等
通过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,获取年月日时分秒等的更多相关文章
- Sql 中获取年月日时分秒的函数
getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...
- jquery获取年月日时分秒当前时间
获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日 00:00:00格式的当前时间 function getCurrentDate(date){ var y ...
- 2018.2.2 java中的Date如何获取 年月日时分秒
package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; pub ...
- 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 ...
- python 获取年月日时分秒 获取当前时间 datetime函数
import datetime#取当前时间print(datetime.datetime.now())#取年print(datetime.datetime.now().year)#取月print(da ...
- TEST===>Sqlserver中获取年月日时分秒
可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...
- JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;
学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...
- 在vue项目中显示实时时间(年月日时分秒)
1.在data中定义一个变量,存储时间 data(){ return { nowTime:'' } }, 2.给定一个div <div>{{nowTime}}</div> 3. ...
- js获取当前时间的年月日时分秒以及时间的格式化
1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...
- jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒
sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...
随机推荐
- 【Redis】05 持久化
持久化概述 Redis提供了不同的持久性选项: 1.RDB持久性按指定的时间间隔执行数据集的时间点快照. 2.AOF持久性会记录服务器接收的每个写入操作,这些操作将在服务器启动时再次播放,以重建原始数 ...
- 用DolphinScheduler轻松实现Flume数据采集任务自动化!
转载自天地风雷水火山泽 目的 因为我们的数仓数据源是Kafka,离线数仓需要用Flume采集Kafka中的数据到HDFS中. 在实际项目中,我们不可能一直在Xshell中启动Flume任务,一是因为项 ...
- 社区6月月报 | Apache SeaTunnel重要更新与优化记录
各位热爱Apache SeaTunnel的小伙伴们,社区6月份月报来啦!这里将记录Apache SeaTunnel社区每月的重要更新,欢迎关注. 月度Merge Stars 感谢以下小伙伴上个月为Ap ...
- SMU Summer 2023 Contest Round 6
SMU Summer 2023 Contest Round 6 A. There Are Two Types Of Burgers 从0枚举到汉堡的最大个数,取最大值 #include <bit ...
- 使用Istio服务网格进行微服务网络治理
在Rainbond中,用户可以对不同的应用设置不同的治理模式,即用户可以通过切换应用的治理模式,来按需治理应用,这样带来的好处便是用户可以不被某一个ServiceMesh框架所绑定,且可以快速试错,能 ...
- zabbix 自定义用户key与参数userparameters监控监本输出
zabbix在模板中预定义了一些key,但通常情况,并不能满足我们的需求.幸运的是zabbix提供了自定义key的方法,因此我们可以灵活的监控各种我们想要监控的数据. 定义key有两种修改方式: vi ...
- Linux 主流桌面环境
GNOME KDE Xfce Ubuntu 使用 GNOME 作为桌面环境. 基于 KDE Plasma 开发的 Ubuntu 发行版:Kubuntu 基于 Xfce 开发的 Ubuntu 发行版:X ...
- Linux 更新 TeX Live
更新 TeX Live 假设你的旧版 TeX Live 版本号为 2023,新版 TeX Live 版本号为 2024.你需要在下面的命令中相应地更改实际版本号.TeX Live 版本可以通过 tlm ...
- docker高级篇:实战-自己开发的微服务怎么在docker上面运行?
通过前面的一系列学习,我们已经知道怎么制作dockerfile了.那么,本篇文章,咱们就把自己写的spring boot的demo项目,部署在docker上面. 案例目标: 我们自己开发的微服务怎么在 ...
- OpenAI注册-临时手机号/邮箱
OpenAI 在注册ChatGPT时,发生了一个错误,使用邮箱进行注册后,在注册界面会提示"Not available OpenAI's services are not available ...