> Java > java지도 시간 > 본문

자바 타임스탬프

PHPz
풀어 주다: 2024-08-30 15:39:02
원래의
1283명이 탐색했습니다.

Java Timestamp는 java.util.Date의 Thin Wrapper에 속하며 JDBC API에서 SQL TIMESTAMP 유형 값으로 인식할 수 있도록 합니다. 타임스탬프는 타임스탬프 값에 대한 JDBC 구문을 이스케이프하는 데 도움이 되는 형식 지정 및 구문 분석과 같은 작업을 지원합니다.

광고 이 카테고리에서 인기 있는 강좌 JAVA MASTERY - 전문 분야 | 78 코스 시리즈 | 15가지 모의고사

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

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

이 Java 타임스탬프의 정밀도는 다음 중 하나로 알려져 있습니다.

  • 19(yyyy-mm-dd hh:mm:ss 형식의 총 문자 수입니다.
  • 20 + s 정밀도는 yyyy-mm-dd hh:mm:ss 형식의 총 문자 수입니다. 여기서 s는 주어진 타임스탬프의 소수 자릿수 초 정밀도를 나타냅니다.

참고: 타임스탬프는 java.util.Date 유형의 복합에 속하며 별도의 나노초 값을 갖습니다. 저장된 값은 java.util.Date 구성 요소의 정수 초입니다. 분리된 나노는 소수 초에 속합니다. Timestamp.equals(Object) 메소드는 java.sql.Timestamp 인스턴스에 속하지 않는 객체가 전달될 때 true를 반환하지 않습니다. 그 이유는 Nanos 구성 요소가 현재까지 알려지지 않았기 때문입니다. 이는 java.util.Date.equals(Object) 메소드와 비교하여 Timestamp.equals(Object) 메소드의 비대칭성을 초래합니다. 기본 java.util.Date 구현은 해시코드 메소드에서 사용되므로 Nanos는 계산에 포함되지 않습니다.

구문:

Timestamp(long time)
로그인 후 복사

밀리초 시간 값을 사용하여 타임스탬프 값을 구성합니다.

Java 타임스탬프 방법

Java의 타임스탬프 기능 작동: 타임스탬프는 다양한 방법으로 사용될 수 있으며 그 중 몇 가지에 대해 아래에서 자세히 설명합니다.

1. 이후

현재 Timestamp 개체가 지정된 개체보다 이후인지 표시합니다.

public boolean after(Timestamp tm)
로그인 후 복사

tm이 Timestamp의 값인 경우, 이 Timestamp 객체와 비교하면 이 함수는 부울 값을 반환합니다. 이 타임스탬프 값이 이후이면 true이고 그 반대의 경우도 마찬가지입니다.

2. 이전

현재 Timestamp 개체가 해당 개체보다 앞에 오는지 표시합니다.

public boolean before(Timestamp tm)
로그인 후 복사

여기서 tm은 Timestamp 객체를 비교하는 Timestamp의 값입니다. 이 함수는 부울 값을 반환합니다. 이 Timestamp 값이 이전이거나 그 반대이면 true입니다.

3. 비교

주어진 Timestamp 객체를 이것과 비교하는 비교 함수입니다.

public int compareTo(Timestamp tm)
로그인 후 복사

tm이 이 Timestamp 객체와 비교할 객체인 경우, 두 객체가 동일하면 이 함수는 0 값을 반환합니다. 이 객체가 주어진 객체 앞에 오면 0보다 작은 값이고, Timestamp 객체가 주어진 Timestamp 객체 뒤에 오면 0보다 큰 값입니다.

4. 같음

이 기능을 사용하면 주어진 두 개의 Timestamp 객체가 동일한지 여부를 확인할 수 있습니다.

public boolean equals(Timestamp tm)
로그인 후 복사

tm이 Timestamp 값을 참조하는 경우 이를 Timestamp 개체와 비교해야 합니다. 이 함수는 부울 값을 반환합니다. 값이 같으면 true가 반환되고, 그렇지 않으면 false가 반환됩니다.

5. 시간 가져오기

이 함수는 이 Timestamp 개체가 나타내는 기본 날짜 값인 1970년 1월 1일, 00:00:00 GMT부터 시작하여 총 밀리초 수를 제공합니다.

public long getTime()
로그인 후 복사
로그인 후 복사

이 함수는 Date 클래스의 getTime 함수(있는 경우)를 재정의합니다. 위와 같이 기본 날짜로부터 밀리초 단위의 숫자를 반환합니다.

6. getNanos

이 함수는 Nanos에서 이 Timestamp 개체의 값을 가져오는 데 사용됩니다.

public int getNanos()
로그인 후 복사

이 함수는 개체의 분수 초 매개변수를 반환합니다.

7. to인스턴트

Timestamp 개체를 Instant로 변경하는 데 사용됩니다. 이 변환 중에 이 타임스탬프와 동일한 선의 지점을 나타내는 Instant가 생성됩니다.

public Instant toInstant()
로그인 후 복사

이 함수는 Date 클래스의 toInstant 메소드를 재정의합니다. 타임라인 상에서 같은 지점을 나타내는 순간을 반환합니다.

8. 시간 설정

이 함수는 1970년 1월 1일 00:00:00 GMT 이후의 시간을 밀리초 단위로 나타내도록 Timestamp 개체를 설정합니다.

public void setTime(long time)
로그인 후 복사

이 함수는 Date 클래스의 setTime 메소드를 재정의합니다. 입력 매개변수 시간, 즉 밀리초 단위의 시간이 걸립니다.

9. 시간 가져오기

이 함수는 이 Timestamp 객체가 나타내는 1970년 1월 1일 00:00:00 GMT 이후의 시간을 밀리초 단위로 가져오는 데 사용됩니다.

public long getTime()
로그인 후 복사
로그인 후 복사

이 함수는 위에서 언급한 기본 시간 이후의 시간을 밀리초 단위로 반환합니다.

10. value

JDBC 타임스탬프 이스케이프 형식에 속하는 String 객체를 Timestamp 값 유형으로 변환하는 메소드입니다.

public static Timestamp valueOf(String str)
로그인 후 복사

An str parameter is the timestamp of format yyyy-[m]m-[d]d hh:mm:ss and the fractional seconds can be ignored. The leading zero given for mm and dd can also be ignored. This function returns its respective Timestamp value. And throws an IllegalArgumentException when the parameter is given is not of the mentioned format.

Examples to Implement Java Timestamp

Below are the examples of Java Timestamp:

Example #1

Code:

// Java program to demonstrate the
// functionalit of getTime() function
import java.sql.*;
class Demo {
public static void main(String args[])
{
// Here we are creating 2 timestamp objects
Timestamp tm = new Timestamp(2000);
// Displaying the created timestamp object
System.out.println("The Timestamp time is : "
+ tm.toString());
System.out.println("The Time in milliseconds is : "
+ tm.getTime());
}
}
로그인 후 복사

Output:

자바 타임스탬프

In the above example, we are first creating a timestamp object. Then we are printing out using two different functions toString and getTime to check the output.

Example #2

Code:

// Below Java code is to showcase the
// functionality of getTime() function
import java.sql.*;
public class Example {
public static void main(String args[])
{
// Creating 2 timestamp objects
Timestamp tm = new Timestamp(-2000);
// Display the timestamp object
System.out.println("Timestamp time is : "
+ tm.toString());
System.out.println("Total time in milliseconds : "
+ tm.getTime());
}
}
로그인 후 복사

Output:

자바 타임스탬프

In the above example, we are first creating 2 timestamp objects and using the getTime() function to fetch the timestamp object’s time and initialize it to a time before the default time of Jan 1 1970. Hence the negative long value given does the purpose.

Advantages of using Timestamp in Java

  • We can notice a clear separation with the Timestamp object’s help between two different time duration, especially Instant and Duration or fragment-related definitions. Ex LocalDate, LocalTime.
  • In comparison with java.Util.The date this Timestamp object allows a better set of functions for manipulating and calculating the logic.
  • It also covers the conversion of units with the help of Duration.toDays().
  • It also covers the Timezone hell with the help of ZonedDateTime.

Conclusion

As seen above, the Timestamp is an extension of java.util.Date class is used as a wrapper to that class in JDBC API to maintain specific SQL requirements. It is mostly used when working with databases and gives output in nanoseconds precision.

위 내용은 자바 타임스탬프의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!