本文介绍了比较两个Date实例是指同一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个java.util.Date的Java实例,我必须弄清楚它们是否在同一天。



我可以做到这一点,把日期分开,比较几天,确保这些年份也一样。



由于这是一个常见的问题,我希望能够更容易地解决这个问题。



谢谢!

解决方案

java.util.Date 的实例参考 instants 及时。他们落在哪一天取决于你使用的时区。您可以使用 java.util.Calendar 来表示特定时区的即时...



。 ..或者您可以使用,这是一个更好的API。无论哪种方式,您都必须知道您感兴趣的时区。



在Joda时间,一旦你有一个相关的时区,你可以转换这两个时刻为 LocalDate 对象进行比较。 (这也意味着你可以比较时区A中的即时X是否与时区B中的即时Y在同一天,如果你想...)


I have two Java instances of java.util.Date and I have to find out if they refer to the same day.

I can do this the hard way, taking the dates apart and compare the days, making sure the years match too.

Since this is such a common problem, I expect there to be an easier solution to this problem.

Thanks!

解决方案

Instances of java.util.Date refer to instants in time. Which day they fall on depends on which time zone you're using. You could use a java.util.Calendar to represent an instant in a particular time zone...

... or you could use Joda Time instead, which is a much, much better API. Either way, you'll have to know what time zone you're interested in.

In Joda Time, once you've got a relevant time zone, you can convert both instants to LocalDate objects and compare those. (That also means you can compare whether instant X in time zone A is on the same day as instant Y in time zone B, should you wish to...)

这篇关于比较两个Date实例是指同一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:29
查看更多