当前位置:网站首页>LocalDateTime的详细使用方法
LocalDateTime的详细使用方法
2022-08-04 21:40:00 【m0_67595943】
有时候,我们需要日期的计算,比如在当前日期的基础上移动X天是什么时候,或者在指定的某一天的基础上移动X天是什么时候,等等。。
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();
//要移动的天数num,可以是整数或者负数
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);
//要移动的天数num,可以是整数或者负数
LocalDate ll = ld.plusDays(num);
System.out.println("指定日期是: " + ld + "------再过" + num + "天是:" + ll);
}
输出结果
今天是: 2018-12-22------再过3天是:2018-12-25
指定日期是: 2016-12-12------再过6天是:2016-12-18
之前我也碰到过类似的问题,但是的解决方案是,感觉还是LocalDate简单
1、在当前基础上后退
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();
//推后一定天数
calendar.add(Calendar.DATE,num);
//返回一个Date对象,表示此Calendar的时间值
date = calendar.getTime();
String dateTime = format.format(date);
return dateTime;
}
}
2、在某一日期之后后退
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,只会获取几时几分几秒
LocalTime localTime = LocalTime.now();
//创建LocalDateTime,获取年月日时分秒,等于LocalDate+LocalTime
LocalDateTime localDateTime = LocalDateTime.now();
}
(二)Duration 或 ChronoUnit 计算两个时间的间隔
Duration 类计算时间间隔
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();
// 相差纳秒
long nanos = duration.toNanos();
ChronoUnit 类计算时间间隔
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();
//要移动的天数num,可以是整数或者负数
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);
//要移动的天数num,可以是整数或者负数
LocalDate ll = ld.plusDays(num);
System.out.println("指定日期是: " + ld + "------再过" + num + "天是:" + ll);
}
今天是: 2018-12-22------再过3天是:2018-12-25
指定日期是: 2016-12-12------再过6天是:2016-12-18
1、在当前基础上后退
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();
//推后一定天数
calendar.add(Calendar.DATE,num);
//返回一个Date对象,表示此Calendar的时间值
date = calendar.getTime();
String dateTime = format.format(date);
return dateTime;
}
}
2、在某一日期之后后退
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);
边栏推荐
- SPSS-System Clustering Software Practice
- Altium Designer 19.1.18 - draw polygons copper hollow out, for the cursor just capture solutions
- Some problems with passing parameters of meta and params in routing (can be passed but not passed, empty, collocation, click to pass multiple parameters to report an error)
- 打卡第 2 天: urllib简记
- 立方度量(Cubic Metric)
- 【SQL之降龙十八掌】01——亢龙有悔:入门10题
- dotnet delete read-only files
- [21 days learning challenge - kernel notes] (2), based in the device tree
- Is the International Project Manager PMP certificate worth taking?
- PCBA scheme design - kitchen voice scale chip scheme
猜你喜欢
【SQL之降龙十八掌】01——亢龙有悔:入门10题
com.jacob.com.ComFailException: Invoke of: ActiveDocument
MySQL查询为啥慢了?
Hands-on Deep Learning_NiN
硬件开发定制全流程解析
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
NFT宝典:你需要知道NFT的术语和定义
【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
七夕特制:《牛郎会织女》
随机推荐
ROS播包可视化
2022强网杯web(部分)
信创是什么意思?涉及哪些行业?为什么要发展信创?
Webmine Webpage Mining Trojan Analysis and Disposal
ES6高级-Promise的用法
Flutter 实现背景图片毛玻璃效果
Arduino 电机测速
2022年江苏省大学生电子设计竞赛(TI杯)B题 飞机 省级一等奖记录 “一个摆烂人的独白”
LayaBox---TypeScript---Problems encountered at first contact
LeetCode 199: 二叉树的右视图
强网杯2022——WEB
In action: 10 ways to implement delayed tasks, with code!
1.读写点云文件
ES6高级-async的用法
Go----Go 语言基础之标识符、关键字、命名规范、变量、常量
如何一键重装Win11系统 一键重装系统方法
UDP通信
C language knowledge (1) - overview of C language, data types
JdbcTemplate概述和测试
Develop your own text recognition application with Tesseract