计算两个JDateChooser值之间的差异

Xyr*_*uen 1 java netbeans jdatechooser

这是我的代码,但是不起作用。

    Start.getDate();
    End.getDate();
    int diffInDays = (int)( (Start.getDate() - End.getDate()) / (1000 * 60 * 60 * 24));
    System.out.println(diffInDays);
Run Code Online (Sandbox Code Playgroud)

因此,我该如何制作一个程序,使用户可以查看2 JDateChooser之间的区别。

Mad*_*mer 5

日期/时间计算是一个复杂的主题,通常最好使用专用库来解决。

Java 8

Instant start = Start.getDate().toInstance();
Instant end = End.getDate().getInstance();

Duration duration = Duration.between(start, end);
Run Code Online (Sandbox Code Playgroud)

乔达时代

如果你不能使用Java 8,那么你应该使用乔达时间,而不是

DateTime start = new DateTime(Start.geDate().getTime());
DateTime end = new DateTime(End.geDate().getTime());

// You might need to use end, start instead, depending on which
// is later
Duration duration = new Duration(start, end);
Period period = duration.toPeriod();
Run Code Online (Sandbox Code Playgroud)

您可以看一下这个答案,看看如何格式化值