> Java > java지도 시간 > 본문

자바 LocalDateTime

WBOY
풀어 주다: 2024-08-30 15:52:00
원래의
799명이 탐색했습니다.

Java의 LocalDateTime은 출력 화면에 현지 날짜와 시간을 표시합니다. 시간 표시의 기본 형식은 YYYY-MM-DD-hh-mm-ss-zzz입니다. 날짜와 시간을 표시하는 다양한 요소는 연도, 월, 일, 시, 분, 초 및 나노초입니다. 특정 일수에 날짜와 시간을 더하고, 그 일수를 뺄 수 있으며, 최종적으로 매우 원활하게 출력이 생성될 수 있습니다. LocalDateTime 클래스는 최종 클래스입니다. 따라서 클래스 확장은 허용되지 않습니다. LocalDateTime 클래스는 equals()를 이용하여 두 날짜와 시간이 서로 같은지 여부를 확인하는 데 사용되는 값 기반 클래스입니다.

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

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

Java LocalDateTime 구문

Java LocalDateTime 클래스는 java.time 패키지의 일부입니다. 다음과 같은 방법으로 클래스의 Java 인스턴스를 생성할 수 있습니다.

다음은 구문입니다.

import java.time.LocalDateTime;
로그인 후 복사

LocalDateTime 클래스의 of()에 값을 전달할 수도 있습니다.

다음 구문은 다음과 같습니다.

LocalDateTime Idt= LocalDateTime.of(2011,15,6,6,30,50,100000);
로그인 후 복사

parse()를 사용하여 문자열 표현으로 시간 값을 전달할 수도 있습니다.

LocalDateTime Idt= LocalDateTime.parse("2011-11-10T22:11:03.46045");
로그인 후 복사

또한 ID 및 Zone ID 정보를 전달하여 ofInstant()를 사용할 수 있습니다.

LocalDateTime Idt= LocalDateTime.ofInstant(Instant.now(), ZoneId.SystemDefault());
로그인 후 복사

Java의 LocalDateTime 메서드

Java LocalDateTime 클래스에는 다양한 메소드가 있습니다.

  • 문자열 형식(DateTimeFormatter 포맷터): 지정된 포맷터를 사용하여 날짜와 시간의 형식을 지정하는 데 사용됩니다.
  • LocalDateTime minusDays(long days): 특정 날짜를 사용하여 날짜와 시간의 형식을 지정하는 데 사용되며 해당 날짜에서 해당 날짜를 뺍니다.
  • LocalDateTime plusDays(long days): 현재 날짜에 특정 일 수를 추가한 후 각각 출력을 인쇄하는 데 사용됩니다.
  • int get(TemporalField field): 날짜 및 시간 값을 int인 정수 형식으로 가져오는 데 사용됩니다.
  • static LocalDateTime now(): 이 메서드를 사용하여 기본 시간대 역할을 하는 현재 시간대에서 기본 날짜와 시간을 검색합니다.

Java LocalDateTime의 예

다음은 언급된 예입니다.

예시 #1

첫 번째 코딩 예제에서는 특정 양의 코드 형식을 지정하기 위해 Java에서 사용되는 format()을 살펴보겠습니다.

코드:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample
{
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
System.out.println("Before doing Formatting: " + now);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = now.format(format);
System.out.println("After doing Formatting: " + fDTime);
}
}
로그인 후 복사

출력:

샘플 출력과 같이 포맷 전후의 날짜와 시간이 표시된 것을 확인할 수 있습니다.

자바 LocalDateTime

예시 #2

두 번째 프로그램에서는 특정 날짜에서 날짜의 특정 값을 뺀 다음 최종적으로 특정 출력을 인쇄할 때 minusDays()가 작동하는 모습을 볼 수 있습니다.

코드:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample2
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.of(2018, 2, 14, 15, 22);
LocalDateTime dt2 = dt1.minusDays(100);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}
로그인 후 복사

위 코드에서는 특정 일수를 뺍니다. 이 경우 코드에 표시된 대로 2018년 2월 14일로부터 100입니다. 아래와 같이 포맷 전과 포맷 후에 답변을 얻을 수 있습니다.

출력:

자바 LocalDateTime

예시 #3

plusDays() 함수는 minusDays() 함수와 매우 유사하지만 유일한 차이점은 특정 일수를 빼는 대신 현재 날짜에 일수를 더한다는 것입니다. 그래서 이번 코딩 예시에서는 기존 날짜와 시간에 특정 일수를 추가해보겠습니다.

코드:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample3
{
public static void main(String[] args) {
LocalDateTime dt1 = LocalDateTime.of(2018, 1, 8, 12, 34);
LocalDateTime dt2 = dt1.plusDays(60);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}
로그인 후 복사

이 샘플 코드에서는 제공된 기본 날짜에 2018년 1월 8을 날짜로 지정합니다. 그리고 추가된 일수, 즉 60일이 표시됩니다. 코드에 60일을 추가하면 날짜가 변경되어 2018년 3월 9이 된 것을 알 수 있습니다. 시간은 동일하게 유지됩니다. 날짜만 변경되어 1월 8에서 3월 9

으로 날짜가 변경되었습니다.

출력:

자바 LocalDateTime

예시 #4

이 코딩 예제에서는 날짜와 시간의 정수 값을 각각 가져오는 get()의 기능을 살펴보겠습니다. 프로그램을 제대로 설명하기 위해 코딩 예제도 살펴보겠습니다.

코드:

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LocalDateTimeExample4
{
public static void main(String[] args)
{
LocalDateTime b = LocalDateTime.of(2018, 3, 10, 14, 36);
System.out.println(b.get(ChronoField.DAY_OF_WEEK));
System.out.println(b.get(ChronoField.DAY_OF_YEAR));
System.out.println(b.get(ChronoField.DAY_OF_MONTH));
System.out.println(b.get(ChronoField.HOUR_OF_DAY));
System.out.println(b.get(ChronoField.MINUTE_OF_DAY));
}
}
로그인 후 복사

샘플 코드는 요일, 연도, 월, 시, 분을 시간순으로 제공합니다. 프로그램은 ChronoField 패키지를 호출하여 올바르게 실행되고 필요에 따라 특정 출력을 생성하는지 확인합니다.

출력:

자바 LocalDateTime

Example #5

In this coding example, we will see the now() in the LocalDateTime class in Java programming language. This program will return the current date and time along with the seconds in the program.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample5
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = dt1.format(format);
System.out.println(fDTime);
}
}
로그인 후 복사

Output:

The output clearly displays the date and time of the current time zone in the format of hh-mm-ss, providing a clear representation.

자바 LocalDateTime

Conclusion

This article has seen several programs illustrating all the methods and functions inside the LocalDateTime class. Also, we know the syntax of the LocalDateTime class that is present. Beyond this, we also notice the output of several programs. In aeronautical engineering, professionals frequently utilize the LocalDateTime class to maintain and monitor an aircraft’s current date and time, observing any changes in time zones and their impact on the watch time.

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

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