简体   繁体   English

日期不符

[英]Dates Not Matching

I have a TreeMap of <DDate, Integer> . 我有一个<DDate, Integer>的TreeMap。

DDate just contains month and year (month is 0 indexed, like in Java, JAN = 0); DDate仅包含月份和年份(月份在索引中为0,就像在Java中一样,JAN = 0);

My compare is not returning the right thing: 我的比较没有返回正确的东西:

@Override
public int compareTo(DDDate o) {

    Calendar cal1 = Calendar.getInstance();
    cal1.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal1.set(year, month, 1); // year is 2012, month is 1

    Calendar cal2 = Calendar.getInstance();
    cal2.setTimeZone(TimeZone.getTimeZone("UTC"));
    cal2.set(o.getYear(), o.getMonth(), 1); // year is 2012, month is 1


    Log.log("COMPARING: " + format.format(cal1.getTime())); // COMPARING: 20120101
    Log.log("COMPARING: " + format.format(cal2.getTime())); // COMPARING: 20120101

    Log.log((cal1.getTime().getTime())); // 1325413927678
    Log.log((cal2.getTime().getTime())); // 1325413927679

    Log.log("WILL RETURN: " + cal1.getTime().compareTo(cal2.getTime())); // WILL RETURN: -1

    return cal1.getTime().compareTo(cal2.getTime());

}

Why is there descrepancy in the two Calendar objects for the same Date? 为什么两个日历对象中的同一日期都减少了? (1325413927678 vs 1325413927679) (1325413927678和1325413927679)

Thank you! 谢谢!

FYI: this method works for a while, and then at a certain point, stops working. 仅供参考:此方法会工作一段时间,然后在某个时候停止工作。

PS - I understand this is overkill, I can have a simpler compareTo, but please humor me. PS-我知道这太过分了,我可以使用一个简单的compareTo,但请幽默一下。

EDIT- FIX 编辑修复

Use JodaTime's LocalDate. 使用JodaTime的LocalDate。

Or do this: 或这样做:

Calendar cal1 = Calendar.getInstance();
cal1.setTimeZone(TimeZone.getTimeZone("UTC"));
cal1.clear();
cal1.set(year, month, 1); // year is 2012, month is 1

You're setting the year, month and day - but that doesn't change the time of day . 您可以设置年,月和日-但这不会更改一天中时间 The internal clock must have ticked between the two calls to Calendar.getInstance , so they have different "milliseconds since the Unix epoch" values. 内部时钟必须在两次对Calendar.getInstance调用之间打勾,因此它们具有不同的“自Unix纪元以来的毫秒数”值。

Personally I'd recommend using Joda Time instead of java.util.Calendar and java.util.Date - that way you can represent what you're actually interested in, such as LocalDate . 我个人建议使用Joda Time而不是java.util.Calendarjava.util.Date这样,您就可以表示您实际感兴趣的内容,例如LocalDate

Because you are making them at two different times. 因为您是在两个不同的时间制作它们。

Calendar cal1 = Calendar.getInstance(); // time now 
cal1.setTimeZone(TimeZone.getTimeZone("UTC")); // time ticking
cal1.set(year, month, 1); // year is 2012, month is 1 // time ticking

Calendar cal2 = Calendar.getInstance(); // time two ticks later

You have changed the day and month and year, however the hour minutes and seconds stayed and keep on moving and chaning. 您已经更改了日期,月份和年份,但是小时,分钟和秒钟保持不变,并且不断变化和变化。

As mentioned here 正如这里提到的
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int , int, int, int, int) http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,int,int,int,int
The other field values are retained and you should use clear() to set them to zero. 其他字段值将保留,您应使用clear()将其设置为零。 Additionally as Jon recomended I would say use Joda time it is much better. 此外,正如乔恩建议的那样,我会说使用乔达时间会更好。

You should initialize the calendar properly to "no time" (0 ticks), this should fix the issue. 您应该将日历正确初始化为“无时间”(0个滴答),这可以解决此问题。

cal1.setTime(new Date(0));
cal1.set(year, month, 1);

Try using the clear() method before setting the date: 在设置日期之前尝试使用clear()方法:

final Calendar cal1 = Calendar.getInstance();
cal1.clear();
cal1.setTimeZone(TimeZone.getTimeZone("UTC"));
cal1.set(2012, 1, 1); // year is 2012, month is 1

final Calendar cal2 = Calendar.getInstance();
cal2.clear();
cal2.setTimeZone(TimeZone.getTimeZone("UTC"));
cal2.set(2012, 1, 1); // year is 2012, month is 1

cal1.getTime().compareTo(cal2.getTime()); // Returns 0

Jon Skeet's answer is correct. 乔恩·斯基特(Jon Skeet)的答案是正确的。

Here is some example code showing how to use Joda-Time to do a comparison of dates. 这是一些示例代码,显示了如何使用Joda-Time进行日期比较。 Much easier in Joda-Time rather than Java's bundled Date/Calendar classes. 在Joda-Time中,它比Java捆绑的Date / Calendar类要容易得多。 Takes only 3 lines of code to both instantiate and compare. 只需3行代码即可实例化和比较。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// New DateTime instance with default time zone. For specific zone, call "withZone()" method.
org.joda.time.DateTime dateTimeX = new org.joda.time.DateTime();
org.joda.time.DateTime dateTimeY = new org.joda.time.DateTime(2012, 1, 1, 0, 0); // First of January.

// To compare the date only while ignoring the time, set identical time component on both.
// Call withTimeAtStartOfDay() to set same time value.
Boolean isSameDate = dateTimeX.withTimeAtStartOfDay().isEqual(dateTimeY.withTimeAtStartOfDay());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM