> Java > java지도 시간 > 본문

Java의 날짜까지의 타임스탬프

WBOY
풀어 주다: 2024-08-30 16:28:48
원래의
538명이 탐색했습니다.

Java에서 타임스탬프는 날짜 및 시간 표시와 같은 이벤트가 발생할 때마다 형식이 지정되거나 인코딩된 데이터를 제공하기 위한 시퀀스로 정의됩니다. 이 타임스탬프 클래스는 Java.util 라이브러리를 사용하여 Java의 날짜 클래스로 변환될 수 있습니다. Java에서 타임스탬프를 처리할 때 가져옵니다. 일반적으로 타임스탬프는 날짜와 시간 값을 모두 포함하는 SQL 타임스탬프를 표시하기 위해 Java.util 클래스의 날짜 클래스를 사용하여 날짜로 변환되며, Java를 사용하여 타임스탬프를 얻을 수 있는 여러 가지 방법으로 타임스탬프를 날짜로 변환할 수 있습니다. java.util 라이브러리에 속하는 util.Date.

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

Java의 TimeStamp to Date 구문

자바에서 타임스탬프를 날짜로 변환하는 방법에는 3가지가 있습니다. 이러한 다양한 메소드의 구문을 별도로 살펴보겠습니다.

  • 생성자 사용: 타임스탬프를 날짜로 변환하는 한 가지 방법은 java.util.Date 클래스의 생성자를 사용하는 것입니다. 이 생성자의 구문을 살펴보겠습니다.
Date ( long l)
로그인 후 복사
  • 날짜 참조 사용: 타임스탬프 클래스의 인스턴스가 Date에 할당된 Date 클래스를 확장하는 타임스탬프 클래스가 있는 곳입니다. 따라서 이 설명의 구문은 다음과 같습니다.
Timestamp t = new Timestamp();
Date d = t;
로그인 후 복사
  • 캘린더 클래스 사용: 캘린더 클래스에도 있는 타임스탬프에서 날짜를 얻으려면 날짜도 제공합니다. 따라서 이는 다음과 같은 구문으로 설명할 수 있습니다.
Timestamp t = new Timestamp();
Calendar c = Calendar.getInstance();
Calendar.getTime();
로그인 후 복사

Java에서 타임스탬프를 날짜로 변환하는 방법

Java에서 타임스탬프는 날짜 및 달력 클래스를 사용하기 위한 java.util 클래스를 사용하는 타임스탬프 클래스를 사용하여 표시되는 시간 및 날짜 형식으로 정의되는 반면, Java에서는 타임스탬프 클래스를 사용하기 위한 java.sql 패키지가 있습니다. Java 프로그램에서 타임스탬프를 구현합니다. 타임스탬프를 날짜로 변환하면 날짜와 시간이 모두 사람이 읽을 수 있는 형식인 밀리초 단위로 표시됩니다. 따라서 타임스탬프를 날짜로 변환하는 정확한 작업은 3가지 다른 방법을 사용하여 수행되며 java.util 또는 java.sql 패키지를 사용하는 타임스탬프 클래스를 사용하여 수행됩니다.

1. 생성자 사용

Java에는 java.util 패키지에서 제공하는 java.util.Date 클래스가 있으며 이 클래스의 생성자를 사용하여 타임스탬프를 날짜로 변환합니다. 이 변환 방법의 작동 방식은 먼저 Date 클래스 생성자에서 매개 변수로 긴 값을 가져오고, 그 결과 getTime() 메서드를 사용하여 긴 값을 변환하는 타임스탬프 객체가 생성됩니다

import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = new Date(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using date constructor : ");
System.out.println(" \n ");
System.out.println( d );
}
}
로그인 후 복사

출력:

Java의 날짜까지의 타임스탬프

위의 예에서는 날짜 생성자를 사용하기 위해 먼저 java.sql, Timestamp 패키지 및 java.util.Date 패키지를 가져왔으며 Date 클래스가 필요하고, 타임스탬프를 사용하려면 Timestamp 클래스를 사용하는 것을 볼 수 있습니다. 따라서 먼저 Timestamp 클래스에 속하는 변수 “t”를 정의 및 선언하고 “new”를 사용하여 이를 정의한 다음 currentTimeMills() 함수를 전달하여 나중에 날짜로 변환되는 타임스탬프를 표시합니다. 그런 다음 위의 타임스탬프를 날짜 형식으로 변환하기 위해 getTime() 함수를 전달하는 "new"를 사용하여 정의된 Date 클래스 "d"의 Date 변수를 선언합니다. 그런 다음 println을 사용하여 얻은 타임스탬프에서 변환된 날짜를 표시합니다. 위 프로그램의 출력은 위 스크린샷과 같습니다.

2. 날짜 참조 사용

java.util.date 패키지의 날짜 클래스를 다시 사용하여 Java에서 타임스탬프를 날짜로 변환하는 또 다른 방법입니다. 이 방법에서는 타임스탬프 인스턴스가 날짜에 직접 할당되는 날짜 클래스를 확장하므로 타임스탬프에서 더 이른 날짜 형식으로 출력되는 날짜 개체가 확장됩니다. Java에서 이러한 방법을 시연하는 예를 아래에서 살펴보겠습니다.

import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = t;
System.out.println(" Demonstration  of timestamp to date conversion using date reference : ");
System.out.println(" \n ");
System.out.println( d );
}
}
로그인 후 복사

출력:

Java의 날짜까지의 타임스탬프

위 예에서는 이전 예와 유사하지만 위 프로그램과 이 프로그램의 유일한 차이점은 여기에서 날짜 클래스를 정의하는 대신 타임스탬프 인스턴스 "t"를 날짜 개체에 직접 할당한다는 것입니다. "d", 여기서는 날짜 객체를 타임스탬프 인스턴스에 직접 할당하므로 getTime() 함수를 사용하지 않습니다. 따라서 여기서 출력은 타임스탬프와 유사해 보이지만 밀리초도 포함하는 시간이 포함된 날짜 형식입니다. 이는 위 스크린샷에서 확인할 수 있습니다.

3. Using Calendar Class

Another method of converting timestamp to date in java is using calendar class which is rarely used by any developers. We can obtain calendar class by the java.util.Calendar package where we use this calendar class along with which we have to use getInstance() function to get timestamp instance and getTime() to get the date along with time which will be converted from the timestamp. Let us see an example below:

Example
import java.sql.Timestamp;
import java.util.Date;
import java.util.Calendar;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using Calendar class : ");
System.out.println(" \n ");
System.out.println(calendar.getTime());
}
}
로그인 후 복사

Output:

Java의 날짜까지의 타임스탬프

In the above example, we can see we have imported Calendar class also from java.util.Calendar package and this example are similar to the first example where we used date class. In this example, we define the calendar instance by using the getInstance() function which will be in the timestamp format later by using getTime() the timestamp instance is converted to a date object which results as shown in the output in the above screenshot.

Conclusion

In this article, we conclude that the timestamp to date conversion in java is very simple. In Java, there is java.util package which provides different classes for supporting this conversion in various ways with different classes. It depends on the user on which method to use according to the user’s requirement and time accuracy which displays the result accurately as required.

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

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