搜索
首页Javajava教程什么是Java中的日期时间字段?

什么是Java中的日期时间字段?

Sep 04, 2023 pm 02:41 PM
时间(time)日期(date)字段(field)

什么是Java中的日期时间字段?

时间字段是日期时间字段,例如一年中的月份或分钟中的小时。这些字段由 TemporalField 接口表示,ChronoField 类实现该接口。

以下是 ChronoField 类支持的有关日期的各种时间字段的列表 -

ALIGNED_DAY_OF_WEEK_IN_YEAR

字段 描述
ALIGNED_DAY_OF_WEEK_IN_MONTH

该字段表示一个月中的星期几。

此字段表示一年中一周的对齐日期。

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: 11
Day of the month: 3
Day of the month: 316
Day of the month: 18577
Week in the month: 4
Day of the week in an year: 1
Era: 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.171
Year: 2019
Year: 8
Year: 3

以上是什么是Java中的日期时间字段?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境