[英]Java Date comparison: Dates in different formats
I have two dates in String format as: say String date1 = 2018-08-29
and in ISO-OFFSET_DATE_TIME
format as String date2 = 2018-08-30T00:00:00+10:00
. 我有两个以String格式表示的日期:比如说String date1 = 2018-08-29
,以ISO-OFFSET_DATE_TIME
格式表示为String date2 = 2018-08-30T00:00:00+10:00
。 What is the best way to compare date1 and date2 for equality? 比较date1和date2是否相等的最佳方法是什么? I am not concerned about time, only day, year and month matters. 我不关心时间,只关心日,年和月。 Should I be converting them to instant and compare? 我应该将它们转换为即时并进行比较吗?
the fastest way is; 最快的方法是
String date1 = "2018-08-29";
String date2 = "2018-08-30T00:00:00+10:00";
boolean isEqual = date2.startsWith(date1);
LocalDate
.parse( "2018-08-29" )
.isEqual(
OffsetDateTime
.parse( "2018-08-30T00:00:00+10:00" )
.toLocalDate()
)
false 假
Parse each input during its appropriate type in java.time . 在java.time中的适当类型期间解析每个输入。
LocalDate ld = LocalDate.parse( "2018-08-29" ) ;
OffsetDateTime odt = OffsetDateTime.parse( "2018-08-30T00:00:00+10:00" ) ;
Compare by extracting a LocalDate
from the OffsetDateTime
. 通过从OffsetDateTime
提取LocalDate
进行比较。
Boolean sameDate = ld.isEqual( odt.toLocalDate() ) ;
false 假
Or perhaps you want to adjust that OffsetDateTime
from its offset of ten hours ahead of UTC to another offset or time zone. 或者,您可能希望将OffsetDateTime
从UTC之前的十个小时的偏移量调整为另一个偏移量或时区。 For example, let's adjust back to UTC before extracting a date to compare. 例如,让我们在提取日期进行比较之前将其调整回UTC。
LocalDate
.parse( "2018-08-29" )
.isEqual(
OffsetDateTime
.parse( "2018-08-30T00:00:00+10:00" )
.withOffsetSameInstant( ZoneOffset.UTC )
.toLocalDate()
)
This changes our results from false
, seen in code above, to true
. 这会将我们的结果从上面的代码中看到的false
更改为true
。
true 真正
If this is the only thing you will ever want to know about the two strings: 如果这是您唯一想了解的两个字符串,则:
LocalDate ld1 = LocalDate.parse(date1);
LocalDate ld2 = LocalDate.parse(date2, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
if (ld1.isEqual(ld2)) {
System.out.println("They are the same date");
} else {
System.out.println("They are different dates");
}
Output using your string examples frmo the question: 使用您的字符串示例输出frmo问题:
They are different dates 他们是不同的日期
I was just writing code similar to that by Basil Bourque when his answer came. 当他的答案出现时,我只是在写类似于Basil Bourque的代码。 I prefer to handle date and time by the correct date-time objects, which is exactly what he's doing. 我更喜欢通过正确的日期时间对象来处理日期和时间,这正是他在做什么。 By parsing date2
into an OffsetDateTime
you are keeping all information, and the conversion to LocalDate
for comparison is easy. 通过将date2
解析为OffsetDateTime
您可以保留所有信息,并且转换为LocalDate
以进行比较也很容易。 For most purposes I recommend that way. 对于大多数目的,我建议采用这种方式。
Only if you know for sure that you will never need the time of day, you may take a shortcut. 仅当您确定自己永远不需要一天中的时间时,才可以使用快捷方式。 You may of course use the fast way by JunJie Wang . 您当然可以使用JunJie Wang的快速方法 。 I prefer my own code above, though, for two reasons: I tells the reader clearly that the strings are dates and times and handles them correctly as such. 不过,我更喜欢上面的代码,这有两个原因:我清楚地告诉读者,字符串是日期和时间,因此可以正确地处理它们。 And it thereby also provides validation of the string formats. 因此,它还提供了字符串格式的验证。 If some day by accident you get a completely different string, you will be notified through a DateTimeParseException
. 如果某天不经意间获得了完全不同的字符串,则会通过DateTimeParseException
通知您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.