Contents

μžλ°” 8 Date

Contents

μ΄μ œκΉŒμ§€ λ‚΄ κΈ°μ–΅μœΌλ‘œλŠ” Date κ΄€λ ¨ 클래슀λ₯Ό μ•„λž˜μ²˜λŸΌ 점차 λ°”κΏ”μ¨μ˜¨κ±Έλ‘œ 기얡이 λ‚œλ‹€. java.util.Date > java.util.Calendar > org.joda.time 그런데 java 8 λ²„μ „μ—μ„œ 기쑴에 μžˆμ—ˆλ˜ λ¬Έμ œλ“€μ„ κ°œμ„ ν•΄μ„œ λ‚˜μ™”λ‹€κ³  ν•œλ‹€. (넀이버 HellowWorld ν¬μŠ€νŒ… μ°Έκ³ ) JSR-310 μ΄λΌλŠ” ν‘œμ€€λͺ…μ„Έλ‘œ.

μ§€κΈˆλΆ€ν„°λŠ” JAVA 8 μ—μ„œ μ œκ³΅ν•˜λŠ” API둜 λ‚ μ§œ 연산을 μ–΄λ–»κ²Œ ν•˜λŠ”μ§€μ— λŒ€ν•΄ μ•Œμ•„λ³΄κ³ μž ν•œλ‹€. (λ¬Όλ‘  μˆ˜λ§Žμ€ λ‚ μ§œ μ—°μ‚° 방법을이 μžˆμ§€λ§Œ 자주 μ“°μ΄λŠ” λΆ€λΆ„λ“€ μœ„μ£Όλ‘œ μ •λ¦¬ν•΄λ³΄μž.)

  • Date > String (format)
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  • String > Date (format)
LocalDateTime.parse("2017-01-01 12:30:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  • λ‚ μ§œ/μ‹œκ°„ 증감
LocalDateTime localDateTime = LocalDateTime.of(2017, 1, 1, 10, 0, 0);
localDateTime.plusDays(1);  // 일
localDateTime.plusMonths(1);  // μ›”
localDateTime.plusHours(1); // μ‹œκ°„
localDateTime.plusWeeks(1); // μ£Ό
localDateTime.minusYears(1);  // λ…„
localDateTime.minusMinutes(1);  // λΆ„

더 λ‹€μ–‘ν•œ λ‚΄μš©λ“€μ€ μ•„λž˜ URL μ—μ„œ 확인이 κ°€λŠ₯ν•˜λ‹€. https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html


Buy me a coffeeBuy me a coffee