Java 帮助比较 Calendar 对象

发布于 2024-10-15 00:56:00 字数 987 浏览 2 评论 0原文

我用 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 技术交流群。

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

发布评论

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

评论(2

以可爱出名 2024-10-22 00:56:00

假设您有 2 个 Calendar 实例:nowCal 指向开始日期的 00:00:00,maxOffsetCal 指向结束日期的 23:59:59。
以下代码将打印所需的日期:

public class Test {
    private final static long MILLIS_IN_DAY = 86400 * 1000;

    public static void main(String[] args) {
        Calendar nowCal = new GregorianCalendar();
        setTime(nowCal, 29, 1, 2011, 0, 0, 0);
        Calendar maxOffsetCal = new GregorianCalendar();
        setTime(maxOffsetCal, 2, 2, 2011, 23, 59, 59);
        long now = nowCal.getTimeInMillis(), endTime = maxOffsetCal.getTimeInMillis();
        for (; now < endTime; now += MILLIS_IN_DAY ) {
            System.out.println(new Date(now));
        }
    }

    private static void setTime(Calendar c, int dayOfMonth, int monthOfYear, int year,
                                    int hourOfDay, int minute, int second) {
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        c.set(Calendar.MONTH, monthOfYear - 1);
        c.set(Calendar.YEAR, year);
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
        c.set(Calendar.MILLISECOND, 0);
    }
}

Let's say, you have 2 Calendar instances: nowCal is pointing to the 00:00:00 of your start date, and maxOffsetCal is pointing to 23:59:59 of your end date.
Following code will print desired dates:

public class Test {
    private final static long MILLIS_IN_DAY = 86400 * 1000;

    public static void main(String[] args) {
        Calendar nowCal = new GregorianCalendar();
        setTime(nowCal, 29, 1, 2011, 0, 0, 0);
        Calendar maxOffsetCal = new GregorianCalendar();
        setTime(maxOffsetCal, 2, 2, 2011, 23, 59, 59);
        long now = nowCal.getTimeInMillis(), endTime = maxOffsetCal.getTimeInMillis();
        for (; now < endTime; now += MILLIS_IN_DAY ) {
            System.out.println(new Date(now));
        }
    }

    private static void setTime(Calendar c, int dayOfMonth, int monthOfYear, int year,
                                    int hourOfDay, int minute, int second) {
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        c.set(Calendar.MONTH, monthOfYear - 1);
        c.set(Calendar.YEAR, year);
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
        c.set(Calendar.MILLISECOND, 0);
    }
}
年少掌心 2024-10-22 00:56:00

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.

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