ocpjp 오라클 자바 자격증
[ocpjp] 자바 LocalTime.of until
xemaker
2023. 10. 3. 16:21
package time;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class LocalTime112 {
public static void main(String[] args) {
LocalTime now=LocalTime.of(6,30);
long timeToBreakfast=0;
LocalTime office_start=LocalTime.of(7,30);
if(office_start.isAfter(now)) {
timeToBreakfast=now.until(office_start, ChronoUnit.MINUTES);
}else {
timeToBreakfast=now.until(office_start, ChronoUnit.HOURS);
}
System.out.println(timeToBreakfast);
}
}
Assume that the value of now is 6:30 in the morning.
What is the result?
출력:
60
해설:
6:30이 현재고(now) 7:30이 office_start이다.
if(office_start.isAfter(now)) 즉, office_start가 now보다 뒤냐고 하면 7:30이 6:30보다 뒤기 때문에 true이다.
timeToBreakfast=now.until(office_start, ChronoUnit.MINUTES); 에 의해 now와 office_start 시간 사이의 양을 분으로 계산하면 60이다.