当前位置:网站首页>Detailed usage of LocalDateTime
Detailed usage of LocalDateTime
2022-08-04 22:02:00 【m0_67595943】
有时候,We need date calculations,Such as moving based on the current dateXWhat time is the day,Or move on a specified day basisXWhat time is the day,等等..
public static void main(String[] args) {
DateDemo.nowDateMove(3);
DateDemo.unKnownDateMove(2016,12,12,6);
}
public static void nowDateMove(int num) {
//获取当前日期
LocalDate ld = LocalDate.now();
//The number of days to movenum,Can be an integer or a negative number
LocalDate ll = ld.plusDays(num);
System.out.println("今天是: " + ld + "------再过" + num + "天是:" + ll);
}
public static void unKnownDateMove(int year, int month, int day, int num) {
//指定日期
LocalDate ld = LocalDate.of(year, month, day);
//The number of days to movenum,Can be an integer or a negative number
LocalDate ll = ld.plusDays(num);
System.out.println("The specified date is: " + ld + "------再过" + num + "天是:" + ll);
}
输出结果
今天是: 2018-12-22------再过3天是:2018-12-25
The specified date is: 2016-12-12------再过6天是:2016-12-18
I had a similar problem before,But the solution is,感觉还是LocalDate简单
1、Step back from the current basis
public class DateMoveUtil {
/**
* 日期计算 在当前日期的基础上,推后num天
* @param num
* @return
*/
public String dateFun(int num) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
//Postponed by a certain number of days
calendar.add(Calendar.DATE,num);
//返回一个Date对象,表示此Calendar的时间值
date = calendar.getTime();
String dateTime = format.format(date);
return dateTime;
}
}
2、Go back after a certain date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(“2018-10-10”);
Date date1 = new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000L);
String time = format.format(date1);
仅供参考,如果大家有好的方法,虚心请教.
(一) LocalDate与LocalDateTime初始化日期,对日期加减.
public static void main(String[] args){
//获取当前时间(年月日)
LocalDate localDate=LocalDate.now();
//格式化日期(DateTimeFormatter是线程安全的,用于localData的时间格式化)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date= LocalDate.parse(localDate.toString(), formatter); //2022-05-23
//增加一年
LocalDate nextYear = date.plusYears(1); //2023-05-23
//减少一年
LocalDate lastYear = date.minusYears(1); //2021-05-23
//增加一个月
LocalDate nextMonths= date.plusMonths(1); //2021-06-23
//增加一天
LocalDate nextDays= date.plusDays(1); //2021-05-24
//构造指定的年月日
LocalDate specifiedDate = LocalDate.of(2022, 5, 23);
//年
int year = specifiedDate.getYear(); //2022
//月(这里用到了ChronoField类,有兴趣的可以去了解一下)
int month1 = specifiedDate.get(ChronoField.MONTH_OF_YEAR); //5
//日
int day = specifiedDate.getDayOfMonth(); //23
//创建LocalTime,Only get hours, minutes and seconds
LocalTime localTime = LocalTime.now();
//创建LocalDateTime,获取年月日时分秒,等于LocalDate+LocalTime
LocalDateTime localDateTime = LocalDateTime.now();
}
(二)Duration 或 ChronoUnit 计算两个时间的间隔
Duration The class calculates the interval
LocalDateTime time = LocalDateTime.parse("2022-06-28T08:41:00");
LocalDateTime time1 = LocalDateTime.parse("2022-06-28T08:42:00");
Duration duration = Duration.between(time, time1);
// 相差天数
long datys = duration.toDays();
// 相差小时
long hours = duration.toHours();
// 相差分钟
long minutes = duration.toMinutes();
// 相差秒数
long seconds = duration.getSeconds();
// 相差毫秒
long millis = duration.toMillis();
// difference in nanoseconds
long nanos = duration.toNanos();
ChronoUnit The class calculates the interval
LocalDateTime time = LocalDateTime.parse("2022-06-28T08:41:00");
LocalDateTime time1 = LocalDateTime.parse("2022-06-28T08:42:00");
//相差月
long cuMonths = ChronoUnit.MONTHS.between(time, time1);
//相差天数
long cuDatys = ChronoUnit.DAYS.between(time, time1);
//相差小时
long cuHours = ChronoUnit.HOURS.between(time, time1);
//相差分钟
long cuMinutes = ChronoUnit.MINUTES.between(time, time1);
//相差秒
long cuSeconds = ChronoUnit.SECONDS.between(time, time1);
public static void main(String[] args) {
DateDemo.nowDateMove(3);
DateDemo.unKnownDateMove(2016,12,12,6);
}
public static void nowDateMove(int num) {
//获取当前日期
LocalDate ld = LocalDate.now();
//The number of days to movenum,Can be an integer or a negative number
LocalDate ll = ld.plusDays(num);
System.out.println("今天是: " + ld + "------再过" + num + "天是:" + ll);
}
public static void unKnownDateMove(int year, int month, int day, int num) {
//指定日期
LocalDate ld = LocalDate.of(year, month, day);
//The number of days to movenum,Can be an integer or a negative number
LocalDate ll = ld.plusDays(num);
System.out.println("The specified date is: " + ld + "------再过" + num + "天是:" + ll);
}
今天是: 2018-12-22------再过3天是:2018-12-25
The specified date is: 2016-12-12------再过6天是:2016-12-18
1、Step back from the current basis
public class DateMoveUtil {
/**
* 日期计算 在当前日期的基础上,推后num天
* @param num
* @return
*/
public String dateFun(int num) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
//Postponed by a certain number of days
calendar.add(Calendar.DATE,num);
//返回一个Date对象,表示此Calendar的时间值
date = calendar.getTime();
String dateTime = format.format(date);
return dateTime;
}
}
2、Go back after a certain date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(“2018-10-10”);
Date date1 = new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000L);
String time = format.format(date1);
边栏推荐
猜你喜欢

炽热如初 向新而生|ISC2022 HackingClub白帽峰会圆满举办!

第二讲 软件生命周期
![Rt-thread [二] 系统初始化流程](/img/46/6e2942e4c18c0220378050205e6528.png)
Rt-thread [二] 系统初始化流程

如何一键重装win7系统?重装win7系统详细教程

EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?

【线性代数02】AX=b的2种解释和矩阵乘法的5种视角

Axure9基本交互操作(一)

Chapter7 : Network-Driven Drug Discovery

开源一夏 | 云服务器ECS安装Mysql、JDK、RocketMQ

Driving point cloud format changes bring efficiency improvement
随机推荐
OC-归档(序列化)(了解的不多 没细看)
ROS播包可视化
Exploration and Practice of Database Governance
Oracle增加表空间解决ORACLE ORA-01653: unable to extend table报错
如何在项目中正确使用WebSocket
OC-拷贝
打卡第 2 天: urllib简记
openresty lua-resty-template页面静态化
rk3399-9.0 first-level and second-level dormancy
软测人面试 ,HR 会问到哪些问题?学会涨薪3000+
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
搬走地下空间开发利用“绊脚石” 中地数码取得地下空间透明化技术突破
"Jianzhi offer" brush title classification
Lecture 2 Software Life Cycle
年薪40W测试工程师成长之路,你在哪个阶段?
Debian防火墙的开关以及状态
OC-协议
Rt-thread [三] link.lds链接脚本详解
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
y87.第五章 分布式链路追踪系统 -- 分布式链路追踪系统起源(一)