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 ...
随机推荐
- nacos配置&gateway配置服务发现一直报500
项目场景: 这两天不是一直在搞简化配置.使用公共配置.我的服务可以通过网关访问这几个任务嘛,也是不断地踩坑补知识才总算把这几个任务都搞好了,下面就是记录过程中遇到的问题. 使用公共配置 因为发现项目使 ...
- 腾讯云免费申请SSL证书配置https
证书申请 1.进入腾讯云官网,在上方直接搜索SSL,搜索到后点击立即选购: 2.点击进去后选择自定义配置,加密标准选择默认的国际标准,证书种类选择域名免费版(DV),勾选同意服务条款后选择免费快速申请 ...
- 1、Springboot2简介
在学习 SpringBoot 之前,建议先具备 SpringMVC(控制层).Spring(业务层)和 Mybatis(持久层)的相关知识 1.1.概述 1.1.1.Spring的缺点 Spring ...
- Google的jax框架在TPU上的循环控制 —— 向量计算设备的循环结构控制
相关: https://jax.readthedocs.io/en/latest/pallas/tpu.html 向量计算设备,如:GPU.TPU等,都是通过向量计算来进行加速的,因此在这类设备中进行 ...
- 向日葵的平替:fastnat的使用
相关: 向日葵的平替:一款个人开发的远程工具--fastnat ---------------------------------------------- 1. 下载安装 2. 注册设备 我这里在两 ...
- MPI经典课程视频 —— 中国科学技术大学-并行计算(国家级精品课) —— 陈国良院士的06年课程
课程视频地址: https://www.bilibili.com/video/BV1U7411N78e
- 分享某Python下的mpi教程 —— A Python Introduction to Parallel Programming with MPI 1.0.2 documentation ( 续 #2 )
接前文: 分享某Python下的mpi教程 -- A Python Introduction to Parallel Programming with MPI 1.0.2 documentation ...
- 人工智能领域在显卡短缺之下的思考——“【亦】微笑面对显卡短缺:不买更快乐”有感——mindspore快快成长
划水之时看了看B站视频: [亦]微笑面对显卡短缺:不买更快乐 https://www.bilibili.com/video/BV1RZ4y1c7qT 在显卡短缺之下不仅仅电脑游戏玩家被卡脖子就连高校和 ...
- python代码实现将PDF文件转为文本及其对应的音频
代码地址: https://github.com/TiffinTech/python-pdf-audo ============================================ imp ...
- 部署CPU与GPU通用的tensorflow:Anaconda环境
本文介绍在Anaconda环境中,下载并配置Python中机器学习.深度学习常用的新版tensorflow库的方法. 在之前的两篇文章Python TensorFlow深度学习回归代码:DNN ...