时间字段是日期时间字段,例如一年中的月份或分钟中的小时。这些字段由 temporalfield 接口表示,chronofield 类实现该接口。
以下是 chronofield 类支持的有关日期的各种时间字段的列表 -
字段描述
aligned_day_of_week_in_month
该字段表示一个月中的星期几。
aligned_day_of_week_in_year
此字段表示一年中一周的对齐日期。
aligned_week_of_month
此字段表示一个月的对齐周。
aligned_week_of_year
此字段表示对齐的周年。
day_of_month
此字段代表一个月中的第几天。
day_of_week
该字段代表一周中的某一天。
day_of_year
此字段代表一年中的第几天。
epoch_day
该字段代表一年中的纪元日。
era
该字段代表当年的时代。
年份
该字段代表年份。
year_of_era
该字段代表时代的年份。
localdate 和 localdatetime 类的 get() 或 getlong() 方法接受时间字段作为参数,并获取当前对象中给定字段的值。
示例现场演示
import java.time.localdate;import java.time.temporal.chronofield;public class demo { public static void main(string args[]) { //instantiating the localdate class localdate ldate = localdate.now(); int field = ldate.get(chronofield.day_of_month); system.out.println("day of the month: "+field); field = ldate.get(chronofield.day_of_week); system.out.println("day of the month: "+field); field = ldate.get(chronofield.day_of_year); system.out.println("day of the month: "+field); long epoch = ldate.getlong(chronofield.epoch_day); system.out.println("day of the month: "+epoch); field = ldate.get(chronofield.aligned_day_of_week_in_month); system.out.println("week in the month: "+field); field = ldate.get(chronofield.aligned_day_of_week_in_year); system.out.println("day of the week in an year: "+field); field = ldate.get(chronofield.era); system.out.println("era: "+field); }}
输出day of the month: 11day of the month: 3day of the month: 316day of the month: 18577week in the month: 4day of the week in an year: 1era: 1
示例现场演示
import java.time.dayofweek;import java.time.localtime;import java.time.month;import java.time.year;import java.time.temporal.chronofield;public class demo { public static void main(string args[]) { //instantiating the localdatetime class localtime ltime = localtime.now(); system.out.println(ltime); int field = year.of(2019).get(chronofield.year); system.out.println("year: "+field); field = month.of(8).get(chronofield.month_of_year); system.out.println("year: "+field); field = dayofweek.of(3).get(chronofield.day_of_week); system.out.println("year: "+field); }}
输出20:01:43.171year: 2019year: 8year: 3
以上就是什么是java中的日期时间字段?的详细内容。