Duration和Period
This commit is contained in:
parent
c977d365d8
commit
6da1f053fb
@ -1,4 +1,20 @@
|
||||
package chapter5;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class Example24 {
|
||||
public static void main(String[] args) {
|
||||
LocalTime start = LocalTime.now();
|
||||
LocalTime end = LocalTime.of(21, 53, 0);
|
||||
|
||||
Duration duration = Duration.between(start, end);
|
||||
|
||||
System.out.println("时间间隔(纳秒)" + duration.toNanos());
|
||||
System.out.println("时间间隔(毫秒)" + duration.toMillis());
|
||||
System.out.println("时间间隔(秒)" + duration.toSeconds());
|
||||
System.out.println("时间间隔(分钟)" + duration.toMinutes());
|
||||
System.out.println("时间间隔(小时)" + duration.toHours());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,16 @@
|
||||
package chapter5;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
public class Example25 {
|
||||
public static void main(String[] args) {
|
||||
LocalDate birthday = LocalDate.of(2018, 12, 12);
|
||||
LocalDate now = LocalDate.of(2020, 11, 13);
|
||||
|
||||
Period period = Period.between(birthday, now);
|
||||
System.out.println("时间间隔(年)" + period.getYears());
|
||||
System.out.println("时间间隔(月)" + period.getMonths());
|
||||
System.out.println("时间间隔(天)" + period.getDays());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user