如何在Java 8中比较两个日期之间的分钟差异,同时考虑时区差异?

huangapple go评论63阅读模式
英文:

How to compare the difference in minutes between 2 dates in Java 8 while also taking into account the time zone difference?

问题

我必须在Java 8中比较两个日期,以获取分钟差异。现在我正在使用java.utils.Date来存储我的日期,但我不知道如何获取分钟差异。另外,要比较的日期可能处于2个不同的时区,如果存在的话,应该考虑进去。我该如何进行?

要比较的示例日期:

2020年10月12日星期一 12:20:00 IST
2020年10月05日星期一 09:56:57 GMT
英文:

I have to compare 2 dates in Java 8 to get the difference of minutes. Now I am using java.utils.Date to store my dates but I am unable to find out how to get the difference in minutes. Also, the dates to be compared might be in 2 different time zones which should be taken into account if present. How do I proceed with this?

Example dates to be compared :

Mon Oct 12 12:20:00 IST 2020
Mon Oct 05 09:56:57 GMT 2020

答案1

得分: 2

以下是翻译好的部分:

时区差异本身不会造成任何问题。Java 处理得很好。但是,IST 存在歧义问题,可能代表爱尔兰夏令时(Irish Summer Time)、以色列标准时间(Israel Standard Time)、印度标准时间(India Standard Time)或其他内容。

java.time

我建议您在处理日期和时间时使用现代的 Java 日期和时间 API - java.time。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu", Locale.ENGLISH);

String aDateTimeString = "Mon Oct 12 12:20:00 IST 2020";
String anotherDateTimeString = "Mon Oct 05 09:56:57 GMT 2020";

ZonedDateTime aDateTime = ZonedDateTime.parse(aDateTimeString, formatter);
ZonedDateTime anotherDateTime = ZonedDateTime.parse(anotherDateTimeString, formatter);
long differenceInMinutes = ChronoUnit.MINUTES.between(anotherDateTime, aDateTime);

System.out.format("The times are %s and %s%n", aDateTime, anotherDateTime);
System.out.format("Difference is %d minutes%n", differenceInMinutes);

输出为:

> 时间分别为 2020-10-12T12:20Z[Atlantic/Reykjavik] 和 2020-10-05T09:56:57Z[GMT]
> 差异为 10223 分钟

Java 将 IST 解释为冰岛时间。这可能不是您的意图。但跨时区的差异计算是有效的。

我在下面提供了一个链接,说明如何控制 Java 解释 IST 的方式。

链接

英文:

The time zone difference itself does not pose any problem. Java handles that nicely. You have a problem in IST being ambiguous, though, it may stand for Irish Summer Time, Israel Standard Time, India Standard Time or something else.

java.time

I recommend you use java.time, the modern Java date and time API, for your date and time work.

	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz uuuu", Locale.ENGLISH);
	
	String aDateTimeString = "Mon Oct 12 12:20:00 IST 2020";
	String anotherDateTimeString = "Mon Oct 05 09:56:57 GMT 2020";
	
	ZonedDateTime aDateTime = ZonedDateTime.parse(aDateTimeString, formatter);
	ZonedDateTime anotherDateTime = ZonedDateTime.parse(anotherDateTimeString, formatter);
	long differenceInMinutes = ChronoUnit.MINUTES.between(anotherDateTime, aDateTime);
	
	System.out.format("The times are %s and %s%n", aDateTime, anotherDateTime);
	System.out.format("Difference is %d minutes%n", differenceInMinutes);

Output is:

> The times are 2020-10-12T12:20Z[Atlantic/Reykjavik] and 2020-10-05T09:56:57Z[GMT]
> Difference is 10223 minutes

Java has interpreted IST as Icelandic time. You might not have intended that. But the calculation of difference across time zones works.

I provide a link below to how to control how Java interprets IST.

huangapple
  • 本文由 发表于 2020年10月5日 12:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64202482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定