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 ...
随机推荐
- linux学习(7):Linux最常用150个命令汇总
Linux最常用150个命令汇总 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更复杂的还有info,但不常用. help 查看Linux内置命令的帮助,比如cd命令. 文件和目录操作 ...
- Jmeter函数助手28-urldecode
urldecode函数用于解码application/x-www-form-urlencoded字符串. String to encode in URL encoded chars:填入applica ...
- 【转载】 在Ubuntu环境下,搜狗输入法乱码问题的解决
原文作者:高坦的博客 | Tan's Blog 原文链接:https://www.cnblogs.com/gtscool/p/12234104.html本文采用 BY-NC-SA 许可协议.转载请注明 ...
- 读论文《IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures》
论文地址: https://arxiv.org/pdf/1802.01561v2.pdf 论文<IMPALA: Scalable Distributed Deep-RL with Importa ...
- SpringBoot Session共享,配置不生效问题排查 → 你竟然在代码里下毒!
开心一刻 快 8 点了,街边卖油条的还没来,我只能给他打电话 大哥在电话中说到:劳资卖了这么多年油条,从来都是自由自在,自从特么认识了你,居然让我有了上班的感觉! Session 共享 SpringB ...
- element-UI tree树形控件 修改小三角图标
.el-tree /deep/ .el-tree-node__expand-icon.expanded{ -webkit-transform: rotate(0deg); transform: rot ...
- java汉字占用字节
若使用utf-8编码,中文占3个字节,英文的话只占一个字节 System.out.println("人".getBytes().length); 输出3 若使用unicode编码, ...
- vba for excel 随笔
q1: excel 没有vba入口 1. 快捷键:Alt + F11 2.调出开发工具 1. 打开文件后,依次点击菜单项[文件]-[选项]: 2.在"Excel"选项界面中点击左侧 ...
- CORDIC算法解释及FPGA实现(圆坐标系)
CORDIC算法解释及Verilog仿真(圆坐标系) CORDIC算法原理阐述 CORDIC(Coordinate Rotation Digital Computer)算法,即坐标旋转数字计算方法,是 ...
- 折腾 Quickwit,Rust 编写的分布式搜索引擎 - 可观测性之分布式追踪
概述 分布式追踪是一种跟踪应用程序请求流经不同服务(如前端.后端.数据库等)的过程.它是一个强大的工具,可以帮助您了解应用程序的工作原理并调试性能问题. Quickwit 是一个用于索引和搜索非结构化 ...