這篇文章將使用兩個例子計算兩個日期的時間差
計算兩個Date之間的時間差
package com
import java
import java
public class DateDifferentExample {
public static void main(String[] args) {
String dateStart =
String dateStop =
SimpleDateFormat format = new SimpleDateFormat(
Date d
Date d
try {
d
d
//毫秒ms
long diff = d
long diffSeconds = diff /
long diffMinutes = diff / (
long diffHours = diff / (
long diffDays = diff / (
System
System
System
System
System
} catch (Exception e) {
e
}
}
}
運行結果
兩個時間相差
package com
import java
import java
import org
import org
import org
import org
import org
public class JodaDateDifferentExample {
public static void main(String[] args) {
String dateStart =
String dateStop =
SimpleDateFormat format = new SimpleDateFormat(
Date d
Date d
try {
d
d
DateTime dt
DateTime dt
System
System
System
+
System
+
System
+
} catch (Exception e) {
e
}
}
}
運行結果
兩個時間相差
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26928.html