Java代码信息

运行结果
教程手册
代码仓库
点击编辑器上方的运行按钮,会在此处显示运行结果。
以下是用户最新保存的代码
test test 发布于:2020-08-15 21:24 API random (摇塞子游戏) 发布于:2020-08-13 15:46 主函数-- 发布于:2020-08-10 17:17 主函数-- 发布于:2020-08-10 17:16 主函数-- 发布于:2020-08-10 17:16 定义类-学生-继承自人类 发布于:2020-08-10 17:11 自动截取日本名称 发布于:2020-08-08 12:10 Base64 发布于:2020-08-07 14:37 池塘问题,待修改 发布于:2020-08-10 14:36 自动截取日本名称 发布于:2020-08-08 11:57 API random (摇塞子游戏) 发布于:2020-08-06 15:05 Nginx log escape 发布于:2020-08-07 15:55 《算法》练习1.1.6 发布于:2020-08-04 21:29 排序... 发布于:2020-08-04 22:10 斐波那契函数 发布于:2020-08-04 15:37 《算法》练习1.1.5 发布于:2020-08-03 23:41 《算法》练习1.1.3 发布于:2020-08-03 22:13 《算法》练习1.1.2 发布于:2020-08-03 20:37 《算法》练习1.1.1 发布于:2020-08-03 19:24 jvaaaaa 发布于:2020-08-03 14:16 练习加深印象 发布于:2020-08-03 13:54 测试显目1 发布于:2020-07-31 16:20 hh-test 发布于:2020-07-30 10:28 聊天小工具。 发布于:2020-07-30 09:40 使用for循环嵌套实现99乘法表。 发布于:2020-07-29 21:22 for循环嵌套练习。 发布于:2020-07-29 19:55 加深三目运算符练习。 发布于:2020-07-29 13:50 逻辑运算符加深印象。 发布于:2020-07-29 13:38 写着玩的东西 发布于:2020-07-30 11:26 算算舍罕王给了多少粒麦子 发布于:2020-07-27 22:48 leetcode 发布于:2020-07-27 01:29 java练习输出打印1到100的奇数和偶数。 发布于:2020-07-25 15:03 test1 发布于:2020-07-24 14:16 第三届天命杯Roll图/模式 发布于:2020-07-23 20:17 用if和else循环判断是雨天还是睛天,男女的状态 发布于:2020-07-23 18:23 用switch循环判断男女还有天气 发布于:2020-07-23 18:22 用switch循环判断输入的数值是星期几。 发布于:2020-07-23 18:22 这个源码是表示商场打折判断写法 发布于:2020-07-23 18:21 这个源码是写用if判断成绩的 发布于:2020-07-23 18:19 用来正实是否是质数的写法 发布于:2020-07-23 18:17 一个登陆功能小文本 发布于:2020-07-23 18:15 用一个递归和一个for循环来表示两个数据 发布于:2020-07-23 18:13 java豪宅房租计算脚本 发布于:2020-07-23 17:49 java练习 发布于:2020-07-23 16:28 输出语句,是System.out.println的... 发布于:2020-07-23 10:27 测试三元运算符 发布于:2020-07-22 19:35 【基础】-数据结构 发布于:2020-07-23 22:40 测试数据类型之间的自动转换和强制转换 发布于:2020-07-22 10:32 java Queue 应用,先进先出用法实例, ... 发布于:2020-07-21 17:16 服务器代码 发布于:2020-07-20 10:59 [更多]
显示目录

Java 8 – Period and Duration examples

Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the difference between dates.

  1. Duration – Measures time in seconds and nanoseconds.
  2. Period – Measures time in years, months and days.

1. Duration Example

A java.time.Duration example to find out difference seconds between two LocalDateTime

DurationExample.java

package com.mkyong.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class DurationExample {

    public static void main(String[] args) {

        // Creating Durations
        System.out.println("--- Examples --- ");

        Duration oneHours = Duration.ofHours(1);
        System.out.println(oneHours.getSeconds() + " seconds");

        Duration oneHours2 = Duration.of(1, ChronoUnit.HOURS);
        System.out.println(oneHours2.getSeconds() + " seconds");

        // Test Duration.between
        System.out.println("\n--- Duration.between --- ");

        LocalDateTime oldDate = LocalDateTime.of(2016, Month.AUGUST, 31, 10, 20, 55);
        LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56);

        System.out.println(oldDate);
        System.out.println(newDate);

        //count seconds between dates
        Duration duration = Duration.between(oldDate, newDate);

        System.out.println(duration.getSeconds() + " seconds");

    }
}

Output

--- Examples --- 
3600 seconds
3600 seconds

--- Duration.between --- 
2016-08-31T10:20:55
2016-11-09T10:21:56
6048061 seconds

2. Period Example

A java.time.Period example to find out differently (years, months, days) between two LocalDates

PeriodExample.java

package com.mkyong.time;

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

public class PeriodExample {

    public static void main(String[] args) {

        System.out.println("--- Examples --- ");

        Period tenDays = Period.ofDays(10); 
        System.out.println(tenDays.getDays()); //10

        Period oneYearTwoMonthsThreeDays = Period.of(1, 2, 3);
        System.out.println(oneYearTwoMonthsThreeDays.getYears());   //1
        System.out.println(oneYearTwoMonthsThreeDays.getMonths());  //2
        System.out.println(oneYearTwoMonthsThreeDays.getDays());    //3

        System.out.println("\n--- Period.between --- ");
        LocalDate oldDate = LocalDate.of(1982, Month.AUGUST, 31);
        LocalDate newDate = LocalDate.of(2016, Month.NOVEMBER, 9);

        System.out.println(oldDate);
        System.out.println(newDate);

        // check period between dates
        Period period = Period.between(oldDate, newDate);

        System.out.print(period.getYears() + " years,");
        System.out.print(period.getMonths() + " months,");
        System.out.print(period.getDays() + " days");

    }
}

Output

--- Examples --- 
10
1
2
3

--- Period.between --- 
1982-08-31
2016-11-09
34 years,2 months,9 days

3. ChronoUnit Example

Alternatively, you can use ChronoUnit.{unit}.between to find out the difference between dates, review the following example :

ChronoUnitExample.java

package com.mkyong.time;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;

public class ChronoUnitExample {

    public static void main(String[] args) {

        LocalDateTime oldDate = LocalDateTime.of(1982, Month.AUGUST, 31, 10, 20, 55);
        LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56);

        System.out.println(oldDate);
        System.out.println(newDate);

        // count between dates
        long years = ChronoUnit.YEARS.between(oldDate, newDate);
        long months = ChronoUnit.MONTHS.between(oldDate, newDate);
        long weeks = ChronoUnit.WEEKS.between(oldDate, newDate);
        long days = ChronoUnit.DAYS.between(oldDate, newDate);
        long hours = ChronoUnit.HOURS.between(oldDate, newDate);
        long minutes = ChronoUnit.MINUTES.between(oldDate, newDate);
        long seconds = ChronoUnit.SECONDS.between(oldDate, newDate);
        long milis = ChronoUnit.MILLIS.between(oldDate, newDate);
        long nano = ChronoUnit.NANOS.between(oldDate, newDate);

        System.out.println("\n--- Total --- ");
        System.out.println(years + " years");
        System.out.println(months + " months");
        System.out.println(weeks + " weeks");
        System.out.println(days + " days");
        System.out.println(hours + " hours");
        System.out.println(minutes + " minutes");
        System.out.println(seconds + " seconds");
        System.out.println(milis + " milis");
        System.out.println(nano + " nano");

    }
}

Output

1982-08-31T10:20:55
2016-11-09T10:21:56

--- Total --- 
34 years
410 months
1784 weeks
12489 days
299736 hours
17984161 minutes
1079049661 seconds
1079049661000 milis
1079049661000000000 nano

References

  1. Oracle Tutorials – Period and Duration
  2. Duration JavaDoc
  3. Period JavaDoc
  4. ChronoUnit JavaDoc
由JSRUN为你提供的Java在线工具
        JSRUN提供的Java在线运行,Java 在线编译工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。

title

使用此草稿 删除草稿