Java

[Java] 자바 날짜, 시간 API의 형변환 방법

📝 작성 : 2020.06.29  ⏱ 수정 : 
final static LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2020, 6, 29, 21, 55, 10);
final static LocalDate LOCAL_DATE = LocalDate.of(2020, 6, 29);
final static LocalTime LOCAL_TIME = LocalTime.of(21, 55, 10);

public static void main(String[] args) {
    // LocalDateTime & String
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
    String dateTimeString = "1992-08-17T04:12:46.332342";

    String ldtToString = LOCAL_DATE_TIME.format(dateTimeFormatter);
    System.out.println("LocalDateTime to String : " + ldtToString +  ", " + ldtToString.getClass().getName());

    LocalDateTime stringToLdt = LocalDateTime.parse(dateTimeString, dateTimeFormatter);
    System.out.println("String to LocalDateTimme : " + stringToLdt +  ", " + stringToLdt.getClass().getName());

    System.out.println("-------------------------------------------------------------------");
    // LocalDate & String
    DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
    String dateString = "1992-08-17";

    String ldToString = LOCAL_DATE.format(dateFormatter);
    System.out.println("LocalDate to String : " + ldToString +  ", " + ldToString.getClass().getName());

    LocalDate stringToLd = LocalDate.parse(dateString);
    System.out.println("String to LocalDate : " + stringToLd +  ", " + stringToLd.getClass().getName());

    System.out.println("-------------------------------------------------------------------");
    // LocalTime & String
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH.mm.ss.SSS");
    String timeString = "23:02:19.235";

    String ltToString = LOCAL_TIME.format(timeFormatter);
    System.out.println("LocalTime to String : " + ltToString +  ", " + ltToString.getClass().getName());

    LocalTime stringToLt = LocalTime.parse(timeString);
    System.out.println("String to LocalTime : " + stringToLt + ", " + stringToLt.getClass().getName());

    System.out.println("-------------------------------------------------------------------");
    // LocalDateTime & java.util.Date
    java.util.Date ldtToUtilDate = java.util.Date.from(LOCAL_DATE_TIME.atZone(ZoneId.systemDefault()).toInstant());
    System.out.println("LocalDateTime to util.Date : " + ldtToUtilDate + ", " + ldtToUtilDate.getClass().getName());

    LocalDateTime utilDateToLdt = LocalDateTime.ofInstant(new java.util.Date(2020, 6, 29, 22, 00 , 16).toInstant(), ZoneId.systemDefault());
    System.out.println("util.Date to LocalDateTime : " + utilDateToLdt + ", " + utilDateToLdt.getClass().getName());

    System.out.println("-------------------------------------------------------------------");
    // LocalDateTIme & java.sql.Timestamp
    Timestamp ldtToTimestamp = Timestamp.valueOf(LOCAL_DATE_TIME);
    System.out.println("LocalDateTime to Timestamp : " + ldtToTimestamp + ", " + ldtToTimestamp.getClass().getName());

    LocalDateTime timestampToLdt = new Timestamp(2020, 6, 29, 22, 15, 24, 5312).toLocalDateTime();
    System.out.println("Timestamp to LocalDateTime : " + timestampToLdt + ", " + timestampToLdt.getClass().getName());

    System.out.println("-------------------------------------------------------------------");
    // LocalDate & java.sql.Date
    java.sql.Date ldToSqlDate = java.sql.Date.valueOf(LOCAL_DATE);
    System.out.println("LocalDate to sql.Date : " + ldToSqlDate + ", " + ldToSqlDate);

    LocalDate sqlDatetoLd = new Date(2020, 6, 29).toLocalDate();
    System.out.println("sql.Date to LocalDate : " + sqlDatetoLd + ", " + sqlDatetoLd);

    System.out.println("-------------------------------------------------------------------");
    // LocalDateTime & LocalDate
    LocalDate ldtToLd = LocalDate.from(LOCAL_DATE_TIME);
    System.out.println("LocalDateTime to LocalDate : " + ldtToLd +  ", " + ldtToLd.getClass().getName());

    LocalDateTime ldToLdt = LOCAL_DATE.atTime(1, 45, 30, 49782);
    System.out.println("LocalDate to LocalDateTime : " + ldToLdt +  ", " + ldToLdt.getClass().getName());
}

결과

LocalDateTime to String : 2020-06-29T21:55:10, java.lang.String
String to LocalDateTimme : 1992-08-17T04:12:46.332342, java.time.LocalDateTime
-------------------------------------------------------------------
LocalDate to String : 20200629, java.lang.String
String to LocalDate : 1992-08-17, java.time.LocalDate
-------------------------------------------------------------------
LocalDateTime to util.Date : Mon Jun 29 21:55:10 KST 2020, java.util.Date
util.Date to LocalDateTime : 3920-07-29T22:00:16, java.time.LocalDateTime
-------------------------------------------------------------------
LocalDateTime to Timestamp : 2020-06-29 21:55:10.0, java.sql.Timestamp
Timestamp to LocalDateTime : 3920-07-29T22:15:24.000005312, java.time.LocalDateTime
-------------------------------------------------------------------
LocalDate to sql.Date : 2020-06-29, 2020-06-29
sql.Date to LocalDate : 3920-07-29, 3920-07-29
-------------------------------------------------------------------
LocalDateTime to LocalDate : 2020-06-29, java.time.LocalDate
LocalDate to LocalDateTime : 2020-06-29T01:45:30.000049782, java.time.LocalDateTime
반응형

'Java' 카테고리의 다른 글

[Java] String = ""; vs new String("");  (0) 2020.07.01
[Java] 배열을 ArrayList로 변환  (0) 2020.06.30
[Java] LocalDateTime  (0) 2020.06.28
[Java] 배열을 복사하는 방법  (0) 2020.06.17
[Java] Wrapper class와 Auto Boxing  (0) 2020.06.16