[Java] 13. 날짜와 시간 관련 연산 메소드
[Java] 12. 날짜 다루기-1
[Java] 11. Stream 마무리-2 [Java] 11. Stream 마무리-2[Java] 10. Stream 마무리-1 [Java] 10. Stream 마무리-1 [Java] 9. 다양한 Stream의 생성 [Java] 9. 다양한 Stream의 생성 [Java] 8. Stream의 개념과 사용 방법-1 [Java] 8. Stream
sesoc.tistory.com
날짜나 시간 객체가 생성되고 나서 처리할 때 사용할 몇 가지 연산 관련 메소드들을 살펴보자
먼저, 조회 메소드의 종류이다.
메소드 명 | 설명 |
int getYear() | 년도 반환 |
Month getMonth() | 달 반환 |
int getDayOfMonth() | 일 반환 |
DayOfWeek getDayOfWeek() | 요일 반환 |
int getDayOfYear() | 1년 중 며칠인지 반환 |
int getHour() | 시간 반환 |
int getMinute() | 분 반환 |
int getSecond() | 초 반환 |
int getNano() | 나노세컨드 반환 |
메소드의 이름들이 직관적으로 알아볼 수 있도록 명명되었기 때문에 아래의 코드를 살펴보는 것 만으로도 메소드의 쓰임새를 충분히 알 수 있을 것으로 생각된다.
RetrieveDate.java
public class RetrieveDate {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Year => " + now.getYear());
System.out.println("Month => " + now.getMonth());
System.out.println("DayOfMonth => " + now.getDayOfMonth());
System.out.println("DayOfWeek => " + now.getDayOfWeek());
System.out.println("DayOfYear => " + now.getDayOfYear());
System.out.println("Hour => " + now.getHour());
System.out.println("Minute => " + now.getMinute());
System.out.println("Second => " + now.getSecond());
System.out.println("Nano second => " + now.getNano());
}
}
위 코드는 현재 시스템 날짜와 시간을 생성한 후 년, 월, 일, 요일, 시, 분, 초 등의 각 값을 조회 한 후 출력한 코드이다.
다음은 날짜 시간 데이터를 연산하는 메소드 이다. 날짜를 이용해 연산을 한다는 개념이 좀 어색하게 느껴질수도 있겠으나 예를 들어 내가 태어난 이후에 몇 일이 경과했을까 등의 흘러간 시간을 알아내는 등의 연산이 필요할 경우가 있다.
이중 특정 날짜에 Plus를 실시하는 메소드를 먼저 살펴보자
1) Plus 연산
메소드 명 | 설명 |
LocalDateTime plusYears(long years) | 인자만큼 년도 추가 |
LocalDateTime plusMonths(long months) | 인자만큼 달 추가 |
LocalDateTime plusWeeks(long weeks) | 인자만큼 주 추가 |
LocalDateTime plusDays(long days) | 인자만큼 일 추가 |
LocalDateTime plusHours(long hours) | 인자만큼 시간 추가 |
LocalDateTime plusMinutes(long minutes) | 인자만큼 분 추가 |
LocalDateTime plusSeconds(long seconds) | 인자만큼 초 추가 |
LocalDateTime plusNanos(long nanos) | 인자만큼 나노세컨드 추가 |
2) Minus 연산
메소드 명 | 설명 |
LocalDateTime minusYears(long years) | 인자만큼 년도 감소 |
LocalDateTime minusMonths(long months) | 인자만큼 달 감소 |
LocalDateTime minusDays(long days) | 인자만큼 일 감소 |
LocalDateTime minusWeeks(long weeks) | 인자만큼 주 감소 |
LocalDateTime minusHours(long hours) | 인자만큼 시간 감소 |
LocalDateTime minusMinutes(long minutes) | 인자만큼 분 감소 |
LocalDateTime minusNanos(long nanos) | 인자만큼 나노세컨드 감소 |
LocalDateTime minusSeconds(long seconds) | 인자만큼 초 감소 |
이 메소드들의 인자로 전달되는 데이터의 타입을 보면 long 형 정수 값을 전달하는 것을 확인할 수 있다.
OperateDate.java
import java.time.LocalDateTime;
public class OperateDate {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// Plus 연산
System.out.println("현재: " + now);
System.out.println();
System.out.println("10년 후: " + now.plusYears(10));
System.out.println("10개월 후: " + now.plusMonths(10));
System.out.println("10일 후: " + now.plusDays(10));
System.out.println("10시간 후: " + now.plusHours(10));
System.out.println("10분 후: " + now.plusMinutes(10));
System.out.println("10초 후 " + now.plusSeconds(10));
// Minus 연산
System.out.println();
System.out.println("10년 전: " + now.minusYears(10));
System.out.println("10개월 전: " + now.minusMonths(10));
System.out.println("10일 전: " + now.minusDays(10));
System.out.println("10시간 전: " + now.minusHours(10));
System.out.println("10분 전: " + now.minusMinutes(10));
System.out.println("10초 전 " + now.minusSeconds(10));
}
}
여기까지 우리는 자바에서 제공하는 날짜 시간관련 클래스를 생성하고 이렇게 생성된 날짜를 기준으로 이전 혹은 이후의 날짜나 시간을 알아올 수 있는 연산 메소드의 사용법을 코드와 함께 확인해 보았다.
다음에는 특정 두 날짜를 비교하는 메소드와 날짜나 시간을 특정한 목적에 맞도록 포맷하여 출력하는 방법에 대해서 알아보도록 하자.
다음 글: [Java] 14. 날짜 시간 포맷