일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Java
- 유닉스
- chatGPT's answer
- 시스템
- 자바
- spring data jpa
- 코틀린
- GPT-4's answer
- 데이터베이스
- 역학
- 소프트웨어공학
- kotlin
- 인프라
- oracle
- python
- write by chatGPT
- jpa
- android
- 파이썬
- JVM
- NIO
- 리눅스
- flet
- 고전역학
- 자바암호
- spring integration
- Database
- 웹 크롤링
- write by GPT-4
- 자바네트워크
- Today
- Total
기억을 지배하는 기록
Java 8 Date/Time API 본문
지금까지의 Date/Time 관련 API는 개발자에게 외면을 받아왔지만, Java 8에서는 "JSR 310: Data and Time API" Spec을 구현하여 획기적으로 변화했다고 한다. 새로운 Date/Time 클래스는 java.time.* 으로 패키징 되어 있다.
System의 Locale에 따른 날짜 및 시간 형식으로 표현된다. 불변성을 가지고 Thread Safe 하다.
java.time.LocalDate
package org.smarttechie; import java.time.LocalDate; import java.time.temporal.ChronoUnit; /** * This class demonstrates JAVA 8 data and time API * * @author Siva Prasad Rao Janapati * */ public class LocalDateDemonstration { /** * @param args */ public static void main(String[] args) { // Create date LocalDate localDate = LocalDate.now(); System.out.println("The local date is :: " + localDate); // Find the length of the month. That is, how many days are there for // this month. System.out.println("The number of days available for this month:: " + localDate.lengthOfMonth()); // Know the month name System.out.println("What is the month name? :: " + localDate.getMonth().name()); // add 2 days to the today's date. System.out.println(localDate.plus(2, ChronoUnit.DAYS)); // substract 2 days from today System.out.println(localDate.minus(2, ChronoUnit.DAYS)); // Convert the string to date System.out.println(LocalDate.parse("2017-04-07")); } } ---------------------------------- The local date is :: 2016-04-28 The number of days available for this month:: 30 What is the month name? :: APRIL 2016-04-30 2016-04-26 2017-04-07 |
java.time.LocalTime
package org.smarttechie; import java.time.LocalTime; import java.time.temporal.ChronoUnit; /** * This class demonstrates JAVA 8 data and time API * * @author Siva Prasad Rao Janapati * */ public class LocalTimeDemonstration { /** * @param args */ public static void main(String[] args) { // Get local time LocalTime localTime = LocalTime.now(); System.out.println(localTime); // Get the hour of the day System.out.println("The hour of the day:: " + localTime.getHour()); // add 2 hours to the time. System.out.println(localTime.plus(2, ChronoUnit.HOURS)); // add 6 minutes to the time. System.out.println(localTime.plusMinutes(6)); // substract 2 hours from current time System.out.println(localTime.minus(2, ChronoUnit.HOURS)); } } -------------------------------------- 13:33:53.602 The hour of the day:: 13 15:33:53.602 13:39:53.602 11:33:53.602 |
java.time.LocalDateTime
package org.smarttechie; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; /** * This class demonstrates JAVA 8 data and time API * * @author Siva Prasad Rao Janapati * */ public class LocalDateTimeDemonstration { /** * @param args */ public static void main(String[] args) { // Get LocalDateTime object LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); // Find the length of month. That is, how many days are there for this // month. System.out.println("The number of days available for this month:: " + localDateTime.getMonth().length(true)); // Know the month name System.out.println("What is the month name? :: " + localDateTime.getMonth().name()); // add 2 days to today's date. System.out.println(localDateTime.plus(2, ChronoUnit.DAYS)); // substract 2 days from today System.out.println(localDateTime.minus(2, ChronoUnit.DAYS)); } } ------------------------------------ 2016-04-28T13:34:58.061 The number of days available for this month:: 30 What is the month name? :: APRIL 2016-04-30T13:34:58.061 2016-04-26T13:34:58.061 |
java.time.Period
기간을 Day보다 적은 단위로 표현이 가능하다. 시, 분, 초, 밀리, 나노
package org.smarttechie; import java.time.LocalDate; import java.time.Period; import java.time.temporal.ChronoUnit; /** * This class demonstrates JAVA 8 data and time API * * @author Siva Prasad Rao Janapati * */ public class PeriodDemonstration { /** * @param args */ public static void main(String[] args) { LocalDate localDate = LocalDate.now(); Period period = Period.between(localDate, localDate.plus(2, ChronoUnit.DAYS)); System.out.println(period.getDays()); } } |
java.time.Duration
Day보다 큰 단위로 표현이 가능. 월, 년
package org.smarttechie; import java.time.Duration; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; /** * This class demonstrates JAVA 8 data and time API * * @author Siva Prasad Rao Janapati * */ public class DurationDemonstration { /** * @param args */ public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); // Get the duration between two dates Duration duration = Duration.between(localDateTime, localDateTime.plus(2, ChronoUnit.DAYS)); System.out.println(duration.toDays()); } } |
java.time.Year
윤년 여부도 알수 있는 Year 클래스
package org.smarttechie;
import java.time.Year;
/** * This class demonstrates JAVA 8 data and time API * @author Siva Prasad Rao Janapati * */ public class YearDemonstration {
/** * @param args */ public static void main(String[] args) {
//Get year Year year = Year.now(); System.out.println("Year ::" + year);
//know the year is leap year or not System.out.println("Is year[" +year+"] leap year?"+ year.isLeap()); } } |
참고 : Java 8 – New way to deal with Date and Time
'오래된글 > Java' 카테고리의 다른 글
자바을 이용한 암호학 - 1 (0) | 2018.04.07 |
---|---|
Java HotSpot VM Options (0) | 2018.04.07 |
Avoid J2EE data layer bottlenecks (0) | 2018.04.07 |
EJB 스팩별 비교표 (0) | 2018.04.07 |
Java Doc 사용법 (0) | 2018.04.07 |