admin管理员组

文章数量:1595878

前言

项目中jdk8日期类库使用较多,本文对LocalDate日期计算和LocalDateTime日期时间计算进行阐述,通过例子测试日期计算的常用方法。

LocalDate日期计算

在应用开发中不可避免要进行日期的计算,如:计算几天后的日期,两个日期相差的天数等 。

api如下,plus开头的方法是加上指定的天数、月数、年数,minus开头的方法是减去指定的天数、月数、年数:

例子:

package com.pbteach.javase.oop.dateapi;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Collection;


/**
 * 日期时间计算测试 
 * @author 攀博课堂(www.pbteach)
 *
 */
public class DateTimeComputingTest {
	public static void test1() {
		//使用of方法创建LocalDate对象,指定年月日
		LocalDate localDate2 = LocalDate.of(2021, 1, 25);
		//日期计算
		//加上一定的天数
		System.out.println(localDate2.plusDays(2));
		//加上一定的月数
		System.out.println(localDate2.plusMonths(1));
		//加上一定的年数
		System.out.println(localDate2.plusYears(1));
		//灵活指定添加的单位及数量
		//第一个参数:数量,第二个参数:单位(年、月、日等)
		System.out.println(localDate2.plus(2, ChronoUnit.DAYS));//加上两天
		//减去一定的天数
		System.out.println(localDate2.minusDays(2));
		//灵活指定减去的单位及数量
		System.out.println(localDate2.minus(1, ChronoUnit.MONTHS));//减去一个月
	}

输出:

2021-01-27
2021-02-25
2022-01-25
2021-01-27
2021-01-23
2020-12-25

Period类

通过Period 可以来计算两个"日期"的间隔。Period类常见的成员方法如下所示:

// 用于计算两个"日期"间隔
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
// 获得这段时间的年数 
public int getYears()
// 获得此期间的总月数
public int getMonths()
//获得此期间的天数	
public int getDays()
//获取此期间的总月数	
public long toTotalMonths()

例子:

public static void test2() {
    // 创建两个LocalDate对象
    LocalDate localDate5 = LocalDate.of(2019, 10, 30);
    LocalDate localDate6 = LocalDate.of(2020 , 12  , 31) ;

    // 调用Period对象的between方法计算两个日期之间的间隔
    Period period = Period.between(localDate5, localDate6);

    // 调用Period的getYears方法,获得这段时间的年数,并且将结果进行输出
    System.out.println(period.getYears());

    // 调用Period的getMonths方法,获得此期间的月数
    System.out.println(period.getMonths());
    // 调用Period的toTotalMonths方法,获取此期间的总月数,此值为间隔年数*12
    System.out.println(period.toTotalMonths());

    // 调用Period的getDays方法,获得此期间的天数,此值并不是开始到截止的总天数
    System.out.println(period.getDays());
    //toEpochDay将此日期转换为大纪元日,Epoch Day count是第0天为1970-01-01 的简单递增计数。
    long days = localDate6.toEpochDay() - localDate5.toEpochDay();
    System.out.println(days);
}

输出:

1
2
14
1
428

LocalDateTime日期时间计算

LocalDateTime类也提供了日期计算的方法,与LocalDate的区别在于LocalDateTime提供了有关时、分、秒时间的计算。

下边主要测试一个时间加减时、分、秒的方法。

package com.pbteach.javase.oop.api;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;


/**
 * 日期时间计算测试 
 * @author 攀博课堂(www.pbteach)
 *
 */
public class DateTimeComputingTest {
	
	//日期时间计算
	public static void test3() {
	    //当前日期时间
		LocalDateTime localDateTime1 = LocalDateTime.of(2021, 1, 25, 18, 33, 36);
	    //加100秒
	    LocalDateTime localDateTime2 = localDateTime1.plusSeconds(100);
	    //加2小时
	    LocalDateTime localDateTime3 = localDateTime1.plusHours(2);
	    //加10天
	    LocalDateTime localDateTime4 = localDateTime1.plusDays(10);
	    System.out.println(localDateTime1);
	    System.out.println(localDateTime2);
	    System.out.println(localDateTime3);
	    System.out.println(localDateTime4);
	   
	}
	public static void main(String[] args) {
		test3();
	}

}

输出:

2021-01-25T18:33:36
2021-01-25T18:35:16
2021-01-25T20:33:36
2021-02-04T18:33:36

Duration类

java.time.Duration 与Period的区别是它可计算两个时间之间的间隔。

//日期时间计算
public static void test4() {
    // 当前日期时间
		LocalDateTime localDateTime1 = LocalDateTime.of(2021, 1, 25, 18, 33, 36);
		// 加100秒
		LocalDateTime localDateTime2 = localDateTime1.plusDays(1);
		Duration duration = Duration.between(localDateTime1, localDateTime2);
		// 获取间隔的小时数
		long hours = duration.toHours();
		System.out.println(hours);
		// 获取间隔的天数
		long days = duration.toDays();
		System.out.println(days);
		// 两个日期时间间隔的秒数
		long seconds = duration.getSeconds();
		System.out.println(seconds);
		//两个日期时间之间的总年数
		//首先将LocalDateTime转为LocalDate类型
		LocalDate localDate1 = localDateTime1.toLocalDate();
		LocalDate localDate2 = localDateTime2.toLocalDate();
		Period period = Period.between(localDate1, localDate2);
		int years = period.getYears();
		System.out.println(years);
}

输出:

24
1
86400

本文标签: 日期时间ampLocalDatePeriod