Calendar< =另一个日历

编程入门 行业动态 更新时间:2024-10-09 06:29:26
本文介绍了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; =另一个日历

本文发布于:2023-11-28 04:09:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日历   Calendar   amp   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!