Calendar< =另一个日历 [英] Calendar <= another calendar

查看:155
本文介绍了Calendar< =另一个日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while (r.next()) {
    String rn = r.getString(3);
    String sqldate = r.getString(2); // database reservation date
}

从数据库获取日期

DateFormat RD = new SimpleDateFormat("yyyy-MM-dd");
     java.sql.Date converteddate = new java.sql.Date(RD.parse(sqldate).getTime());
     Calendar cal = Calendar.getInstance();
     Calendar cal1 = Calendar.getInstance();
     cal.setTime(converteddate);
     cal.add(Calendar.DATE, 7);
     if(cal<=cal1){ }// i need to this opration i dont know how

我需要检查 cal 是< = Cal1 。任何想法?

I need to check cal is <= Cal1. Any ideas?

推荐答案

如何使用简单的方法:

if (cal.before(cal1)) { ... }

这说明:如果一个日期在另一个日期之前,你应该做以下的..。

This reads: "If a date is before another date, you should do the following..".

我认为如果你写之前比compareTo,即使操作是相同的。 另外阅读Java日历的文档

I think it makes it easier to read if you write "before" than "compareTo", even though the operation is the same. Also read the documentation for Java Calendar.

编辑:
我已经更新了答案,以包括下面的评论,所以答案是更完整。感谢指向它的assylias,但如果你想比较一个日期是否在另一个日期之前或等于另一个日期,你应该使用:

I've updated the answer a bit to include what's been commented below so the answer is more complete. Thanks to assylias for pointing it out, but if you want to compare if a date is before or equal to another date, you should use:

if (!cal.after(cal1)) { ... }

如果日期不是在另一个日期之后,你应该做以下..。另请注意,不在之后与之前或之前相同。

This reads: "If a date is not after another date, you should do the following..". Also note that "not after" is the same as "before or equal to".

这篇关于Calendar&lt; =另一个日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆