Java 1.5 Calendar#compareTo() 混淆
好吧,我正式被这个问题难住了。 我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已向
datePast
添加 6 个月,而不是dateFuture
。 这是工作代码:您的结果会不一致,因为有时
dateFuture
会晚于datePresent
,具体取决于内部时钟“滴答”的时间。除此之外,我忍不住给出我的标准 Java 日期和时间建议:使用 Joda Time 相反。 尽管许多类型是不可变的这一事实会有所帮助,但它不对这个特定问题负责。 但总的来说,它是一个更好的库。
You've added 6 months to
datePast
, notdateFuture
. Here's the working code:Your results would have been inconsistent because sometimes
dateFuture
would be later thandatePresent
, 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.