Java代码信息

运行结果
教程手册
代码仓库
极速运行
交互输入
极速运行模式,不支持键盘输入语句但是拥有更高的运行速度,输出简洁明了 。 点击编辑器上方的运行按钮即刻体验吧。
以下是用户最新保存的代码
System.in.read()测试 发布于:2021-01-16 23:46 计算100万以内素数 发布于:2021-01-16 21:36 新核云-----题目2 发布于:2021-01-15 00:08 输出 id 和 level 的映射 发布于:2021-01-12 23:55 寻找链表的头节点 发布于:2021-01-12 22:07 新核云后端笔试 - NodeTraverse 发布于:2021-01-12 17:37 新核云后端笔试 - FindFirstNode 发布于:2021-01-12 17:37 九九乘法表 发布于:2021-01-11 16:01 将8进制数转换为十进制数 发布于:2021-01-11 15:56 输入三个数,按从大到小输出 发布于:2021-01-11 15:56 100以内的所有偶数的和 发布于:2021-01-11 15:55 编写一个程序判断字符串“Tom”是否在另一个字符串“I am Tom, I am from China”中出现 发布于:2021-01-11 15:51 编写一个类,类名为Rectangle(矩形),它有两个整型的变量width(宽)和height(高);有一个方法area(),没有参数,返回类型为double,功能是求矩形的面积;还有另一个方法为perimeter()没有参数,返回类型为double,功能是求矩形的周长, 发布于:2021-01-11 15:49 编写这样一个程序找出字符串“My name is Tom, I come from China.”中的大写字母,并打印输出。 发布于:2021-01-11 15:49 任意给出一个十进制整数,将十进制整数转换为二进制数 发布于:2021-01-11 15:48 定义一个一维数组,求出数组的最大值,最小值,平均值。 发布于:2021-01-11 15:47 用mkdir创建目录, 用到File类创建文件对象,list()列函数,isDirectory()判断是否目录函数。 发布于:2021-01-11 15:04 写出N的乘积方法factorial 发布于:2021-01-11 12:37 FileOutputStream与OutputStreamWriter FileInputStream与InputStreamReader append()函数,ready()函数,toString()函数 发布于:2021-01-11 14:05 首先创建文件 test.txt,并把给定的数字以二进制形式写进该文件,同时输出到控制台上。 OutputStream与FileOutputStream创建文件,用write()写入数据,for循环语句组个写入数组数据,但会产生异常。 发布于:2021-01-11 11:34 BufferedReader()总的readLine()函数,注意不需要强制类型转换读取值,用比较字符转函数equals();判断取值是否符合结束条件。 发布于:2021-01-11 09:40 BufferedReader(),用read()函数读取输入内容;一定要抛出异常,否则会出错,强制转化读取的内容为字符类型(char),否则也回归报错。遇到Q退出。 发布于:2021-01-11 09:46 n的阶乘用n!表示 发布于:2021-01-10 20:25 九九乘法表 精简 发布于:2021-01-10 15:31 解密rsa 发布于:2021-01-10 13:56 1-7的阶乘的和方法 发布于:2021-01-09 20:37 程序编写题 发布于:2021-01-09 20:15 大数据一班26杨惠恒 发布于:2021-01-09 18:58 1到7的阶乘的和 02zzg 发布于:2021-01-09 18:29 一百一类的素数精简版 发布于:2021-01-08 20:58 ds03tsh 1到7的阶乘的和 发布于:2021-01-07 22:10 java函数式编程 发布于:2021-01-07 16:21 46 赖赵强 1-100里面的素数 发布于:2021-01-05 21:56 把三个成员变量全部设置为10; 发布于:2021-01-05 21:16 求1到100里面的素数 发布于:2021-01-05 21:14 大数据1班26杨惠恒 发布于:2021-01-05 20:16 初始化时,把三个成员变量全部设置为10 发布于:2021-01-05 20:06 1到100里面的素数 发布于:2021-01-05 20:02 1到100里面的素数 发布于:2021-01-05 19:58 DSH TSH03 一百以内的素数 发布于:2021-01-05 19:36 反写的Hello world! 发布于:2021-01-05 15:29 输出 id 和 level 的映射 发布于:2021-01-04 17:12 测试延时的效果 发布于:2021-01-04 17:01 寻找链表的头节点 发布于:2021-01-04 16:57 输出二进制结果 发布于:2021-01-11 09:09 集合类型,返回值为对象时。 发布于:2020-12-29 20:42 骰子游戏步骤 发布于:2020-12-29 11:07 求面积和周长 发布于:2020-12-29 11:05 用Java实现如下的骰子游戏: 丢下两个骰子,若总值为7点,则赢,否则输。 发布于:2020-12-28 22:28 编写一个类,类名为Rectangle(矩形),它有两个整型的变量width(宽)和height(高);有一个方法area(),没有参数,返回类型为double,功能是求矩形的面积;还有另一个方法为perimeter()没有参数,返回类型为double,功能是求矩形的周长, 发布于:2020-12-28 22:26 [更多]
显示目录

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

使用此草稿 删除草稿

皮肤:

运行模式:

嵌入代码 iframe嵌入: