오늘의 잔디
오늘의 공부
축제 준비도 해야하고 여러모로 바쁘다 코딩 공부 시간이
방학떄에 비해 많이 줄었다.
그래도 포기하지 않고 조금씩이라도 꾸준히 하다보면
뭔가 달라지겠지?

시간의 개념은 크게 2가지로 표현할 수 있다.
Period , Duration 은 시간의 간격(기간)을 표현하는데 사용된다.
시간의 간격은 영어로 amount of time(시간의 양)으로 불린다.
Period
두 날짜 사이의 간격을 년, 월, 일 단위로 나타낸다.
Duration
두 시간 사이의 간격을 시, 분, 초(나노초) 단위로 나타낸다.

두 날짜 사이의 간격을 년, 월, 일 단위로 나타낸다.
public class Period {
private final int years;
private final int months;
private final int days;
}
package time;
import java.time.LocalDate;
import java.time.Period;
public class PeriodMain {
public static void main(String[] args) {
//생성
Period period = Period.ofDays(10);
System.out.println("period = " + period);
//계산에 사용
LocalDate currentDate = LocalDate.of(2030, 1, 1);
LocalDate plusDate = currentDate.plus(period);
System.out.println("현재 날짜: " + currentDate);
System.out.println("더한 날짜: " + plusDate);
//기간 차이
LocalDate startDate = LocalDate.of(2023, 1, 1); LocalDate endDate = LocalDate.of(2023, 4, 2);
Period between = Period.between(startDate, endDate);
System.out.println("기간: " + between.getMonths() + "개월 " +
between.getDays() + "일");
}
}
실행 결과
period = P10D
현재 날짜: 2030-01-01
더한 날짜: 2030-01-11
기간: 3개월 1일
생성
of() : 특정 기간을 지정해서 Period 를 생성한다.of(년, 월, 일)ofDays()ofMonths()ofYears()계산에 사용
기간 차이
Period.between(startDate, endDate) 와 같이 특정 날짜의 차이를 구하면 Period 가 반환된다.두 시간 사이의 간격을 시, 분, 초(나노초) 단위로 나타낸다.
public class Duration {
private final long seconds; private final int nanos;
}
내부에서 초를 기반으로 시, 분, 초를 계산해서 사용한다.
package time;
import java.time.Duration;
import java.time.LocalTime;
public class DurationMain {
public static void main(String[] args) {
//생성
Duration duration = Duration.ofMinutes(30);
System.out.println("duration = " + duration);
LocalTime lt = LocalTime.of(1, 0);
System.out.println("기준 시간 = " + lt);
//계산에 사용
LocalTime plusTime = lt.plus(duration);
System.out.println("더한 시간 = " + plusTime);
//시간 차이
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(10, 0);
Duration between = Duration.between(start, end);
System.out.println("차이: " + between.getSeconds() + "초");
System.out.println("근무 시간: " + between.toHours() + "시간 " +
between.toMinutesPart() + "분");
}
}
실행 결과
duration = PT30M
기준 시간 = 01:00더한 시간 = 01:30
차이: 3600초
근무 시간: 1시간 0분
생성
of() : 특정 시간을 지정해서 Duration 를 생성한다.of(지정)ofSeconds()ofMinutes()ofHours()계산에 사용
시간 차이
Duration.between(start, end) 와 같이 특정 시간의 차이를 구하면 Duration 이 반환된다.