這篇文章將使用兩個(gè)例子計(jì)算兩個(gè)日期的時(shí)間差。
1.使用Java SDK。
2.使用Joda庫(kù)。
1.使用Java SDK
計(jì)算兩個(gè)Date之間的時(shí)間差,基本思路為把Date轉(zhuǎn)換為ms(微秒),然后計(jì)算兩個(gè)微秒時(shí)間差。時(shí)間的兌換規(guī)則如下:
1s秒 = 1000ms毫秒 1min分種 = 60s秒 1hours小時(shí) = 60min分鐘 1day天 = 24hours小時(shí) |
package com.qiyadeng.date;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDifferentExample {
public static void main(String[] args) {
String dateStart = "2013-02-19 09:29:58";
String dateStop = "2013-02-20 11:31:48";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//毫秒ms
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print("兩個(gè)時(shí)間相差:");
System.out.print(diffDays + " 天, ");
System.out.print(diffHours + " 小時(shí), ");
System.out.print(diffMinutes + " 分鐘, ");
System.out.print(diffSeconds + " 秒.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDifferentExample {
public static void main(String[] args) {
String dateStart = "2013-02-19 09:29:58";
String dateStop = "2013-02-20 11:31:48";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//毫秒ms
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print("兩個(gè)時(shí)間相差:");
System.out.print(diffDays + " 天, ");
System.out.print(diffHours + " 小時(shí), ");
System.out.print(diffMinutes + " 分鐘, ");
System.out.print(diffSeconds + " 秒.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
兩個(gè)時(shí)間相差:1 天, 2 小時(shí), 1 分鐘, 50 秒. |
2.Joda時(shí)間庫(kù)
package com.qiyadeng.date;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
public class JodaDateDifferentExample {
public static void main(String[] args) {
String dateStart = "2013-02-19 09:29:58";
String dateStop = "2013-02-20 11:31:48";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print("兩個(gè)時(shí)間相差:");
System.out.print(Days.daysBetween(dt1, dt2).getDays() + " 天, ");
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24
+ " 小時(shí), ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60
+ " 分鐘, ");
System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60
+ " 秒.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
public class JodaDateDifferentExample {
public static void main(String[] args) {
String dateStart = "2013-02-19 09:29:58";
String dateStop = "2013-02-20 11:31:48";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print("兩個(gè)時(shí)間相差:");
System.out.print(Days.daysBetween(dt1, dt2).getDays() + " 天, ");
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24
+ " 小時(shí), ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60
+ " 分鐘, ");
System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60
+ " 秒.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
兩個(gè)時(shí)間相差:1 天, 2 小時(shí), 1 分鐘, 50 秒. |
原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明: 轉(zhuǎn)載自http://www.qiyadeng.com/
本文鏈接地址: Java計(jì)算日期和時(shí)間差