백준 10699 자바(현재날짜 가져오기)

정호윤·2023년 3월 3일

자바

목록 보기
8/46

오늘 날짜를 가져오는 간단한 문제이다.출력 형식만 지정해주면 된다.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // 현재 날짜 가져오기
        LocalDate currentDate = LocalDate.now();

        // 출력 형식 지정하기
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 날짜를 문자열로 변환하여 출력하기
        String formattedDate = currentDate.format(formatter);
        System.out.println(formattedDate);
    }
}

time.LocalDate에서 now() 메서드르 현재 날짜를 가져온다.
그 다음 time.format.DateTimeFormatter을 통해서 출력 형식을 지정해준다.

그 후 LocalDate 클래스의 format 메서드를 통해서 날짜를 문자열로 바꿔준다.

formatter를 콘솔에 찍어보면 Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2) 이런게 나온다.
.getCalss()로 찍어보면 class java.time.format.DateTimeFormatter 이렇게 나온다.

profile
개발자로 취직을 희망합니다.

0개의 댓글