获取 java 中两个日期之间的精确差异

Get precise difference between two dates in java

我正在寻找一种方法来获取两个日期之间的差异,正如标题所说,在 Java 中。例如,如果 d1 = 2017-01-23 05:25:00d2 = 2018-03-20 07:29:50,某些方法将打印

Years: 1
Months: 1
Days: 25
Hours: 2
Minutes: 4
Seconds: 50

我曾尝试使用 LocalDateTime objects 来执行此操作,但我找不到获取它们之间月份的方法。我认为不需要包含该代码,但如果需要的话我可以。我不在乎日期是什么类型,但我不想使用 Joda 时间或其他外部库。预先感谢您的帮助。

包 com.mkyong.date;

进口java.text.SimpleDateFormat; 导入 java.util.Date;

public class DateDifferentExample {

public static void main(String[] args) {

    String dateStart = "01/14/2012 09:29:58";
    String dateStop = "01/15/2012 10:31:48";

    //HH converts hour in 24 hours format (0-23), day calculation
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    java.util.Date d1 = null;
    java.util.Date d2 = null;

    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateStop);

        //in milliseconds
        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(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

结果

1 天 1 小时 1 分钟 50 秒。

我只想将您想要的两个日期和时间放入 SimpleDateFormat 中。然后你可以只做 date.getTime() 和 return 以毫秒为单位的距离,然后可以将其转换回天数、年数或月数。这是我写的一个例子:

</p>

<pre><code>import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;

public class DateTest{
    public static void main(String[] args) throws ParseException{
        System.out.println("Current time in milliseconds: " + System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss MM/dd/yyyy");

        String dateAndTime1 = "2:30:00 1/23/2017";
        String dateAndTime2 = "1:45:00 5/23/2015";

        Date date1 = format.parse(dateAndTime1);
        Date date2 = format.parse(dateAndTime2);
        long difference = date1.getTime() - date2.getTime();


        System.out.println("The difference in milliseconds is " + difference);
    }
}

ISO 8601

我们必须解析您的字符串。当 parsing/generating 字符串时,java.time classes 默认使用标准 ISO 8601 格式。您的输入接近合规;我们只需要将中间的 SPACE 替换为 T.

String inputStart = "2017-01-23 05:25:00".replace ( " " , "T" );
String inputStop = "2018-03-20 07:29:50".replace ( " " , "T" );

LocalDateTime

您的输入没有任何 offset-from-UTC 或时区的指示。所以他们 代表时间轴上的实际时刻,只是时间轴上 可能 时刻的模糊概念。所以我们将把它解析为 LocalDateTime objects. The math we will perform to determine spans of time will be based on generic 24-hour days, and will ignore anomalies such as Daylight Saving Time (DST).

LocalDateTime start = LocalDateTime.parse ( inputStart );
LocalDateTime stop = LocalDateTime.parse ( inputStop );

Period & Duration

java.time classes 将独立于时间轴的时间跨度定义为两个部分。 Period class represents years-months-days, while the Duration class 表示 hours-minutes-seconds。

将时间跨度分成这两部分可以避免某些问题,我们必须解决这些问题才能解决您的请求。您究竟想如何处理第一天可能不完整的一天,以及最后可能不完整的一天?一天中午到次日中午是一天还是24小时?如果我们将第一天的小时数与最后一天的小时数相加,如果这个数字超过 24 小时,我们是否应该挤出一整天?

正在计算

这里我采用的方法是计算整日的天数,忽略第一天和最后一天。然后我将第一天的小时数与最后一天的小时数相加。正如我们在您的示例数据中看到的那样,这可能会导致我们的 Duration 超过 24 小时。这种方法可能会或可能不会满足您未说明的 intentions/definitions.

Period p = Period.between ( start.toLocalDate () , stop.toLocalDate () ).minusDays ( 1 );  // Subtract one, as we account for hours of first day.

// Get the Duration of first day's hours-minutes-seconds.
LocalDateTime startNextDay = start.toLocalDate ().plusDays ( 1 ).atStartOfDay ();
Duration dStart = Duration.between ( start , startNextDay );

// Get the Duration of first day's hours-minutes-seconds.
LocalDateTime stopStartOfDay = stop.toLocalDate ().atStartOfDay ();
Duration dStop = Duration.between ( stopStartOfDay , stop );

// Combine the pair of partial days into a single Duration.
Duration d = dStart.plus ( dStop );

转储到控制台。 Period & Duration class 上的 toString 方法在 ISO 8601 的 standard durations format 中生成字符串: PnYnMnDTnHnMnS 其中 P 标记开头和 T 将 years-month-days 与 hours-minutes-seconds 分开。

System.out.println ( "start/stop: " + start + "/" + stop );
System.out.println ( "p: " + p );
System.out.println ( "dStart: " + dStart + " and dStop: " + dStop );
System.out.println ( "d: " + d );

start/stop: 2017-01-23T05:25/2018-03-20T07:29:50

p: P1Y1M24D

dStart: PT18H35M and dStop: PT7H29M50S

d: PT26H4M50S

要获得所需的文本输出,您需要请求 Period 的每个部分和 Duration 的每个部分。

int years = p.getYears ();
int months = p.getMonths ();
int days = p.getDays ();

Java 8 莫名其妙地缺少从 Duration 中方便地获取每个小时或分钟或秒部分的方法。 Java9 增加了这样的方法。有关更多讨论,请参阅 this Question

long durationDays = d.toDaysPart();
long hours = d.toHoursPart();
long minutes = d.toMinutesPart();
long seconds = d.toSecondsPart();
int nanos = d.toNanosPart();

活码

查看此示例 code working live in IdeOne.com


ZonedDateTime

上面的讨论是针对通用的 24 小时制,忽略了 offset-from-UTC/time 区域的问题。

如果您打算跟踪时间轴上的实际时刻,则必须应用适用于那些 date-time 字符串的时区。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtStart = start.atZone( z );
…

其余代码类似,但将 ZoneId 传递给 atStartOfDay 调用以生成 ZonedDateTime 对象而不是 LocalDateTime 对象。

请记住,使用 ZonedDateTime 个对象而不是 LocalDateTime 个对象可能会得到不同的结果。

参见示例 code live in IdeOne.com

String inputStart = "2017-01-23 05:25:00".replace ( " " , "T" );
String inputStop = "2018-03-20 07:29:50".replace ( " " , "T" );

LocalDateTime ldtStart = LocalDateTime.parse ( inputStart ); // LocalDateTime used only briefly here, to parse inputs. Then we switch to `ZonedDateTime`.
LocalDateTime ldtStop = LocalDateTime.parse ( inputStop );

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtStart = ldtStart.atZone( z ); // Invalid values may be adjusted, such as during the hour of a "Spring-forward" Daylight Saving Time (DST) switch-over.
ZonedDateTime zdtStop = ldtStop.atZone( z );

Period p = Period.between ( zdtStart.toLocalDate () , zdtStop.toLocalDate () ).minusDays ( 1 );  // Subtract one, as we account for hours of first day.

// Get the Duration of first day's hours-minutes-seconds.
ZonedDateTime zdtStartNextDay = zdtStart.toLocalDate ().plusDays ( 1 ).atStartOfDay ( z );
Duration dStart = Duration.between ( zdtStart , zdtStartNextDay );

// Get the Duration of first day's hours-minutes-seconds.
ZonedDateTime zdtStopStartOfDay = zdtStop.toLocalDate ().atStartOfDay ( z );
Duration dStop = Duration.between ( zdtStopStartOfDay , zdtStop );

// Combine the pair of partial days into a single Duration.
Duration d = dStart.plus ( dStop );

System.out.println ( "zdtStart: " + zdtStart );
System.out.println ( "zdtStop: " + zdtStop );
System.out.println ( "p: " + p );
System.out.println ( "dStart: " + dStart + " and dStop: " + dStop );
System.out.println ( "d: " + d );

int years = p.getYears ();
int months = p.getMonths ();
int days = p.getDays ();
System.out.println( "Years: " + years + "\nMonths: " + months + "\nDays: " + days );

// Enable if in Java 9 or later (but not in Java 8)
// long durationDays = d.toDaysPart();
// long hours = d.toHoursPart();
// long minutes = d.toMinutesPart();
// long seconds = d.toSecondsPart();
// int nanos = d.toNanosPart();

提示:ThreeTen-Extra library offers a class Interval to track a pair of Instant objects. An Instant is moment on the timeline in UTC. You may extract an Instant from a ZonedDateTime by calling toInstant.


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

  • Java SE 8 and SE 9 及更高版本
    • Built-in。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and SE 7
    • java.time 的大部分功能是 back-ported 到 Java ThreeTen-Backport 中的 6 和 7。
  • Android
    • ThreeTenABP项目专门为Android改编ThreeTen-Backport(上面提到的)。
    • 参见

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.