java 日历逻辑帮助
我正在使用 Java Calendar 类执行以下操作:
- 设置开始日期
- 设置结束日期
- 该范围内的任何日期都是“有效”日期
我对此有些工作,有些则不然。请参阅下面的代码:
nowCalendar.set(Calendar.DATE, nowCalendar.get(Calendar.DATE) + offset);
int nowDay = nowCalendar.get(Calendar.DATE);
Calendar futureCalendar = Calendar.getInstance();
futureCalendar.set(Calendar.DATE, nowDay + days);
Date now = nowCalendar.getTime();
Date endTime = futureCalendar.getTime();
long now_ms = now.getTime();
long endTime_ms = endTime.getTime();
for (; now_ms < endTime_ms; now_ms += MILLIS_IN_DAY) {
valid_days.addElement(new Date(now_ms));
System.out.println("VALID DAY: " + new Date(now_ms));
}
基本上,我设置一个“现在”日历和一个“未来”日历,然后比较两个日历以找到有效日期。在我的日历上,有效日期将显示为白色,无效日期将显示为灰色。您会注意到两个变量:
offset = three days after the current selected date
days = the number of valid days from the current selected date
这有效...除非当前选择的日期是该月的最后一天,或两天前(三天在一起)。我认为偏移肯定是把事情搞砸了,但这个逻辑在其他地方都适用。有什么想法吗?
I am working with the Java Calendar class to do the following:
- Set a start date
- Set an end date
- Any date within that range is a "valid" date
I have this somewhat working, and somewhat not. Please see the code below:
nowCalendar.set(Calendar.DATE, nowCalendar.get(Calendar.DATE) + offset);
int nowDay = nowCalendar.get(Calendar.DATE);
Calendar futureCalendar = Calendar.getInstance();
futureCalendar.set(Calendar.DATE, nowDay + days);
Date now = nowCalendar.getTime();
Date endTime = futureCalendar.getTime();
long now_ms = now.getTime();
long endTime_ms = endTime.getTime();
for (; now_ms < endTime_ms; now_ms += MILLIS_IN_DAY) {
valid_days.addElement(new Date(now_ms));
System.out.println("VALID DAY: " + new Date(now_ms));
}
Basically, I set a "NOW" calendar and a "FUTURE" calendar, and then I compare the two calendars to find the valid days. On my calendar, valid days will be shaded white and invalid days will be shaded gray. You will notice two variables:
offset = three days after the current selected date
days = the number of valid days from the current selected date
This works...EXCEPT when the current selected date is the last day of the month, or two days prior (three all together). I think that its the offset that is definitely screwing it up, but the logic works everywhere else. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要摆弄毫秒。克隆
nowCalendar
,在循环中使用Calendar#add()
添加 1 天,只要不超过futureCalendar
并获取使用Calendar#getTime()
获取日期
。(请注意,我将
validDays
改进为List
,而不是旧版Vector
)Don't fiddle with milliseconds. Clone the
nowCalendar
, add 1 day to it usingCalendar#add()
in a loop as long as it does not exceedfutureCalendar
and get theDate
out of it usingCalendar#getTime()
.(note that I improved
validDays
to be aList
instead of the legacyVector
)在第一行中使用
add
而不是set
,否则如果您处于月份边界,则不会调整月份:Use
add
instead ofset
in the first line, otherwise the month is not adjusted if you are at the month boundary:这里startCal是开始时间的日历实例,endCal是结束时间。
Here the startCal is the calendar instance of start time and endCal is end time.
我发现了问题:
一旦我将 futureCalendar 设置为 nowCalendar 的克隆(加上额外的天数),它就开始工作。谢谢大家的建议!
I found the problem:
As soon as I set futureCalendar to be a clone of nowCalendar (plus the additional days), then it started working. Thanks for everyone's suggestions!