시간 필드는 월-오-분 또는 시와 같은 날짜-시간 필드입니다. 이러한 필드는 TemporalField 인터페이스로 표시되며 ChronoField 클래스는 이 인터페이스를 구현합니다.
LocaldateTime 클래스의 get() 또는 getLong() 메서드는 시간 필드를 매개변수로 받아 현재 객체에서 주어진 필드의 값을 가져옵니다.
예시
다음 예에서는 날짜와 관련된 시간 필드의 날짜 값을 검색합니다.
import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class Demo { public static void main(String args[]) { //Instantiating the LocalDateTime class LocalDateTime lDate = LocalDateTime.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: 25 Day of the month: 5 Day of the month: 237 Day of the month: 17403 Week in the month: 4 Day of the week in an year: 6 Am or Pm: 6 Era: 1
예시
다음 예제는 날짜에서 시간 필드와 관련된 시간 값을 검색합니다.
import java.time.Clock; import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class CreateDateTime { public static void main(String args[]) { //Instantiating the LocalDateTime class LocalDateTime lDateTime = LocalDateTime.now(); System.out.println(lDate); int field = lDateTime.get(ChronoField.CLOCK_HOUR_OF_AMPM); System.out.println("Hour of the day: "+field); field = lDateTime.get(ChronoField.AMPM_OF_DAY); System.out.println("Am or Pm: "+field); field = lDateTime.get(ChronoField.CLOCK_HOUR_OF_DAY); System.out.println("Hour of the day: "+field); long epoch = lDateTime.getLong(ChronoField.MINUTE_OF_DAY); System.out.println("Minute of the day: "+epoch); field = lDateTime.get(ChronoField.MINUTE_OF_HOUR); System.out.println("Minutes of the hour: "+field); field = lDateTime.get(ChronoField.SECOND_OF_DAY); System.out.println("Seconds of the day: "+field); field = lDateTime.get(ChronoField.SECOND_OF_MINUTE); System.out.println("Seconds of the minute: "+field); } }
출력
2020-11-11T14:58:22.680 Hour of the day: 2 Am or Pm: 1 Hour of the day: 14 Minute of the day: 898 Minutes of the hour: 58 Seconds of the day: 53902 Seconds of the minute: 22