Java 8 calculation of time difference method example
•
Java
package insping;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
public class Test {
public static void main(String[] args) {
LocalDate today = LocalDate.Now();
System.out.println("Today : " + today);
LocalDate birthDate = LocalDate.of(1993,Month.OCTOBER,19);
System.out.println("BirthDate : " + birthDate);
Period p = Period.between(birthDate,today);
System.out.printf("年龄 : %d 年 %d 月 %d 日",p.getYears(),p.getMonths(),p.getDays());
}
}
package insping;
import java.time.Duration;
import java.time.Instant;
public class Test {
public static void main(String[] args) {
Instant inst1 = Instant.Now();
System.out.println("Inst1 : " + inst1);
Instant inst2 = inst1.plus(Duration.ofSeconds(10));
System.out.println("Inst2 : " + inst2);
System.out.println("Difference in milliseconds : " + Duration.between(inst1,inst2).toMillis());
System.out.println("Difference in seconds : " + Duration.between(inst1,inst2).getSeconds());
}
}
package insping;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class Test {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(1993,19);
System.out.println("开始时间 : " + startDate);
LocalDate endDate = LocalDate.of(2017,Month.JUNE,16);
System.out.println("结束时间 : " + endDate);
long daysDiff = ChronoUnit.DAYS.between(startDate,endDate);
System.out.println("两天之间的差在天数 : " + daysDiff);
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
