通过Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析 ,可以看出java8设计非常好,实现接口Temporal, TemporalAdjuster, ChronoLocalDate等,有非常丰富的方法。例如:LocalDateTime:的部分方法:

  

包含了年月日,时分秒的加和减。Date中如果要进行加减,必须使用Calendar才可以。现在通过将Date转换为LocalDateTime,就能非常方便,线程安全的加减年月日,时分秒等信息。

	// plus two times

	public static Date plusYears(Date date, long amountToAdd){
return plus(date, ChronoUnit.YEARS, amountToAdd);
} public static LocalDateTime plusYears(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.YEARS, amountToAdd);
} public static LocalDate plusYears(LocalDate localDate, long amountToAdd){
return (LocalDate) plus(localDate, ChronoUnit.YEARS, amountToAdd);
} public static Date plusMonths(Date date, long amountToAdd){
return plus(date, ChronoUnit.MONTHS, amountToAdd);
} public static LocalDateTime plusMonths(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.MONTHS, amountToAdd);
} public static LocalDate plusMonths(LocalDate localDate, long amountToAdd){
return (LocalDate) plus(localDate, ChronoUnit.MONTHS, amountToAdd);
} public static Date plusWeeks(Date date, long amountToAdd){
return plus(date, ChronoUnit.WEEKS, amountToAdd);
} public static LocalDateTime plusWeeks(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.WEEKS, amountToAdd);
} public static LocalDate plusWeeks(LocalDate localDate, long amountToAdd){
return (LocalDate) plus(localDate, ChronoUnit.WEEKS, amountToAdd);
} public static Date plusDays(Date date, long amountToAdd){
return plus(date, ChronoUnit.DAYS, amountToAdd);
} public static LocalDateTime plusDays(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.DAYS, amountToAdd);
} public static LocalDate plusDays(LocalDate localDate, long amountToAdd){
return (LocalDate) plus(localDate, ChronoUnit.DAYS, amountToAdd);
} public static Date plusHours(Date date, long amountToAdd){
return plus(date, ChronoUnit.HOURS, amountToAdd);
} public static LocalDateTime plusHours(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.HOURS, amountToAdd);
} public static LocalTime plusHours(LocalTime localTime, long amountToAdd){
return (LocalTime) plus(localTime, ChronoUnit.HOURS, amountToAdd);
} public static Date plusMinutes(Date date, long amountToAdd){
return plus(date, ChronoUnit.MINUTES, amountToAdd);
} public static LocalDateTime plusMinutes(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.MINUTES, amountToAdd);
} public static LocalTime plusMinutes(LocalTime localTime, long amountToAdd){
return (LocalTime) plus(localTime, ChronoUnit.MINUTES, amountToAdd);
} public static Date plusSeconds(Date date, long amountToAdd){
return plus(date, ChronoUnit.SECONDS, amountToAdd);
} public static LocalDateTime plusSeconds(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) plus(localDateTime, ChronoUnit.SECONDS, amountToAdd);
} public static LocalTime plusSeconds(LocalTime localTime, long amountToAdd){
return (LocalTime) plus(localTime, ChronoUnit.SECONDS, amountToAdd);
} // minus two times public static Date minusYears(Date date, long amountToAdd){
return minus(date, ChronoUnit.YEARS, amountToAdd);
} public static LocalDateTime minusYears(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.YEARS, amountToAdd);
} public static LocalDate minusYears(LocalDate localDate, long amountToAdd){
return (LocalDate) minus(localDate, ChronoUnit.YEARS, amountToAdd);
} public static Date minusMonths(Date date, long amountToAdd){
return minus(date, ChronoUnit.MONTHS, amountToAdd);
} public static LocalDateTime minusMonths(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.MONTHS, amountToAdd);
} public static LocalDate minusMonths(LocalDate localDate, long amountToAdd){
return (LocalDate) minus(localDate, ChronoUnit.MONTHS, amountToAdd);
} public static Date minusWeeks(Date date, long amountToAdd){
return minus(date, ChronoUnit.WEEKS, amountToAdd);
} public static LocalDateTime minusWeeks(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.WEEKS, amountToAdd);
} public static LocalDate minusWeeks(LocalDate localDate, long amountToAdd){
return (LocalDate) minus(localDate, ChronoUnit.WEEKS, amountToAdd);
} public static Date minusDays(Date date, long amountToAdd){
return minus(date, ChronoUnit.DAYS, amountToAdd);
} public static LocalDateTime minusDays(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.DAYS, amountToAdd);
} public static LocalDate minusDays(LocalDate localDate, long amountToAdd){
return (LocalDate) minus(localDate, ChronoUnit.DAYS, amountToAdd);
} public static Date minusHours(Date date, long amountToAdd){
return minus(date, ChronoUnit.HOURS, amountToAdd);
} public static LocalDateTime minusHours(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.HOURS, amountToAdd);
} public static LocalTime minusHours(LocalTime localTime, long amountToAdd){
return (LocalTime) minus(localTime, ChronoUnit.HOURS, amountToAdd);
} public static Date minusMinutes(Date date, long amountToAdd){
return minus(date, ChronoUnit.MINUTES, amountToAdd);
} public static LocalDateTime minusMinutes(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.MINUTES, amountToAdd);
} public static LocalTime minusMinutes(LocalTime localTime, long amountToAdd){
return (LocalTime) minus(localTime, ChronoUnit.MINUTES, amountToAdd);
} public static Date minusSeconds(Date date, long amountToAdd){
return minus(date, ChronoUnit.SECONDS, amountToAdd);
} public static LocalDateTime minusSeconds(LocalDateTime localDateTime, long amountToAdd){
return (LocalDateTime) minus(localDateTime, ChronoUnit.SECONDS, amountToAdd);
} public static LocalTime minusSeconds(LocalTime localTime, long amountToAdd){
return (LocalTime) minus(localTime, ChronoUnit.SECONDS, amountToAdd);
}

测试类:

@Test
public void dateCalculatorPlusTest(){
Date date = new Date();
System.out.println(date);
System.out.println(DateTimeConverterUtil.toLocalDateTime(date)); System.out.println(DateTimeCalculatorUtil.plusYears(date, 1));
System.out.println(DateTimeCalculatorUtil.plusMonths(date, 1));
System.out.println(DateTimeCalculatorUtil.plusWeeks(date, 1));
System.out.println(DateTimeCalculatorUtil.plusDays(date, 1));
System.out.println(DateTimeCalculatorUtil.plusHours(date, 1));
System.out.println(DateTimeCalculatorUtil.plusMinutes(date, 1));
System.out.println(DateTimeCalculatorUtil.plusSeconds(date, 1));
} @Test
public void dateCalculatorMinusTest(){
Date date = new Date();
System.out.println(date);
System.out.println(DateTimeConverterUtil.toLocalDateTime(date)); System.out.println(DateTimeCalculatorUtil.minusYears(date, 1));
System.out.println(DateTimeCalculatorUtil.minusMonths(date, 1));
System.out.println(DateTimeCalculatorUtil.minusWeeks(date, 1));
System.out.println(DateTimeCalculatorUtil.minusDays(date, 1));
System.out.println(DateTimeCalculatorUtil.minusHours(date, 1));
System.out.println(DateTimeCalculatorUtil.minusMinutes(date, 1));
System.out.println(DateTimeCalculatorUtil.minusSeconds(date, 1));
} @Test
public void dateCalculatorPlusTest2(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt); System.out.println(DateTimeCalculatorUtil.plusYears(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusMonths(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusWeeks(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusDays(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusHours(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusMinutes(ldt, 1));
System.out.println(DateTimeCalculatorUtil.plusSeconds(ldt, 1));
} @Test
public void dateCalculatorMinusTest2(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt); System.out.println(DateTimeCalculatorUtil.minusYears(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusMonths(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusWeeks(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusDays(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusHours(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusMinutes(ldt, 1));
System.out.println(DateTimeCalculatorUtil.minusSeconds(ldt, 1));
}

输出:

Wed Feb 05 14:27:30 CST 2020
2020-02-05T14:27:30.398
Fri Feb 05 14:27:30 CST 2021
Thu Mar 05 14:27:30 CST 2020
Wed Feb 12 14:27:30 CST 2020
Thu Feb 06 14:27:30 CST 2020
Wed Feb 05 15:27:30 CST 2020
Wed Feb 05 14:28:30 CST 2020
Wed Feb 05 14:27:31 CST 2020

Wed Feb 05 14:27:43 CST 2020

2020-02-05T14:27:43.498

Tue Feb 05 14:27:43 CST 2019

Sun Jan 05 14:27:43 CST 2020

Wed Jan 29 14:27:43 CST 2020

Tue Feb 04 14:27:43 CST 2020

Wed Feb 05 13:27:43 CST 2020

Wed Feb 05 14:26:43 CST 2020

Wed Feb 05 14:27:42 CST 2020

2020-02-05T14:27:54.747

2021-02-05T14:27:54.747

2020-03-05T14:27:54.747

2020-02-12T14:27:54.747

2020-02-06T14:27:54.747

2020-02-05T15:27:54.747

2020-02-05T14:28:54.747

2020-02-05T14:27:55.747

2020-02-05T14:27:54.747

2021-02-05T14:27:54.747

2020-03-05T14:27:54.747

2020-02-12T14:27:54.747

2020-02-06T14:27:54.747

2020-02-05T15:27:54.747

2020-02-05T14:28:54.747

2020-02-05T14:27:55.747

源代码地址:https://github.com/xkzhangsan/xk-time

Java日期时间API系列15-----Jdk8中java.time包中的新的日期时间API类,java日期计算2,年月日时分秒的加减等的更多相关文章

  1. 时间格式的转化 vue与js 年月日 时分秒

    首先使用原生转化的方法 第一种 //时间转换 dateStr(d, sign) { //如果没有传递符号,给一个默认的符号 if (!sign) { sign = '-' } //获取d里面年月日时分 ...

  2. JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;

    学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...

  3. 在vue项目中显示实时时间(年月日时分秒)

    1.在data中定义一个变量,存储时间 data(){ return { nowTime:'' } }, 2.给定一个div <div>{{nowTime}}</div> 3. ...

  4. js获取当前时间的年月日时分秒以及时间的格式化

    1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...

  5. Sql 中获取年月日时分秒的函数

    getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...

  6. jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒

    sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...

  7. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  8. [置顶] java得到前一个月的年月日时分秒

    import java.util.Calendar; /** * 得到前一个月的年月日时分秒 * @author Mr.hu * 2013-6-28上午12:00:35 * Class Explain ...

  9. 在swt中获取jar包中的文件 uri is not hierarchical

    uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...

  10. C语言 - 获取系统时间 以年月日时分秒的形式输出

    ESP32需要给下位机通过UART发送时间戳,形式是年月日时分秒的十六进制数据包. #include <stdio.h> #include <time.h> int main( ...

随机推荐

  1. 亚马逊 vpc 子网 路由表 互联网网关 弹性ip

    创建vpc,子网,路由表,互联网网关,弹性ip等网络资源 vpc和子网 创建互联网网关 附加到vpc 创建路由表 路由表编辑路由 此路由通过这个网关出去 编辑子网关联 保存关联 有关云主机 创建属于那 ...

  2. Jmeter函数助手27-urlencode

    urlencode函数用于将字符串进行application/x-www-form-urlencoded编码格式化. String to encode in URL encoded chars:填入字 ...

  3. Efficientvit: Enhanced linear attention for high-resolution low-computation visual recognition

    GitHub地址: https://github.com/mit-han-lab/efficientvit#usage 相关: https://www.jetson-ai-lab.com/vit/tu ...

  4. 构建人工智能的工具 —— VXscan-R:数字孪生环境软件模块

    地址: https://www.creaform3d.com.cn/zh/ji-liang-jie-jue-fang/vxscan-rshu-zi-luan-sheng-huan-jing-ruan- ...

  5. 对于围棋AI作弊的一些思考

    最近看到些关于围棋AI作弊的报道有了一些思考. 相关视频链接: https://www.bilibili.com/video/BV1np411f73b/?spm_id_from=autoNext ht ...

  6. CUDA编译.cu文件报错unsupported GNU version! gcc versions later than 10 are not supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check;

    最近使用cuda11.3编译.cu文件,报错: #error -- unsupported GNU version! gcc versions later than 10 are not suppor ...

  7. 海豚²来了丨DolphinDB 集成 DolphinScheduler,任务调度更轻松

    DolphinDB 是一款高性能时序数据库.DolphinDB 集成了功能强大的编程语言和高容量高速度的批流一体数据分析系统,为海量数据(特别是时间序列数据)的快速存储.检索.计算及分析提供一站式解决 ...

  8. 软件开发工程师,几款常用的APP,你用过几款?最后一个测试网络必备

    作为一名程序员,手机里一定有几个常用的app,下面给大家推荐几款. 1. CSDN 国内最大编程论坛:虽然有多少人吐槽现在使用csdn就像屎里淘金, 但是不得不承认他仍然是大家搜索技术资料.问题的首选 ...

  9. Blazor开发框架Known-V2.0.8

    V2.0.8 Known是基于Blazor的企业级快速开发框架,低代码,跨平台,开箱即用,一处代码,多处运行.目前已有部分客户在使用,最近客户的项目和产品,有的在Docker中运行,有的在重新升级改造 ...

  10. 【Appium】之自动化定位总结

    一.同级定位时,先定位上级 我想定位[必填]框,我先定位[姓名]的同一个上级 self.driver.find_element(MobileBy.XPATH,"//*[contains(@t ...