计算日期时间间隔,可计算情侣计算在一起的。精确到,天,时,分,秒(输入的日期距离现在有:819天19670小时1180221分钟70813262秒)
生活随笔
收集整理的這篇文章主要介紹了
计算日期时间间隔,可计算情侣计算在一起的。精确到,天,时,分,秒(输入的日期距离现在有:819天19670小时1180221分钟70813262秒)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;/*** 情侶計算在一起的時間,天,時,分,秒* @author silence*/
public class Demo2 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss");while (true) {System.out.println("請輸入日期:(列:1970-01-01)");String next = scanner.next();LocalDateTime start = null;try {//格式化起始時間,提供默認時間輸入日期的0時0分0秒start = LocalDateTime.parse(next + " 00:00:00", formatter);} catch (Exception e) {System.out.println("輸入的日期格式錯誤,請輸入正確的日期格式如:1970-01-01");continue;}//獲得當前時間LocalDateTime end = LocalDateTime.now();//計算時間間隔Duration between = Duration.between(start, end);long days = between.toDays();//獲取此持續時間內的小時數。long hours = between.toHours();//獲取此持續時間的分鐘數。long minutes = between.toMinutes();//獲取此持續時間內的秒數。long seconds = between.getSeconds();//如果輸入的時間大于當前的分鐘,重新輸入if (minutes <= 0) {System.out.println("不能輸入當前時間之后的時間日期");continue;}System.out.println("輸入的日期距離現在有:" + days + "天" + hours + "小時" + minutes + "分鐘"+seconds+"秒");break;}}
}
總結
以上是生活随笔為你收集整理的计算日期时间间隔,可计算情侣计算在一起的。精确到,天,时,分,秒(输入的日期距离现在有:819天19670小时1180221分钟70813262秒)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为Mate 50系列曝光:屏幕为双挖孔
- 下一篇: 只要2199元 小米推出536L超大冰箱