当前位置:网站首页>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);
边栏推荐
猜你喜欢
随机推荐
Chapter7 : Network-Driven Drug Discovery
Altium Designer 19.1.18 - Protecting Locked Objects
3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
OC-类簇
AtCoder Beginner Contest 262 D - I Hate Non-integer Number
Several ways for rk3399 to drive screen parameters
数字重塑客观世界,全空间GIS发展正当其时
Yolov7:Trainable bag-of-freebies sets new state-of-the-art for real-time objectdetectors
热力学相关的两个定律
boostrap多选PID查找端口 window
炽热如初 向新而生|ISC2022 HackingClub白帽峰会圆满举办!
Qt面试题整理
《剑指offer》刷题分类
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd6 in position 120: invalid continuation byte
开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
Milvus configuration related
UDP communication
ES 数据聚合、数据同步、集群
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
SPSS-System Clustering Software Practice









