>  기사  >  Java  >  Java 오프셋 날짜시간

Java 오프셋 날짜시간

WBOY
WBOY원래의
2024-08-30 15:51:45360검색

Java8에 OffsetDateTime이 도입되어 더 정확하고 정밀하게 데이터 및 시간 필드를 저장하며, 이는 다양한 매체에서 데이터를 전송할 때 유용합니다. 이러한 필드는 최대 나노초 단위의 정밀도로 DateTime 값을 저장합니다. 또한 이러한 필드에는 UTC/그리니치와의 오프셋이 있습니다. 오프셋과 함께 날짜-시간을 변경할 수 없게 표현한 것입니다. 이 클래스는 java에 속합니다. Time 패키지이며 java.lang.Object를 슈퍼클래스로 갖습니다. UTC/그리니치로부터의 오프셋을 추가하면 현지 날짜-시간을 얻을 수도 있습니다. 따라서 데이터베이스나 네트워크를 통해 통신할 때 최고의 성능을 발휘합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

아래는 java의 멤버인 OffsetDateTime 클래스의 구문입니다. 시간 수업.

public final class OffsetDateTime extends Object implements Serializable, Temporal, TemporalAdjuster, Comparable<OffsetDateTime>

Java OffsetDateTime의 인터페이스

이 클래스는 Java 패키지의 Object 클래스를 상속합니다. 이를 통해 아래에 제공된 많은 인터페이스도 구현합니다.

1. 직렬화 가능

어떤 메소드도 포함하지 않은 마커 인터페이스입니다. 이 인터페이스를 구현하면 OffsetDateTime이 직렬화 및 역직렬화를 지원한다는 사실을 Java에 알리는 데 도움이 됩니다. 즉, 개체를 바이트 스트림으로 쉽게 변환할 수 있습니다. 또한 바이트 스트림을 실제 Java 객체로 변환할 수 있습니다.

2. 시간적

객체에 대한 읽기-쓰기 액세스를 정의하는 프레임워크 수준의 인터페이스입니다. OffsetDateTime은 이 인터페이스를 사용하여 플러스-마이너스 조작이 가능하도록 충분히 완성됩니다.

3. 시간조정자

이 인터페이스는 월의 마지막 날로 설정해야 하는 날짜 조정과 같은 임시 개체를 수정하는 도구를 제공합니다. 이 인터페이스를 구현하면 OffsetDateTime이 비즈니스 디자인 패턴에 따라 외부적으로 조정할 수 있습니다.

4. 비교 가능

이 인터페이스는 해당 필드 중 하나를 기준으로 클래스 개체의 순서를 지정하는 데 도움이 됩니다. 이를 위해 객체를 정렬할 수 있는 comapreTo(Object) 함수를 제공합니다. 따라서 이 함수를 사용하면 OffsetDateTime 개체를 빠르게 정렬할 수 있습니다.
OffsetDateTime, ZonedDateTime 및 Instant는 아래에 제공된 최대 나노초의 정밀도로 순간을 저장하는 데 도움이 되는 Java8 클래스입니다.

  • 인스턴트: 세 가지 중 가장 간단합니다. OffsetDateTime 클래스는 UTC/그리니치의 오프셋을 순간에 추가하여 현지 날짜-시간을 얻는 데 도움이 됩니다. zonedDateTime 클래스는 풀타임 존 규칙을 추가합니다.
  • ZonedDateTime: 사용이 더 간편하고 DST를 완벽하게 인식하므로 일광 절약 시간제 조정이 훨씬 쉬워집니다. ISO-8601 달력 시스템의 현지 시간 오프셋을 사용합니다.
  • OffsetDateTime: 이 클래스는 ZonedDateTime과 유사하지만 ISO-8601 달력 시스템에서 UTC/그리니치의 오프셋을 사용합니다. 주로 이는 데이터베이스 및 네트워크를 다룰 때 선호되는 통신 방법입니다.

이 클래스에는 액세스 가능한 생성자가 없습니다. 그것은 최종적이고 불변입니다. 따라서(==) 개체에 대한 ID 해시 코드 사용은 금지됩니다. 이러한 유형의 클래스를 값 기반 클래스라고도 합니다. 따라서 이러한 클래스를 비교하려면 .equals() 메서드가 선호됩니다. 예를 들어 OffsetDateTime에 저장된 값은 "2nd October 2007 at 13:45.30.123456789 +02:00"으로 표시될 수 있습니다.

필드:

Field Name Description
MAX It is a static field of OffsetDateTime type, which stores the maximum supported value that is.

‘+999999999-12-31T23:59:59.999999999-18:00’

MIN It is a static field of OffsetDateTime type, which stores the maximum supported value that is.

‘-999999999-01-01T00:00:00+18:00’

필드 이름 설명 최대 OffsetDateTime 유형의 정적 필드로 지원되는 최대 값을 저장합니다. '+999999999-12-31T23:59:59.999999999-18:00' MIN OffsetDateTime 유형의 정적 필드로 지원되는 최대 값을 저장합니다. '-999999999-01-01T00:00:00+18:00'

Methods of Java OffsetDateTime

Let’s see some of the Java OffsetDateTime methods.

1. int get(TemporalField field)

It is used to get the int value from a date-time field.

2. int getDayOfMonth()

You can use this function to retrieve the numerical value of the day in a given month, and it will return a value between – 1 to 31.

Code:

import java.time.OffsetDateTime;
public class Demo {
public static void main(String[] args) {
OffsetDateTime mydate = OffsetDateTime.parse("2020-01-26T12:30:30+01:00");
System.out.println("DayOfMonth output - "+mydate.getDayOfMonth());
}
}

Output:

Java 오프셋 날짜시간

3. int getDayOfYear()

This function gives the day of the year field from the value specified. Its output is an integer within the range of 1 to 365.

Code:

import java.time.OffsetDateTime;
public class HelloWorld{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("DayOfYear output - "+mydate.getDayOfYear());
}
}

Output:

Java 오프셋 날짜시간

4. DayOfWeek getDayOfWeek()

This function returns an enum of DayOfWeek to tell which day of the week is specified. Enum consists of int value and the names of the week that help avoid confusion about what the number represents.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("DayOfWeek output - "+ mydate.getDayOfWeek());
}
}

Output:

Java 오프셋 날짜시간

5. OffsetDateTime minusDays(long days)

This function returns the OffsetDateTime object after subtracting the specified number of days from it.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("minusDays output - "+ mydate.minusDays(2)); } }

Output:

Java 오프셋 날짜시간

6. Static OffsetDateTime now()

This function returns the current date-time from the system clock in the time zone. Return type if OffsetDateTime only.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("now output - "+ mydate.now());
}
}

Output:

Java 오프셋 날짜시간

7. OffsetDateTime plusDays(long days)

This function returns the OffsetDateTime object after adding the specified number of days to it.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("plusDays output - "+ mydate.plusDays(5)); } }

Output:

Java 오프셋 날짜시간

8. LocalDate toLocalDate()

This function returns the LocalDate part of date-time.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toLocalDate output - "+ mydate.toLocalDate());
}
}

Output:

Java 오프셋 날짜시간

9. Offset toOffsetTime()

This function helps to convert date-time to Offset Time.

 Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toOffsetTime output - "+ mydate.toOffsetTime());
}
}

Output:

Java 오프셋 날짜시간

10. ZonedDateTime toZonedDateTime()

This function helps convert the object to ZonedDateTime type, a fully DST-aware date-time representation that handles daylight saving conversion much easier.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toZonedDateTime output - "+ mydate.toZonedDateTime());
}
}

Output:

Java 오프셋 날짜시간

Conclusion

The OffsetDateTime class introduces the storage of date-time fields with precision up to nanoseconds. It utilizes an offset of UTC/Greenwich in the ISO calendar system. These options find the highest preference when working with databases or transferring data over the network. It supports many functions to extract different information in different formats.

위 내용은 Java 오프셋 날짜시간의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:자바 로컬 날짜다음 기사:자바 로컬 날짜