[英]How to calculate the difference of dates?
Im using java.util.Date
class and want to compare a Date
object and the current time.我使用
java.util.Date
类并想要比较Date
对象和当前时间。
output format must be like this :输出格式必须是这样的:
... years , ... months , ... days , ...hours , ... minutes , ... seconds.
this is my code :这是我的代码:
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar(2022, 1 , 25 , 12 , 20 , 33);
Date now = new Date(); //Tue Feb 25 11:49:05 IRST 2020
Date date = calendar.getTime(); //Fri Feb 25 12:20:33 IRST 2022
}
is there a simple way to calculate that?有没有简单的方法来计算?
java.time
and why & how to use it java.time
以及为什么和如何使用它For the difference in years, months and days, there is a java.time.Period
you can easily use to get what you want:对于年、月和日的差异,您可以轻松使用
java.time.Period
来获取所需内容:
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2022, 1 , 25 , 12 , 20 , 33);
LocalDateTime now = LocalDateTime.now();
// get the difference in years, months and days
Period p = Period.between(now.toLocalDate(), localDateTime.toLocalDate());
// and print the result(s)
System.out.println("Difference between " + localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
+ " and " + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + " is:\n"
+ p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days");
}
The output of this code example would be (depending on the current day, of course):此代码示例的输出将是(当然取决于当天):
Difference between 2022-01-25T12:20:33 and 2020-02-25T10:52:43.327 is:
1 years, 11 months, 0 days
You may use a java.time.Duration
as shown in one of the other answers in order to get the additional difference in hours, minutes and seconds.您可以使用其他答案之一中所示的
java.time.Duration
以获得小时、分钟和秒的额外差异。 See this example (which is basically the one from above plus time calculation):看这个例子(基本上就是上面加上时间计算的那个):
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2022, 1 , 25 , 12 , 20 , 33);
LocalDateTime now = LocalDateTime.now();
// get the difference in years, months and days (date related difference)
Period p = Period.between(now.toLocalDate(), localDateTime.toLocalDate());
// and the difference in hours, minutes and seconds (time-of-day related difference)
Duration d = Duration.between(now.toLocalTime(), localDateTime.toLocalTime());
long totalSeconds = d.getSeconds();
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
// and print the result(s)
System.out.println("Difference between " + localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
+ " and " + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + " is:\n"
+ p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days, "
+ hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
}
Output:输出:
Difference between 2022-01-25T12:20:33 and 2020-02-25T11:25:42.712 is:
1 years, 11 months, 0 days, 0 hours, 54 minutes, 50 seconds
If you are receiving java.util.Date
s or extending legacy code (or if you are just too lazy to change all your code to using java.time
), you can make use of the following compatibility methods:如果您正在接收
java.util.Date
或扩展遗留代码(或者如果您懒得将所有代码更改为使用java.time
),则可以使用以下兼容性方法:
LocalDateTime fromDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
Date fromLocalDateTime = Date.from(LocalDateTime.now().toInstant((ZoneOffset.UTC)));
Java 8 Date/Time API is flexible in comparing dates. Java 8 Date/Time API在比较日期方面很灵活。
Example:例子:
LocalDate currentDate = LocalDate.now();
LocalDate previousDate = LocalDate.of(2000, 7, 1);
Period period = Period.between(previousDate, currentDate);
String output = String.format("%s years , %s months , %s days",
period.getYears(), period.getMonths(), period.getDays());
System.out.println(output);
> 19 years , 7 months , 24 days
This might help you, if you want to decide between Period
or Duration
.如果您想在
Period
或Duration
之间做出决定, 这可能会对您有所帮助。
Use java Instant使用 Java Instant
Instant start, end;
Duration duration = Duration.between(start, stop);
long hours = dur.toHours();
long minutes = dur.toMinutes();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.