Java로 세계 표준시 출력하기

  • 2 minutes to read

이 프로그램은 Java를 사용하여 현재의 세계 표준시를 출력하는 간단한 예제입니다.

코드: WorldStandardTime.java

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class WorldStandardTime {
    public static void main(String[] args) {
        ZonedDateTime utcNow = ZonedDateTime.now(ZoneOffset.UTC);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
        String formattedTime = utcNow.format(formatter);

        System.out.println("세계 표준시 : " + formattedTime);
    }
}
세계 표준시 : 2023.04.07 20:04:21
  1. java.time 패키지의 ZonedDateTime, ZoneOffset, DateTimeFormatter 클래스를 사용합니다.
  2. WorldStandardTime 클래스를 정의하고, main 함수를 작성합니다.
  3. ZonedDateTime.now(ZoneOffset.UTC)를 사용하여 현재 시간을 UTC로 가져옵니다.
  4. DateTimeFormatter.ofPattern() 메서드를 사용하여 날짜 및 시간 형식을 지정한 포매터를 생성합니다.
  5. utcNow.format(formatter)를 사용하여 생성된 포매터를 이용해 현재 시간을 형식화합니다.
  6. System.out.println()을 사용하여 세계 표준시를 출력합니다.
VisualAcademy Docs의 모든 콘텐츠, 이미지, 동영상의 저작권은 박용준에게 있습니다. 저작권법에 의해 보호를 받는 저작물이므로 무단 전재와 복제를 금합니다. 사이트의 콘텐츠를 복제하여 블로그, 웹사이트 등에 게시할 수 없습니다. 단, 링크와 SNS 공유, Youtube 동영상 공유는 허용합니다. www.VisualAcademy.com
박용준 강사의 모든 동영상 강의는 데브렉에서 독점으로 제공됩니다. www.devlec.com