1. (Annotation) String > LocalDateTime, LocalDate, LocalTime 

import java.time.LocalTime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;


...

@DateTimeFormat(pattern = "YYYY-MM-dd'T'HH:mm:ss")
private LocalDateTime localDateTime;

@DateTimeFormat(pattern = "YYYY-MM-dd")
private LocalDate localDate;

@DateTimeFormat(pattern = "HH:mm:ss")
private LocalTime localTime;

...

2. String > LocalDateTime, LocalDate, LocalTime 

LocalDateTime.parse(localDateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
LocalDate.parse(localDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalTime.parse(localTimeStr, DateTimeFormatter.ofPattern("HH:mm:ss"));

3. LocalDateTime, LocalDate, LocalTime > String

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

public class Main {

    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.now();
        String localDateTimeStr = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
        System.out.println(localDateTimeStr);

        LocalDate localDate = LocalDate.now();
        String localDateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(localDateStr);

        LocalTime localTime = LocalTime.now();
        String localTimeStr = localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(localTimeStr);

    }
}

 

'IT > JAVA' 카테고리의 다른 글

[JAVA] 배열 설명 및 초기화 방법  (0) 2020.07.09
[Java] Windows 10 Open-JDK 8 다운로드 및 환경변수 설정  (0) 2020.04.07
[Java] 람다(Lambda) 란?  (0) 2020.03.13
[JAVA] Java 란?  (0) 2020.03.03
[JAVA] String 메소드 정리  (0) 2019.12.18

+ Recent posts