Java计算时间差_传统的SimpleDateFormat类
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java计算时间差_传统的SimpleDateFormat类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Java計算時間差_傳統的SimpleDateFormat類
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");/*天數差*/Date fromDate1 = simpleFormat.parse("2022-03-23 12:00");Date toDate1 = simpleFormat.parse("2022-03-23 13:00");long from1 = fromDate1.getTime();long to1 = toDate1.getTime();int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));System.out.println("兩個時間之間的天數差為:" + days);/*小時差*/Date fromDate2 = simpleFormat.parse("2022-03-23 12:00");Date toDate2 = simpleFormat.parse("2022-03-23 13:00");long from2 = fromDate2.getTime();long to2 = toDate2.getTime();int hours = (int) ((to2 - from2) / (1000 * 60 * 60));System.out.println("兩個時間之間的小時差為:" + hours);/*分鐘差*/Date fromDate3 = simpleFormat.parse("2022-03-23 12:00");Date toDate3 = simpleFormat.parse("2022-03-23 13:00");long from3 = fromDate3.getTime();long to3 = toDate3.getTime();int minutes = (int) ((to3 - from3) / (1000 * 60));System.out.println("兩個時間之間的分鐘差為:" + minutes);輸出:
如果你要把date改為12小時制的 ,也就是 SimpleDateFormat simpleFormat = new
 SimpleDateFormat(“yyyy-MM-dd hh:mm”); 不然算出來的分鐘數是不對的。
總結
以上是生活随笔為你收集整理的Java计算时间差_传统的SimpleDateFormat类的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 上周卖出2.4亿美元亚马逊股票后 贝索斯
- 下一篇: rust火箭基地主楼开启方法_Rust
