我想获取我的Java程序的ans,我想在其中找出当前日期是一年中的哪一天,当前星期是一年中的哪一周,等等。当我尝试使用日历类方法进行操作时,它是给出错误的答案。
下面是我的代码
{
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
有人可以帮忙找出解决方案吗?
答案 0 :(得分:0)
您的Calendar
变量为c1
。但这根本没有引用c1
:
Calendar.DAY_OF_YEAR
那是什么? According to the documentation这些是常量,用于标识查询该对象时要返回的字段。您不想输出常量本身,而是想使用它来获取c1
对象的值。像这样:
System.out.println("Today is " + c1.get(Calendar.DAY_OF_YEAR) + " day of year");
重复其他字段。