java中Calendar.getInstance()。get(Calendar.DAY_OF_WEEK)和Calander.DAY_OF_WEEK有什么区别 [英] What's the difference between Calendar.getInstance().get(Calendar.DAY_OF_WEEK) and Calander.DAY_OF_WEEK in java

查看:689
本文介绍了java中Calendar.getInstance()。get(Calendar.DAY_OF_WEEK)和Calander.DAY_OF_WEEK有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kotlin的新手,在上一门课程时,您在当前工作日中有些工作。

I'm new to Kotlin and while doing a course there was a bit where you worked with the current weekday.

该课程使用以下Java代码来获取它:

The course used this java code to get it:

导入java.util。*

Calendar.getInstance()。 get(Calendar.DAY_OF_WEEK)

但我不明白为什么
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

推荐答案

Calendar.DAY_OF_WEEK 是一个常数,用于日历对象中的访问字段。

Calendar.DAY_OF_WEEK is a constant number, used to access fields within the Calendar object.

Calendar.getInstance()。get(Calendar.DAY_OF_WEEK)使用此常数读取星期几的值

Calendar.getInstance().get(Calendar.DAY_OF_WEEK) uses this constant number to read the value of the "day of week" field from the calendar.

这有点不寻常。无需添加诸如 getDayOfWeek, setDayOfWeek和 addDayOfWeek之类的数十种方法,对于所有日历字段,Calendar类的设计者将 get添加到日历类别。 设置和添加

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.

请注意,当今将Calendar视为旧版方法。 class-对于新代码,最好使用 java.time 包中的类。获取星期几的现代方法是:

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();

这篇关于java中Calendar.getInstance()。get(Calendar.DAY_OF_WEEK)和Calander.DAY_OF_WEEK有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆