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

 

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

 // modify property		public static Date withYear(Date date, long newValue){		return with(date, ChronoField.YEAR, newValue);	}		public static LocalDateTime withYear(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.YEAR, newValue);	}		public static LocalDate withYear(LocalDate localDate, long newValue){		return (LocalDate) with(localDate, ChronoField.YEAR, newValue);	}		public static Date withMonth(Date date, long newValue){		return with(date, ChronoField.MONTH_OF_YEAR, newValue);	}		public static LocalDateTime withMonth(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.MONTH_OF_YEAR, newValue);	}		public static LocalDate withMonth(LocalDate localDate, long newValue){		return (LocalDate) with(localDate, ChronoField.MONTH_OF_YEAR, newValue);	}		public static Date withDayOfMonth(Date date, long newValue){		return with(date, ChronoField.DAY_OF_MONTH, newValue);	}		public static LocalDateTime withDayOfMonth(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.DAY_OF_MONTH, newValue);	}		public static LocalDate withDayOfMonth(LocalDate localDate, long newValue){		return (LocalDate) with(localDate, ChronoField.DAY_OF_MONTH, newValue);	}			public static Date withDayOfYear(Date date, long newValue){		return with(date, ChronoField.DAY_OF_YEAR, newValue);	}		public static LocalDateTime withDayOfYear(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.DAY_OF_YEAR, newValue);	}		public static LocalDate withDayOfYear(LocalDate localDate, long newValue){		return (LocalDate) with(localDate, ChronoField.DAY_OF_YEAR, newValue);	}		public static Date withHour(Date date, long newValue){		return with(date, ChronoField.HOUR_OF_DAY, newValue);	}		public static LocalDateTime withHour(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.HOUR_OF_DAY, newValue);	}		public static LocalTime withHour(LocalTime localTime, long newValue){		return (LocalTime) with(localTime, ChronoField.HOUR_OF_DAY, newValue);	}		public static Date withMinute(Date date, long newValue){		return with(date, ChronoField.MINUTE_OF_HOUR, newValue);	}		public static LocalDateTime withMinute(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.MINUTE_OF_HOUR, newValue);	}		public static LocalTime withMinute(LocalTime localTime, long newValue){		return (LocalTime) with(localTime, ChronoField.MINUTE_OF_HOUR, newValue);	}		public static Date withSecond(Date date, long newValue){		return with(date, ChronoField.SECOND_OF_MINUTE, newValue);	}		public static LocalDateTime withSecond(LocalDateTime localDateTime, long newValue){		return (LocalDateTime) with(localDateTime, ChronoField.SECOND_OF_MINUTE, newValue);	}		public static LocalTime withSecond(LocalTime localTime, long newValue){		return (LocalTime) with(localTime, ChronoField.SECOND_OF_MINUTE, newValue);	}

测试类:

 @Test	public void dateCalculatorWithTest(){		Date date = new Date();		System.out.println(date);		System.out.println(DateTimeConverterUtil.toLocalDateTime(date));		System.out.println(DateTimeCalculatorUtil.getDayOfYear(date));				System.out.println(DateTimeCalculatorUtil.withYear(date, 2021));		System.out.println(DateTimeCalculatorUtil.withMonth(date, 3));		System.out.println(DateTimeCalculatorUtil.withDayOfMonth(date, 6));		System.out.println(DateTimeCalculatorUtil.withDayOfYear(date, 37));		System.out.println(DateTimeCalculatorUtil.withHour(date, 17));		System.out.println(DateTimeCalculatorUtil.withMinute(date, 30));		System.out.println(DateTimeCalculatorUtil.withSecond(date, 30));	}		@Test	public void dateCalculatorWithTest2(){		LocalDateTime ldt = LocalDateTime.now();		System.out.println(ldt);		System.out.println(ldt.getDayOfYear());				System.out.println(DateTimeCalculatorUtil.withYear(ldt, 2021));		System.out.println(DateTimeCalculatorUtil.withMonth(ldt, 3));		System.out.println(DateTimeCalculatorUtil.withDayOfMonth(ldt, 6));		System.out.println(DateTimeCalculatorUtil.withDayOfYear(ldt, 37));		System.out.println(DateTimeCalculatorUtil.withHour(ldt, 17));		System.out.println(DateTimeCalculatorUtil.withMinute(ldt, 30));		System.out.println(DateTimeCalculatorUtil.withSecond(ldt, 30));	}

输出:

Wed Feb 05 16:20:47 CST 20202020-02-05T16:20:47.95536Fri Feb 05 16:20:47 CST 2021Thu Mar 05 16:20:47 CST 2020Thu Feb 06 16:20:47 CST 2020Thu Feb 06 16:20:47 CST 2020Wed Feb 05 17:20:47 CST 2020Wed Feb 05 16:30:47 CST 2020Wed Feb 05 16:20:30 CST 2020
 
 
2020-02-05T16:21:03.760362021-02-05T16:21:03.7602020-03-05T16:21:03.7602020-02-06T16:21:03.7602020-02-06T16:21:03.7602020-02-05T17:21:03.7602020-02-05T16:30:03.7602020-02-05T16:21:30.760

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

Java日期时间API系列16-----Jdk8中java.time包中的新的日期时间API类,java日期计算3,日期中年月日时分秒的属性值修改等的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. jquery获取年月日时分秒当前时间

    获取年月日时分秒的当前时间,可按照某种格式显示出来,下面是一种得到如2017年02月02日  00:00:00格式的当前时间 function getCurrentDate(date){ var y ...

随机推荐

  1. 强化学习中经典算法 —— reinforce算法 —— (进一步理解, 理论推导出的计算模型和实际应用中的计算模型的区别)

    在奖励折扣率为1的情况下,既没有折扣的情况下,reinforce算法理论上可以写为: 但是在有折扣的情况下,reinforce算法理论上可以写为: 以上均为理论模型. ================ ...

  2. Hessian Free Optimization——外国网友分享的“共轭梯度”的推导

    外国网友分享的"共轭梯度"的推导: https://andrew.gibiansky.com/blog/machine-learning/hessian-free-optimiza ...

  3. 关于我升级VS16.8,结果一些项目运行报错“Phx.FatalError”这件事

    背景 不知道啥时候开始,一些的项目不能好好运行了.一运行就报错 解决办法 https://developercommunity.visualstudio.com/content/problem/125 ...

  4. 洛谷P1209修理牛棚 Barn Repair

    [USACO1.3] 修理牛棚 Barn Repair 题目描述 在一个月黑风高的暴风雨夜,Farmer John 的牛棚的屋顶.门被吹飞了 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一 ...

  5. 使用 createError 创建错误对象的详细指南

    title: 使用 createError 创建错误对象的详细指南 date: 2024/8/8 updated: 2024/8/8 author: cmdragon excerpt: 摘要:本文介绍 ...

  6. 在一串字符串中Java使用正则匹配电话号码的方法

    1.使用正则表达式来匹配电话号码 在Java中,使用正则表达式匹配电话号码是一个常见的需求.电话号码的格式可能因国家/地区而异,但一个典型的格式可能是这样的:(123) 456-7890.在这个例子中 ...

  7. posix是什么都不知道,还好意思说你懂Linux?

    Linux开发者越来越多,但是仍然有很多人整不明白POSIX是什么.本文就带着大家来了解一下到底什么是POSIX,了解他的历史和重要性. 一.什么是posix? 1. 概念 POSIX:可移植操作系统 ...

  8. nginx配置请求头防止点击劫持

    在返回index.html配置中加入add_header X-Frame-Options DENY; location / {undefined root /data/nginx/html/dist/ ...

  9. 组合数取模的几种方法--Exlucas&杨辉三角&组合

    组合数取模的几个方法 求: \[C^{m}_{n} \bmod P \] 1.杨辉三角法 \[C^{m}_{n} = C^{m - 1}_{n - 1} + C^{ m }_{n - 1} \] 时间 ...

  10. Ubuntu Server 部署 FRP 反向代理

    踩坑记录 我使用的配置文件是官方提供的示例配置文件 通过 SSH 访问内网机器,应该没有问题. 第一次我使用 Docker 镜像 snowdreamtech/frps 在服务器上部署 frps,发现始 ...