如何在java中找到两个日期之间的差异持续时间?
Posted
技术标签:
【中文标题】如何在java中找到两个日期之间的差异持续时间?【英文标题】:How to find the duration of difference between two dates in java? 【发布时间】:2013-07-30 04:43:48 【问题描述】:我有两个DateTime的对象,需要找到他们的区别的持续时间,
我有以下代码,但不知道如何继续它以达到预期的结果,如下所示:
示例:
11/03/14 09:30:58
11/03/14 09:33:43
elapsed time is 02 minutes and 45 seconds
-----------------------------------------------------
11/03/14 09:30:58
11/03/15 09:30:58
elapsed time is a day
-----------------------------------------------------
11/03/14 09:30:58
11/03/16 09:30:58
elapsed time is two days
-----------------------------------------------------
11/03/14 09:30:58
11/03/16 09:35:58
elapsed time is two days and 05 minutes
代码:
String dateStart = "11/03/14 09:29:58";
String dateStop = "11/03/14 09:33:43";
Custom date format
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
catch (ParseException e)
e.printStackTrace();
// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
【问题讨论】:
请查看 Joda time,它已内置支持此功能。 你的代码有什么问题,你只需要一些调整来实现所需的输出,让我们试试吧 先求小时差,余数求分,再求秒! @PeterLawrey 我提供了不同的例子 @aquestion duplication 表示两个问题的预期结果相同,这个问题的预期输出与您提供的不同。 【参考方案1】:如果有人想要一个字符串,所有这些都在一起,可以使用这个函数。
String getTimeDifference(long duration)
StringBuilder timeRemaining = new StringBuilder();
long days = TimeUnit.MILLISECONDS.toDays(duration);
if (days >= 1)
timeRemaining.append(days).append((days == 1) ? " day " : " days ");
duration -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(duration);
if (hours >= 1)
timeRemaining.append(hours).append((hours == 1) ? " hour " : " hours ");
duration -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
if (minutes >= 1)
timeRemaining.append(minutes).append((hours == 1) ? " minute " : " minutes ");
return timeRemaining.toString().trim();
【讨论】:
【参考方案2】:下面的代码将给出两个 DateTime 之间的区别(适用于 Java 8 及更高版本)
private long countDaysBetween(LocalDateTime startDate, LocalDateTime enddate)
if(startDate == null || enddate == null)
throw new IllegalArgumentException("No such a date");
long daysBetween = ChronoUnit.DAYS.between(startDate, enddate);
return daysBetween;
【讨论】:
【参考方案3】:您可以使用此方法获取两个 DateTime 之间的差异
DateTime startDate = DateTime.now();
DateTime endDate = DateTime.now();
Days daysBetween = Days.daysBetween(startDate, endDate);
System.out.println(daysBetween.toStandardSeconds());
【讨论】:
【参考方案4】:java.time.Duration
我仍然觉得没有任何答案是最新的和中肯的。所以这里是使用来自 java.time 的Duration
的现代答案,现代 Java 日期和时间 API(MayurB 和 mkobit 的答案提到了同一个类,但它们都没有按照要求正确转换为天、小时、分钟和分钟)。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss");
String dateStart = "11/03/14 09:29:58";
String dateStop = "11/03/14 09:33:43";
ZoneId zone = ZoneId.systemDefault();
ZonedDateTime startDateTime = LocalDateTime.parse(dateStart, formatter).atZone(zone);
ZonedDateTime endDateTime = LocalDateTime.parse(dateStop, formatter).atZone(zone);
Duration diff = Duration.between(startDateTime, endDateTime);
if (diff.isZero())
System.out.println("0 minutes");
else
long days = diff.toDays();
if (days != 0)
System.out.print("" + days + " days ");
diff = diff.minusDays(days);
long hours = diff.toHours();
if (hours != 0)
System.out.print("" + hours + " hours ");
diff = diff.minusHours(hours);
long minutes = diff.toMinutes();
if (minutes != 0)
System.out.print("" + minutes + " minutes ");
diff = diff.minusMinutes(minutes);
long seconds = diff.getSeconds();
if (seconds != 0)
System.out.print("" + seconds + " seconds ");
System.out.println();
这个例子 sn-p 的输出是:
3 分 45 秒
请注意,Duration
始终将一天视为 24 小时。如果您想以不同的方式处理诸如夏令时转换之类的时间异常,解决方案包括(1)使用ChronoUnit.DAYS
(2)使用Period
(3)Use
LocalDateTimeinstead of
ZonedDateTime`(可能被认为是黑客)。
上面的代码适用于 Java 8 和 ThreeTen Backport,即 java.time 到 Java 6 和 7 的反向移植。从 Java 9 开始,可以使用 toHoursPart
、@987654329 方法更好地编写它@ 和 toSecondsPart
添加在那里。
我会在有时间的时候进一步详细解释,可能要到下周。
【讨论】:
【参考方案5】:它对我有用,可以试试这个,希望它会有所帮助。如果有任何问题,请告诉我。
Date startDate = java.util.Calendar.getInstance().getTime(); //set your start time
Date endDate = java.util.Calendar.getInstance().getTime(); // set your end time
long duration = endDate.getTime() - startDate.getTime();
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);
Toast.makeText(MainActivity.this, "Diff"
+ duration + diffInDays + diffInHours + diffInMinutes + diffInSeconds, Toast.LENGTH_SHORT).show(); **// Toast message for android .**
System.out.println("Diff" + duration + diffInDays + diffInHours + diffInMinutes + diffInSeconds); **// Print console message for Java .**
【讨论】:
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);【参考方案6】:参考 shamim 的答案更新,这是一种无需使用任何第三方库即可完成任务的方法。只需复制方法并使用
public static String getDurationTimeStamp(String date)
String timeDifference = "";
//date formatter as per the coder need
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//parse the string date-ti
// me to Date object
Date startDate = null;
try
startDate = sdf.parse(date);
catch (ParseException e)
e.printStackTrace();
//end date will be the current system time to calculate the lapse time difference
//if needed, coder can add end date to whatever date
Date endDate = new Date();
System.out.println(startDate);
System.out.println(endDate);
//get the time difference in milliseconds
long duration = endDate.getTime() - startDate.getTime();
//now we calculate the differences in different time units
//this long value will be the total time difference in each unit
//i.e; total difference in seconds, total difference in minutes etc...
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);
//now we create the time stamps depending on the value of each unit that we get
//as we do not have the unit in years,
//we will see if the days difference is more that 365 days, as 365 days = 1 year
if (diffInDays > 365)
//we get the year in integer not in float
//ex- 791/365 = 2.167 in float but it will be 2 years in int
int year = (int) (diffInDays / 365);
timeDifference = year + " years ago";
System.out.println(year + " years ago");
//if days are not enough to create year then get the days
else if (diffInDays > 1)
timeDifference = diffInDays + " days ago";
System.out.println(diffInDays + " days ago");
//if days value<1 then get the hours
else if (diffInHours > 1)
timeDifference = diffInHours + " hours ago";
System.out.println(diffInHours + " hours ago");
//if hours value<1 then get the minutes
else if (diffInMinutes > 1)
timeDifference = diffInMinutes + " minutes ago";
System.out.println(diffInMinutes + " minutes ago");
//if minutes value<1 then get the seconds
else if (diffInSeconds > 1)
timeDifference = diffInSeconds + " seconds ago";
System.out.println(diffInSeconds + " seconds ago");
return timeDifference;
// that's all. Happy Coding :)
【讨论】:
【参考方案7】:使用 Java 内置类 TimeUnit 可以更好地处理日期差异转换。它提供了实用方法来做到这一点:
Date startDate = // Set start date
Date endDate = // Set end date
long duration = endDate.getTime() - startDate.getTime();
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);
【讨论】:
可选长 diffInSeconds = TimeUnit.SECONDS.convert(duration,TimeUnit.MILLSECONDS); 这是最好的答案。 我支持该议案;这个答案是最好的。 不依赖第三方库。 OP 需要天、小时和分钟的差异。小时和分钟不是绝对的,而是余数。此解决方案简洁但不提供相同的答案。【参考方案8】:我最近用一个简单的方法解决了类似的问题。
public static void main(String[] args) throws IOException, ParseException
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(utc);
Date until = calendar.getTime();
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date since = calendar.getTime();
long durationInSeconds = TimeUnit.MILLISECONDS.toSeconds(until.getTime() - since.getTime());
long SECONDS_IN_A_MINUTE = 60;
long MINUTES_IN_AN_HOUR = 60;
long HOURS_IN_A_DAY = 24;
long DAYS_IN_A_MONTH = 30;
long MONTHS_IN_A_YEAR = 12;
long sec = (durationInSeconds >= SECONDS_IN_A_MINUTE) ? durationInSeconds % SECONDS_IN_A_MINUTE : durationInSeconds;
long min = (durationInSeconds /= SECONDS_IN_A_MINUTE) >= MINUTES_IN_AN_HOUR ? durationInSeconds%MINUTES_IN_AN_HOUR : durationInSeconds;
long hrs = (durationInSeconds /= MINUTES_IN_AN_HOUR) >= HOURS_IN_A_DAY ? durationInSeconds % HOURS_IN_A_DAY : durationInSeconds;
long days = (durationInSeconds /= HOURS_IN_A_DAY) >= DAYS_IN_A_MONTH ? durationInSeconds % DAYS_IN_A_MONTH : durationInSeconds;
long months = (durationInSeconds /=DAYS_IN_A_MONTH) >= MONTHS_IN_A_YEAR ? durationInSeconds % MONTHS_IN_A_YEAR : durationInSeconds;
long years = (durationInSeconds /= MONTHS_IN_A_YEAR);
String duration = getDuration(sec,min,hrs,days,months,years);
System.out.println(duration);
private static String getDuration(long secs, long mins, long hrs, long days, long months, long years)
StringBuffer sb = new StringBuffer();
String EMPTY_STRING = "";
sb.append(years > 0 ? years + (years > 1 ? " years " : " year "): EMPTY_STRING);
sb.append(months > 0 ? months + (months > 1 ? " months " : " month "): EMPTY_STRING);
sb.append(days > 0 ? days + (days > 1 ? " days " : " day "): EMPTY_STRING);
sb.append(hrs > 0 ? hrs + (hrs > 1 ? " hours " : " hour "): EMPTY_STRING);
sb.append(mins > 0 ? mins + (mins > 1 ? " mins " : " min "): EMPTY_STRING);
sb.append(secs > 0 ? secs + (secs > 1 ? " secs " : " secs "): EMPTY_STRING);
sb.append("ago");
return sb.toString();
正如预期的那样,它会打印:7 days ago
。
【讨论】:
【参考方案9】: // calculating the difference b/w startDate and endDate
String startDate = "01-01-2016";
String endDate = simpleDateFormat.format(currentDate);
date1 = simpleDateFormat.parse(startDate);
date2 = simpleDateFormat.parse(endDate);
long getDiff = date2.getTime() - date1.getTime();
// using TimeUnit class from java.util.concurrent package
long getDaysDiff = TimeUnit.MILLISECONDS.toDays(getDiff);
How to calculate difference between two dates in Java
【讨论】:
【参考方案10】:使用Joda-Time库
DateTime startTime, endTime;
Period p = new Period(startTime, endTime);
long hours = p.getHours();
long minutes = p.getMinutes();
Joda Time 有一个时间间隔的概念:
Interval interval = new Interval(oldTime, new Instant());
还有一个例子 Date Difference
还有一个Link
或使用 Java-8(集成了 Joda-Time 概念)
Instant start, end;//
Duration dur = Duration.between(start, stop);
long hours = dur.toHours();
long minutes = dur.toMinutes();
【讨论】:
这应该是公认的答案。乔达时间是要走的路 正确处理时区、日光变化等的唯一安全方法 只是一个小错字:您在第二行中的意思是“结束”而不是“停止”(“Duration dur = Duration.between(start, stop);”)。【参考方案11】:这是我写的一个程序,它获取两个日期之间的天数(这里没有时间)。
import java.util.Scanner;
public class HelloWorld
public static void main(String args[])
Scanner s = new Scanner(System.in);
System.out.print("Enter starting date separated by dots: ");
String inp1 = s.nextLine();
System.out.print("Enter ending date separated by dots: ");
String inp2 = s.nextLine();
int[] nodim =
0,
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
;
String[] inpArr1 = split(inp1);
String[] inpArr2 = split(inp2);
int d1 = Integer.parseInt(inpArr1[0]);
int m1 = Integer.parseInt(inpArr1[1]);
int y1 = Integer.parseInt(inpArr1[2]);
int d2 = Integer.parseInt(inpArr2[0]);
int m2 = Integer.parseInt(inpArr2[1]);
int y2 = Integer.parseInt(inpArr2[2]);
if (y1 % 4 == 0) nodim[2] = 29;
int diff = m1 == m2 && y1 == y2 ? d2 - (d1 - 1) : (nodim[m1] - (d1 - 1));
int mm1 = m1 + 1, mm2 = m2 - 1, yy1 = y1, yy2 = y2;
for (; yy1 <= yy2; yy1++, mm1 = 1)
mm2 = yy1 == yy2 ? (m2 - 1) : 12;
if (yy1 % 4 == 0) nodim[2] = 29;
else nodim[2] = 28;
if (mm2 == 0)
mm2 = 12;
yy2 = yy2 - 1;
for (; mm1 <= mm2 && yy1 <= yy2; mm1++) diff = diff + nodim[mm1];
System.out.print("No. of days from " + inp1 + " to " + inp2 + " is " + diff);
public static String[] split(String s)
String[] retval =
"",
"",
""
;
s = s + ".";
s = s + " ";
for (int i = 0; i <= 2; i++)
retval[i] = s.substring(0, s.indexOf("."));
s = s.substring((s.indexOf(".") + 1), s.length());
return retval;
http://pastebin.com/HRsjTtUf
【讨论】:
【参考方案12】:这是代码:
String date1 = "07/15/2013";
String time1 = "11:00:01";
String date2 = "07/16/2013";
String time2 = "22:15:10";
String format = "MM/dd/yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date fromDate = sdf.parse(date1 + " " + time1);
Date toDate = sdf.parse(date2 + " " + time2);
long diff = toDate.getTime() - fromDate.getTime();
String dateFormat="duration: ";
int diffDays = (int) (diff / (24 * 60 * 60 * 1000));
if(diffDays>0)
dateFormat+=diffDays+" day ";
diff -= diffDays * (24 * 60 * 60 * 1000);
int diffhours = (int) (diff / (60 * 60 * 1000));
if(diffhours>0)
dateFormat+=diffhours+" hour ";
diff -= diffhours * (60 * 60 * 1000);
int diffmin = (int) (diff / (60 * 1000));
if(diffmin>0)
dateFormat+=diffmin+" min ";
diff -= diffmin * (60 * 1000);
int diffsec = (int) (diff / (1000));
if(diffsec>0)
dateFormat+=diffsec+" sec";
System.out.println(dateFormat);
结果是:
duration: 1 day 11 hour 15 min 9 sec
【讨论】:
【参考方案13】:在 Java 8 中,您可以使用 DateTimeFormatter
、Duration
和 LocalDateTime
。这是一个例子:
final String dateStart = "11/03/14 09:29:58";
final String dateStop = "11/03/14 09:33:43";
final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('/')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('/')
.appendValueReduced(ChronoField.YEAR, 2, 2, 2000)
.appendLiteral(' ')
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.toFormatter();
final LocalDateTime start = LocalDateTime.parse(dateStart, formatter);
final LocalDateTime stop = LocalDateTime.parse(dateStop, formatter);
final Duration between = Duration.between(start, stop);
System.out.println(start);
System.out.println(stop);
System.out.println(formatter.format(start));
System.out.println(formatter.format(stop));
System.out.println(between);
System.out.println(between.get(ChronoUnit.SECONDS));
【讨论】:
【参考方案14】:Date d2 = new Date();
Date d1 = new Date(1384831803875l);
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
int diffInDays = (int) diff / (1000 * 60 * 60 * 24);
System.out.println(diffInDays+" days");
System.out.println(diffHours+" Hour");
System.out.println(diffMinutes+" min");
System.out.println(diffSeconds+" sec");
【讨论】:
您好,首先非常感谢您简短而精彩的回答,我在您的解决方案中遇到了一个问题,例如我有两个日期 06_12_2017_07_18_02_PM,另一个是 06_12_2017_07_13_16_PM,我得到 286 秒而不是我应该只有 46 秒【参考方案15】:你可以创建一个类似的方法
public long getDaysBetweenDates(Date d1, Date d2)
return TimeUnit.MILLISECONDS.toDays(d1.getTime() - d2.getTime());
此方法将返回 2 天之间的天数。
【讨论】:
简短回答。解决方案一定是这样的。【参考方案16】:这就是问题在 Java 8 中如何解决,就像 shamimz 的回答一样。
来源:http://docs.oracle.com/javase/tutorial/datetime/iso/period.html
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() + " months, and " + p.getDays() + " days old. (" + p2 + " days total)");
代码产生类似于以下的输出:
You are 53 years, 4 months, and 29 days old. (19508 days total)
我们必须使用 LocalDateTime http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html 来获得小时、分钟、秒的差异。
【讨论】:
这很像 MayurB 回答的 Joda-Time 方式。 joda-time.sourceforge.net 您与Joda-Time 的链接已过期。当前网址是joda.org/joda-time LocalDate 不存储时间和时区。它只保留日-月-年。见docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html 这没有考虑时间。 OP 的问题有秒、分和小时。【参考方案17】:试试下面的
Date dt2 = new DateAndTime().getCurrentDateTime();
long diff = dt2.getTime() - dt1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
int diffInDays = (int) ((dt2.getTime() - dt1.getTime()) / (1000 * 60 * 60 * 24));
if (diffInDays > 1)
System.err.println("Difference in number of days (2) : " + diffInDays);
return false;
else if (diffHours > 24)
System.err.println(">24");
return false;
else if ((diffHours == 24) && (diffMinutes >= 1))
System.err.println("minutes");
return false;
return true;
【讨论】:
这个答案忽略了定义日子开始和结束的时区。这个答案忽略了夏令时和其他意味着一天并不总是 24 小时的异常情况。查看使用 Joda-Time 或 java.time 库的正确答案。 正如 Basil 所指出的,这个答案是不正确的。如果结束日期出现在夏令时,但开始日期没有出现,则会给出错误的天数。【参考方案18】:正如 Michael Borgwardt 在his answer here 中所写:
int diffInDays = (int)( (newerDate.getTime() - olderDate.getTime()) / (1000 * 60 * 60 * 24) )
请注意,这适用于 UTC 日期,因此差异可能是一天 如果您查看当地日期,请关闭。并让它正常工作 由于日光,本地日期需要完全不同的方法 节省时间。
【讨论】:
手动将这些值相乘并不是一个好主意。而是使用 java TimeUnit 类来做到这一点。 你所说的关于当地日期的说法是不正确的。 getTime() 方法,根据 API 文档 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 如果两个数字具有相同的单位,则为可以安全地添加和减去它们。 是的。它是安全的,但代码并不干净,因为 java 提供了标准的处理方式。 除了提供答案链接外,您还必须清楚地引用您从其他人那里复制的措辞。以上是关于如何在java中找到两个日期之间的差异持续时间?的主要内容,如果未能解决你的问题,请参考以下文章