免费网站建站塔山双喜,电子商务网站建设pdf,怎么优化网站关键字,wordpress对比计算两个时间的间隔
方法#xff1a;between【静态方法】 1.获取相隔的年月日用Period调用#xff0c;参数用LocalDate类对象封装的时间 2.获取相隔的天年月日用Duration调用#xff0c;参数用LocalDateTime类对象封装的时间
//计算两个时间的间隔
public class Demo7 {pu…计算两个时间的间隔
方法between【静态方法】 1.获取相隔的年月日用Period调用参数用LocalDate类对象封装的时间 2.获取相隔的天年月日用Duration调用参数用LocalDateTime类对象封装的时间
//计算两个时间的间隔
public class Demo7 {public static void main(String[] args) {//Period[年月日],需要LocalDate对象LocalDateTime ldt1 LocalDateTime.of(2021, 06, 29, 10, 10, 10);LocalDateTime ldt2 LocalDateTime.of(2021, 11, 11, 00, 00, 00);//between为静态方法,Period调用后得到间隔对象Period period Period.between(ldt1.toLocalDate(),ldt2.toLocalDate());//前面的年份靠前,不然得到间隔的数为负数//获取相隔的年月日System.out.println(还有period.getYears()年 period.getMonths()月 period.getDays()天 );//-------------------------------------------------------------------------------------------//Duration[时分秒],需要LocalDateTime对象//between为静态方法,Duration调用后得到间隔对象// 对象不采用(ldt1.toLocalTime(), ldt2.toLocalTime())因为它的目标是只针对时分秒Duration duration Duration.between(ldt1, ldt2);//获取相隔的天时分秒//duration.toDaysPart()计算总天数System.out.println(duration.toDaysPart()天duration.toHoursPart()时 duration.toMinutesPart()分 duration.toSecondsPart()秒 );//不满一时的分[若前面分钟大,则得到60-]System.out.println(duration.toMinutesPart());//不满一分的秒System.out.println(duration.toSecondsPart());}
}打印结果
----------------------------------------------------------------------
还有0年 4月 13天
134天13时 49分 50秒
49
50判断一个字符串的时间是否为闰年
题目定义一个时间字符串2000-08-08 08:08:08求这一年是平年还是闰年 思路 1使用LocalDateTime的parse方法解析为LocalDateTime对象 2把LocalDateTime的时间设置到2000年的3月1日再减1天就到了二月的最后一天再获取这一天的是几号如果是28就是平年否则就是闰年 实现
public class Test3 {public static void main(String[] args) {String str 2000-08-08 08:08:08;LocalDateTime parse LocalDateTime.parse(str, DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss));//把LocalDateTime的时间设置到2000年的3月1日再减1天就到了二月的最后一天LocalDateTime ldt parse.withMonth(3).withDayOfMonth(1).plusDays(-1);int dayOfMonth ldt.getDayOfMonth();System.out.println(dayOfMonth);if(dayOfMonth28){System.out.println(平年);}else {System.out.println(闰年);}}
}打印结果
----------------------------------------------------------------------
29
闰年