Java 帮助比较 Calendar 对象
我用 Java 开发了一个日历小部件(特别是用于 BlackBerry 开发)。用户可以查看当月的所有日期,以及向前/向后移动几个月/几年。
当我绘制日历(以表格格式)时,我想更改当前日期之前 X 天的任何日期的颜色。目前,我只能在屏幕上显示的日历与当前月份相同的情况下进行检查:
if (calendarMonth == currentMonth) {
for (int i = 1; i <= (NUM_DAYS_IN_MONTH); i++) {
if (i > currentDay + Constants.CALENDAR_DAYS_IN_ADVANCE) {
System.out.println("VALID: " + i);
}
}
}
但是当显示的日历与当前月份不同时,我无法编写解决方案。例如,今天是 1 月 26 日,因此 1 月日历会将 1 月的所有日期显示为灰色。当用户将月份更改为二月时,接下来的日子应该是灰色的:
Constants.CALENDAR_DAYS_IN_ADVANCE = 14;
1/26/2011 - 2/9/2011
超过该范围的任何日子都将是黑色。所以基本上,我希望编写一个函数,它将接受两个 java.util.Calendar 对象(显示的活动日历和当前日期的日历),并且该函数将返回 CURRENT DATE 范围内的日期数组 - CALENDAR_DAYS_IN_ADVANCE。
我还需要记住以下几点:
1)我无法将日期与 java.util.Calendar.add() 或 java.util.Calendar.subtract() 函数进行比较,因为 BlackBerry 的 java 受到限制
2)这必须也可以跨年份工作,例如 2010 年 12 月 - 2011 年 1 月
有人可以帮忙解释一下逻辑吗?
谢谢!
I have developed a Calendar widget in Java (for BlackBerry development, specifically). A user can view all of the days in a current month, as well as move forward/backward in months/years.
When I draw my calendar (in a table format), I want to change the color of any days that are X days in advance of the current date. I can currently check for this ONLY if the calendar shown on the screen is the same month as the current month:
if (calendarMonth == currentMonth) {
for (int i = 1; i <= (NUM_DAYS_IN_MONTH); i++) {
if (i > currentDay + Constants.CALENDAR_DAYS_IN_ADVANCE) {
System.out.println("VALID: " + i);
}
}
}
But I am having trouble coding a solution for when the calendar shown is a different month from the current month. For example, today is January 26th, so the January calendar will show all of the January days as a grey color. When the user changes the month to February, then the following days should be grey:
Constants.CALENDAR_DAYS_IN_ADVANCE = 14;
1/26/2011 - 2/9/2011
Any days past that range will be a black color. So basically, I am looking to write a function that will accept two java.util.Calendar objects (the active calendar shown and a calendar for the current date), and the function will return an array of dates in the range of CURRENT DATE - CALENDAR_DAYS_IN_ADVANCE.
I also need to keep in mind the following:
1) I cannot compare dates with the java.util.Calendar.add() or java.util.Calendar.subtract() functions, as java for BlackBerry is limited
2) This has to work across years, too, for example December 2010 - January 2011
Can anybody help with the logic?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有 2 个
Calendar
实例:nowCal
指向开始日期的 00:00:00,maxOffsetCal
指向结束日期的 23:59:59。以下代码将打印所需的日期:
Let's say, you have 2
Calendar
instances:nowCal
is pointing to the 00:00:00 of your start date, andmaxOffsetCal
is pointing to 23:59:59 of your end date.Following code will print desired dates:
if (calendarMonth == currentMonth)
是错误的。切勿使用 == 来比较对象。运算符 == 比较引用,因此仅当您处理相同的对象而不是不同的相等对象时才为真。您应该使用方法
equals()
来代替。if (calendarMonth == currentMonth)
is wrong. Never use == to compare objects. Operator == compare references, so it is true only if you deal with the same object but not different equal objects. You should use method
equals()
instead.