Java 1.5 Calendar#compareTo() 混淆

发布于 2024-07-16 11:29:49 字数 1326 浏览 4 评论 0原文

好吧,我正式被这个问题难住了。 我有一个 GregorianCalendar 对象,我想确定它是过去、现在还是将来。 到目前为止, Calendar#compareTo 文档让我感到困惑,因为我得到的结果不稳定。 一个简单的测试类将说明我的问题:

import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{

        public static void main( String[] args )
        {
                GregorianCalendar datePast = new GregorianCalendar();
                datePast.add(Calendar.MONTH, -6); // subtract 6 months
                GregorianCalendar datePresent = new GregorianCalendar();
                GregorianCalendar dateFuture = new GregorianCalendar();
                datePast.add(Calendar.MONTH, 6); // add 6 months

                System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
                System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
                System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
        }
}

输出:

compare datePresent to datePast: 1
compare datePresent to datePresent: 0
compare datePresent to dateFuture: 0

我对compareTo的理解是最后一行应该是-1。 谁能告诉我我做错了什么?

Ok, I'm officially stumped on this one. I have a GregorianCalendar object that I would like to determine if it is in the past, present, or future. So far, the Calendar#compareTo docs are confusing to me, in that I am getting erratic results. A simple test class will illustrate my problem:

import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{

        public static void main( String[] args )
        {
                GregorianCalendar datePast = new GregorianCalendar();
                datePast.add(Calendar.MONTH, -6); // subtract 6 months
                GregorianCalendar datePresent = new GregorianCalendar();
                GregorianCalendar dateFuture = new GregorianCalendar();
                datePast.add(Calendar.MONTH, 6); // add 6 months

                System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
                System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
                System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
        }
}

And the output:

compare datePresent to datePast: 1
compare datePresent to datePresent: 0
compare datePresent to dateFuture: 0

My understanding of compareTo is that the last line should be a -1. Can anyone tell me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2024-07-23 11:29:49

您已向 datePast 添加 6 个月,而不是 dateFuture。 这是工作代码:

import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{

    public static void main( String[] args )
    {
        GregorianCalendar datePast = new GregorianCalendar();
        datePast.add(Calendar.MONTH, -6); // subtract 6 months
        GregorianCalendar datePresent = new GregorianCalendar();
        GregorianCalendar dateFuture = new GregorianCalendar();
        dateFuture.add(Calendar.MONTH, 6); // add 6 months

        System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
        System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
        System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
    }
}

您的结果会不一致,因为有时 dateFuture 会晚于 datePresent,具体取决于内部时钟“滴答”的时间。

除此之外,我忍不住给出我的标准 Java 日期和时间建议:使用 Joda Time 相反。 尽管许多类型是不可变的这一事实会有所帮助,但它不对这个特定问题负责。 但总的来说,它是一个更好的库。

You've added 6 months to datePast, not dateFuture. Here's the working code:

import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{

    public static void main( String[] args )
    {
        GregorianCalendar datePast = new GregorianCalendar();
        datePast.add(Calendar.MONTH, -6); // subtract 6 months
        GregorianCalendar datePresent = new GregorianCalendar();
        GregorianCalendar dateFuture = new GregorianCalendar();
        dateFuture.add(Calendar.MONTH, 6); // add 6 months

        System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
        System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
        System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
    }
}

Your results would have been inconsistent because sometimes dateFuture would be later than datePresent, depending on when the internal clock "ticked".

In addition to all of this, I can't help but give my standard Java date and time advice: use Joda Time instead. It's not responsible for this particular problem, although the fact that many of the types are immutable would have helped. But in general it's a much better library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文