What is a datetime field in Java?
A time field is a datetime field, such as a month in a year or an hour in a minute. These fields are represented by the TemporalField interface, which the ChronoField class implements.
The following is a list of various time fields related to dates supported by the ChronoField class -
Field | Description |
---|---|
ALIGNED_DAY_OF_WEEK_IN_MONTH | This field represents the day of the week in the month. |
This field represents the aligned day of the week of the year. | |
ALIGNED_WEEK_OF_MONTH | This field represents the aligned week of the month. |
ALIGNED_WEEK_OF_YEAR | This field represents the aligned anniversary. |
DAY_OF_MONTH | This field represents the day of the month. |
DAY_OF_WEEK | This field represents the day of the week. |
DAY_OF_YEAR | This field represents the day of the year. |
EPOCH_DAY | This field represents the epoch day of the year. |
ERA | This field represents the era of the current year. |
Year | This field represents the year. |
YEAR_OF_ERA | This field represents the year of the era. |
The get() or getLong() methods of the LocalDate and LocaldateTime classes accept the time field as a parameter and get the value of the given field in the current object .
Example
Live Demonstration
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); } }
Output
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
Example
Live Demonstration
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); } }
Output
20:01:43.171 Year: 2019 Year: 8 Year: 3
The above is the detailed content of What is a datetime field in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
