当前位置:网站首页>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);
边栏推荐
- ES6高级-async的用法
- 第二讲 软件生命周期
- NFT宝典:你需要知道NFT的术语和定义
- 3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
- 立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新
- How to right use of WebSocket in project
- ES6高级-Promise的用法
- What does Xinchuang mean?Which industries are involved?Why develop Xinchuang?
- Codeforces Round #811 (Div. 3)
- 开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
猜你喜欢

【TCP/IP 五 ICMP】

In action: 10 ways to implement delayed tasks, with code!

【TCP/IP 四 IP 网际协议】

Cocoa Application-test

ES 数据聚合、数据同步、集群

Oracle增加表空间解决ORACLE ORA-01653: unable to extend table报错

y87.第五章 分布式链路追踪系统 -- 分布式链路追踪系统起源(一)

How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?

【社媒营销】WhatsApp Business API:您需要知道的一切

2022强网杯web(部分)
随机推荐
七夕特制:《牛郎会织女》
Lecture 2 Software Life Cycle
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
Autowired自动装配
国际项目管理师PMP证书,值得考嘛?
QT 子窗口—>主窗口 信号和槽的交互
立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新
Cocoa Application-基础
Autowired autowiring
puzzle(022.1)黑白迭代
快速web开发框架——learun framework
Unknown point cloud structure file conversion requirements
Cocoa Application-test
七夕特制:《牛郎会织女》
unity2D横版游戏教程9-对话框dialog
开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
Redis中的LRU算法
# #ifndef/#define/#endif使用详解
The use and principle of CountDownLatch
rk3399-9.0一级二级休眠