英文:
What's the difference between Calendar.getInstance().get(Calendar.DAY_OF_WEEK) and Calander.DAY_OF_WEEK in java
问题
我刚接触 Kotlin,在学习课程时遇到了一个关于获取当前工作日的部分。
课程使用了这段 Java 代码来获取:
import java.util.*
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
但我不明白为什么 Calendar.DAY_OF_WEEK
也不起作用,或者这两者之间的区别是什么。
感谢解释。
英文:
I'm new to Kotlin and while doing a course there was a bit where you worked with the current weekday.
The course used this java code to get it:
import java.util.*
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
but I don't understand why
Calendar.DAY_OF_WEEK
wouldn't work either, or whats the difference between the two.
Thanks for the explanation
答案1
得分: 4
Calendar.DAY_OF_WEEK
是一个常数,用于访问Calendar对象内的字段。
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
使用这个常数来读取日历中“星期几”字段的值。
这是一个略微不同寻常的设计。Calendar类的设计者没有为所有的日历字段添加像是“getDayOfWeek”、“setDayOfWeek”和“addDayOfWeek”这样的几十个方法,而是添加了“get”、“set”和“add”方法,这些方法以数值字段标识符作为参数。
需要注意的是,Calendar现在被视为一个“遗留”类 - 对于新代码,最好使用java.time
包中的类。获取今天星期几的现代方法是:
DayOfWeek dow = LocalDate.now().getDayOfWeek();
英文:
Calendar.DAY_OF_WEEK
is a constant number, used to access fields within the Calendar object.
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
uses this constant number to read the value of the "day of week" field from the calendar.
This is somewhat unusual design. Instead of adding dozens of methods like "getDayOfWeek", "setDayOfWeek", and "addDayOfWeek" for all the calendar fields, the designers of the Calendar class added "get" "set" and "add" methods that take a numeric field identifier as the parameter.
Note that Calendar is nowadays considered a "legacy" class - for new code it's better to use the classes in the java.time
package. The modern way to get today's day of the week is:
DayOfWeek dow = LocalDate.now().getDayOfWeek();
答案2
得分: 2
我建议您不要使用 java.util
包中过时且容易出错的日期/时间 API。请使用 java.time
包中的现代日期/时间 API。从**教程:日期时间**了解更多信息。
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDate.now().getDayOfWeek());
System.out.println(LocalDate.now().getDayOfWeek().getValue());
}
}
输出:
SUNDAY
7
> Calendar.DAY_OF_WEEK
也不起作用,或者说两者之间有什么区别。谢谢解释。
Calendar.DAY_OF_WEEK
是一个常量,表示用于 get
和 set
的字段编号,指示星期几。
英文:
I suggest you do not use the outdated error-prone date/time API from java.util
package. Use the modern date/time API from java.time
package. Learn more about it from Trail: Date Time
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDate.now().getDayOfWeek());
System.out.println(LocalDate.now().getDayOfWeek().getValue());
}
}
Output:
SUNDAY
7
> Calendar.DAY_OF_WEEK wouldn't work either, or whats the difference
> between the two. Thanks for the explanation
Calendar.DAY_OF_WEEK
is a constant representing the field number for get
and set
indicating the day of the week.
答案3
得分: 1
Calendar.DAY_OF_WEEK只是一个常量,用于告诉Calendar API您想请求哪些信息:
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_WEEK
Calendar.getInstance()会提供一个Calendar实例。它会考虑当前的时区和本地时间。get()方法使您能够使用上述常量获取信息。
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int)
英文:
Calendar.DAY_OF_WEEK is simply a constant to tell the Calendar API which info you'd like to request:
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_WEEK
Calendar.getInstance() gives you a Calendar instance. This takes things like the current timezone and local time into account. The get() method allows you to get information using the constants above.
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论