从 java 中的预定义日历函数中获取错误答案

Getting wrong answers from predefined Calendar function in java

我想得到我的 java 程序的答案,我想在其中找出当前日期是一年中的哪一天,当前周是一年中的哪一周等。当我尝试这样做时calendar class 方法,它给出了错误的答案。

下面是我的代码

{
            Calendar c1 = Calendar.getInstance();
            c1.setTime(new Date());
            System.out.println("Today is "+Calendar.DAY_OF_YEAR+" day of year");
            System.out.println("Today is "+Calendar.WEEK_OF_YEAR+" week of year");
            System.out.println("Today is "+Calendar.DAY_OF_WEEK_IN_MONTH+" day of week in month");



    }

输出:-

  Today is 6 day of year
    Today is 3 week of year
    Today is 8 day of week in month

谁能帮忙找出解决办法?

您的 Calendar 变量是 c1。但这根本没有引用 c1

Calendar.DAY_OF_YEAR

那是什么? According to the documentation 这些是在查询该对象时将字段标识为 return 的常量。您不想输出常量本身,而是想使用它从 c1 对象中获取值。像这样:

System.out.println("Today is " + c1.get(Calendar.DAY_OF_YEAR) + " day of year");

对您的其他字段也重复此操作。