시작하기무료로 시작하기

Formatting and parsing dates

Formatting and parsing dates are essential for working with different date representations. In this exercise, you will format a date and parse a string into a LocalDate.

All the required classes from java.time have been imported for you.

이 연습은 강의의 일부입니다

Input/Output and Streams in Java

강의 보기

연습 안내

  • Retrieve the current date.
  • Define a DateTimeFormatter with the pattern dd-MM-yyyy.
  • Format the current date using the formatter.
  • Parse a date string using the defined formatter.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

public class DateExample {
    public static void main(String[] args) {
    	// Get current date
        LocalDate date = ____; 
        System.out.println("Current date in default format: " + date); 
     
        // Define format with pattern dd-MM-yyyy
        DateTimeFormatter formatter = DateTimeFormatter.____("dd-MM-yyyy"); 
        
        // Format and print date
        System.out.println(date.____(formatter)); 
        
        // Parse a text into a string 
        LocalDate parseDate = LocalDate.____("2025-02-10");
        
        System.out.println("Parse the text into date: " + parseDate); 
    }
}
코드 편집 및 실행