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 ...
随机推荐
- 模型推理batch inference速度无明显提升、耗时线性增长问题排查
模型推理batch inference速度无明显提升.耗时线性增长问题排查 现象描述 当模型在推理阶段使用batch inference时,推理速度并无明显提升,相比单帧多次推理收益不大.如笔者在Xa ...
- Python和RPA网页自动化-发送邮件
以163邮箱为例,分别使用Python和RPA网页自动化发送邮件到指定邮箱 其中2个方法都需要用到163邮箱授权码,开启IMAP/SMTP服务即可得到授权码(POP3/SMTP服务不用开启) 1.py ...
- 【Vue】分组类型排名查询功能
一.书接上回: https://www.cnblogs.com/mindzone/p/17749725.html 这个效果被否决了,产品要求一定要UI的来,UI效果如图: 按人为主体的时候,固定有4个 ...
- 【SpringBoot】01 快速上手
环境搭建: JDK8 + IDEA 2018 + SpringBoot + Maven 3.0 + 创建Boot项目 2020.6.1更新补充: 最近才发现SpringBoot用IDEA构建项目会发生 ...
- 【Git】03 撤销 & 版本回退
回退分为三种情况,每种情况对应了我们文件的存储区域 工作区 | 暂存区 | 版本区(当前分支) 1.文件可能存放在工作区,没有被Git追踪[红色标记状态] 2.文件可能已经添加到暂存区,没有被Git提 ...
- JDBC详解学习笔记
JDBC简介 架构时--没有什么是加一层解决不了的,如果有,就再加一层. 如tomcat集群上面的Nginx,Nginx集群上面的LVS. JDBC是数据库驱动的接口规范,是SUN公司未来简化开发人员 ...
- 代码随想录Day4
24.两两交换链表中的节点 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点.你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换). 示例 1: 输入:head = [1, ...
- Python Pillow(PIL 第三方模块)和 cv2 (opencv第三方模块)对图片的 resize 操作 (缩放图片大小)
PIL 模块的 resize 操作: 1. 从文件中读取图片,然后 resize 大小: import matplotlib.pyplot as plt import numpy as np ...
- 【深度学习的linux显卡服务器维护记录】 服务器cuda不能用,nvidia-smi报错“Failed to initialize NVML: Driver/library version mismatch”
如题,服务器报错: 查看日志: 发现问题: Starting Daily apt upgrade and clean activities... 这个 apt upgrade 不是普通的update, ...
- 【转载】 t-SNE是什么? —— 使用指南
原文地址: https://www.cnblogs.com/LuckBelongsToStrugglingMan/p/14161405.html 转者前言: 该文相当于一个 t-SNE 使用指南, ...