>  기사  >  Java  >  java.util.날짜

java.util.날짜

王林
王林원래의
2024-08-30 15:49:14917검색

Java의 날짜와 시간은 java.util.Date 클래스라는 클래스로 표현됩니다. 이 클래스는 Java에서 시간과 날짜를 처리하는 메서드와 생성자를 제공하고 Serialized, Comparable Cloneable 인터페이스는 java.util.Date 클래스에 의해 구현되며 java.sql.Date, java.sql.Time 및 java.sql.Timestamp 인터페이스는 java.util.Date 클래스의 기본 클래스입니다.

java.util.Date 생성자

java.util.Date 클래스의 생성자는 다음과 같습니다.

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

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

1. Date(): Date() 생성자를 사용하여 현재 시간과 날짜를 나타내는 날짜 개체가 생성됩니다.

2. Date(long milliseconds): Date(long milliseconds) 생성자를 사용하여 1970년 1월 1일 st 00:00:00 GMT 이후 지정된 밀리초 동안 날짜 객체가 생성됩니다. .

java.util.Date의 메소드

java.util.Date 클래스의 메소드는 다음과 같습니다.

1. booleanafter(Date date): Boolean after(Date date) 메소드는 현재 날짜가 파라미터로 지정된 날짜 이후인지 확인하는 데 사용됩니다.

2. booleanbefore(Date date): Boolean before(Date date) 메소드는 현재 날짜가 q파라미터로 지정한 날짜보다 이전인지 확인하는 데 사용됩니다.

다음은 java.util.Date의 예입니다.

코드:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
// a variable called dat1 is defined to store one date
Date dat1=new Date(2020,6,01);
//a variable called dat2 is defined to store another date
Date dat2=new Date(2021,6,01);
//boolean after(Date date) method is used to check if dat1 comes after dat2 date
System.out.println("The date represented by the variable dat1 is after the date represented by the variable dat2 : "+dat1.after(dat2));
//boolean before(Date date) method is used to check if dat1 comes before dat2 date
System.out.println("The date represented by the variable dat1 is before the date represented by the variable dat2 : "+dat1.before(dat2));
}
}

출력:

java.util.날짜

설명: 위 프로그램에서는 dat1이라는 변수가 하나의 날짜를 저장하도록 정의되어 있습니다. 그런 다음 다른 날짜를 저장하기 위해 dat2라는 변수가 정의됩니다. 그런 다음 boolean after(Date date) 메소드를 사용하여 dat1이 dat2 날짜 이후에 오는지 확인하고 반환된 출력이 true 또는 false인지 확인합니다. 그런 다음 boolean before(Date date) 메소드를 사용하여 dat1이 dat2 날짜보다 앞에 왔는지 확인하고 반환된 출력이 true 또는 false인지 확인합니다.

3. Object clone(): 현재 날짜의 복제 객체를 반환하기 위해 object clone() 메소드를 사용합니다.

다음은 java.util.Date의 예입니다.

코드:

import java.util.Date;
public class Main
{
public static void main(String[] args)
{
//an instance of the date class is created
Date dat=new Date(2020,6,01);
//clone method is called on the instance of the date class to create a clone of the given date
System.out.println("The clone of the given date is : "+dat.clone());
}
}

출력:

java.util.날짜

설명: 위 프로그램에서는 날짜 클래스의 인스턴스가 생성됩니다. 그런 다음 날짜 클래스 인스턴스에서 clone 메소드가 호출되어 지정된 날짜의 복제본을 생성합니다.

4. intcompareTo(Date date): int CompareTo(Date date) 메소드는 현재 날짜와 매개변수로 지정된 날짜를 비교하는 데 사용됩니다. 현재 날짜가 매개변수로 지정된 날짜와 동일한 경우 출력은 0으로 반환됩니다. 현재 날짜가 매개변수로 지정된 날짜 이전인 경우 출력은 0보다 작은 값으로 반환됩니다. 현재 날짜가 매개변수로 지정된 날짜 이후인 경우 0보다 큰 출력이 반환됩니다.

5. booleanequals(Date date): boolean equals(Date date) 메소드는 현재 날짜와 동등 매개변수로 지정된 날짜를 비교하는 데 사용됩니다. 현재 날짜가 매개변수로 지정된 날짜와 동일하면 출력이 true로 반환되고, 그렇지 않으면 출력이 false로 반환됩니다.

다음은 java.util.Date의 예입니다.

코드:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat1=new Date(2020,6,01);
//an instance of the date class is created to store another date
Date dat2=new Date(2021,6,01);
//compareTo method is used to compare the two dates stored using the two variables defined before
int comp=dat1.compareTo(dat2);
//the result is displayed after comparing the two dates
System.out.println("The result after comparing the two values is : "+comp);
//Equals() method of date class is used to check if the two dates specified are equal
System.out.println("The dates stored in the two variables are equal : "+dat1.equals(dat2));
}
}

출력:

java.util.날짜

설명: 위 프로그램에서는 하나의 날짜를 저장하기 위해 날짜 클래스의 인스턴스가 생성됩니다. 그런 다음 다른 날짜를 저장하기 위해 날짜 클래스의 인스턴스가 생성됩니다. 그런 다음 CompareTo 메서드를 사용하여 이전에 정의된 두 변수를 사용하여 저장된 두 날짜를 비교합니다. 그런 다음 두 날짜를 비교한 후 결과가 표시됩니다. 그런 다음 이전에 정의한 두 변수를 사용하여 저장된 두 날짜가 동일한지 확인하기 위해 equals 메소드를 사용합니다.

6. static Date from(Instant instant): static Date from(Instant instant) 메소드는 인스턴트 날짜로부터 날짜 객체의 인스턴스를 반환하는 데 사용됩니다.

7. long getTime(): long getTime() 메소드는 날짜 객체가 나타내는 시간을 반환하는 데 사용됩니다.

8. inthashCode(): int hashCode() 메서드는 날짜 객체가 나타내는 해시 코드 값을 반환하는 데 사용됩니다.

9. Instant toInstant(): Instant toInstant() 메소드는 현재 날짜를 인스턴트 객체로 변환하는 데 사용됩니다.

10. String toString(): 문자열 toString() 메소드는 주어진 날짜를 인스턴트 개체로 변환하는 데 사용됩니다.

다음은 java.util.Date의 예입니다.

코드:

import java.time.Instant;
import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created which stores the present date
Date dat=new Date();
//instant date and time is obtained using instant.now() method
Instant inst= Instant.now();
//the instant date and time is displayed by using from() method
System.out.println("The instant date is  : "+dat.from(inst));
//getTime() method is used to obtain the number of milliseconds since January 1st 1970
System.out.println("The number of milliseconds since January 1, 1970, 00:00:00 GTM : "+dat.getTime());
//hashcode() method is used to obtain the hash code of the given date
System.out.println("The hash code for the given date is : "+dat.hashCode());
//toInstant() method is called to convert the present date to the instant object
System.out.println("The instant object after converting the present date : "+dat.toInstant());
//toString() method is used to convert the date to string
System.out.println("The date after conversion to string is : "+dat.toString());
}
}

출력:

java.util.날짜

Explanations: In the above program, an instance of the date class is created to store the present date. Then instant date and time are obtained using instant.now() method. Then the instant date and time are displayed by using from the () method. Then getTime() method is used to obtain the number of milliseconds since January 1st, 1970. Then hashcode() method is used to obtain the hash code of the given date. Then toInstant() method is called to convert the present date to the instant object. Then toString() method is used to convert the date to string.

11. void setTime(long time): void setTime(long time) method is used to set the present date and time to the time specified as a parameter.

Below are the examples of java.util.Date:

Code:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat=new Date(2020,6,01);
long lil=2000;
//settime() method is used to set the time to the given time specified
dat.setTime(lil);
System.out.println("The time after setting the time to given time is : "+dat.toString());
}
}

Output:

java.util.날짜

Explanations: In the above program, an instance of the date class is created to store one date. Then setTime() method id used to set the present time to the given date.

Conclusion

In this tutorial, we understand the concept of java.util.Date in Java through definition, its constructors and methods through programming examples and their outputs.

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

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