集册 Java实例教程 计算两个日期和时间之间经过的时间量

计算两个日期和时间之间经过的时间量

欢马劈雪     最近更新时间:2020-01-02 10:19:05

448
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算两个日期和时间之间经过的时间量
// 来 自 N o w J a v a . c o m - 时  代  Java

import java.time.LocalDate;

import java.time.LocalTime;

import java.time.Month;

import static java.time.temporal.ChronoUnit.DAYS;

import static java.time.temporal.ChronoUnit.HOURS;

import static java.time.temporal.ChronoUnit.MINUTES;


public class Main {

  public static void main(String[] args) {

    LocalDate ld1 = LocalDate.of(2019, Month.JANUARY, 7);

    LocalDate ld2 = LocalDate.of(2019, Month.MAY, 18);

    long days = DAYS.between(ld1, ld2);

    

    LocalTime lt1 = LocalTime.of(7, 0);//from 时 代 J     a    v  a - nowjava.com

    LocalTime lt2 = LocalTime.of(9, 30);

    long hours = HOURS.between(lt1, lt2);

    long minutes = MINUTES.between(lt1, lt2);

    

    System.out.println("Using between (days): " + days);

    System.out.println("Using between (hours): " + hours);

    System.out.println("Using between (minutes): " + minutes);

    

    // Using the until() method

    long days2 = ld1.until(ld2, DAYS);

    long hours2 = lt1.until(lt2, HOURS);

    long
展开阅读全文