java 日历逻辑帮助

发布于 2024-10-22 18:24:53 字数 1057 浏览 1 评论 0原文

我正在使用 Java Calendar 类执行以下操作:

  1. 设置开始日期
  2. 设置结束日期
  3. 该范围内的任何日期都是“有效”日期

我对此有些工作,有些则不然。请参阅下面的代码:

    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:

  1. Set a start date
  2. Set an end date
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长安忆 2024-10-29 18:24:53

不要摆弄毫秒。克隆 nowCalendar,在循环中使用 Calendar#add() 添加 1 天,只要不超过 futureCalendar 并获取使用Calendar#getTime()获取日期

Calendar clone = nowCalendar.clone();

while (!clone.after(futureCalendar)) {
    validDays.add(clone.getTime());
    clone.add(Calendar.DATE, 1);
}

(请注意,我将 validDays 改进为 List,而不是旧版 Vector

Don't fiddle with milliseconds. Clone the nowCalendar, add 1 day to it using Calendar#add() in a loop as long as it does not exceed futureCalendar and get the Date out of it using Calendar#getTime().

Calendar clone = nowCalendar.clone();

while (!clone.after(futureCalendar)) {
    validDays.add(clone.getTime());
    clone.add(Calendar.DATE, 1);
}

(note that I improved validDays to be a List instead of the legacy Vector)

德意的啸 2024-10-29 18:24:53

在第一行中使用 add 而不是 set,否则如果您处于月份边界,则不会调整月份:

nowCalendar.add(Calendar.DATE, offset);

Use add instead of set in the first line, otherwise the month is not adjusted if you are at the month boundary:

nowCalendar.add(Calendar.DATE, offset);
云之铃。 2024-10-29 18:24:53
public boolean isInRange(Date d)
{
   Calendar cal = Calendar.getInstance();
   cal.setTime(d);
   return cal.after(startCal) && cal.before(endCal);
}

这里startCal是开始时间的日历实例,endCal是结束时间。

public boolean isInRange(Date d)
{
   Calendar cal = Calendar.getInstance();
   cal.setTime(d);
   return cal.after(startCal) && cal.before(endCal);
}

Here the startCal is the calendar instance of start time and endCal is end time.

梦里人 2024-10-29 18:24:53

我发现了问题:

一旦我将 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文