Java日期时间API系列15-----Jdk8中java.time包中的新的日期时间API类,java日期计算2,年月日时分秒的加减等

  通过Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析 ,可以看出java8设计非常好,实现接口Temporal, TemporalAdjuster, ChronoLocalDate等,有非常丰富的方法。例如:LocalDateTime:的部分方法:

  Java日期时间API系列15-----Jdk8中java.time包中的新的日期时间API类,java日期计算2,年月日时分秒的加减等_第1张图片

 

包含了年月日,时分秒的加和减。Date中如果要进行加减,必须使用Calendar才可以。现在通过将Date转换为LocalDateTime,就能非常方便,线程安全的加减年月日,时分秒等信息。

	// plus two times
	
	public static Date plusYears(Date date, long amountToAdd){
		return plus(date, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static LocalDateTime plusYears(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static LocalDate plusYears(LocalDate localDate, long amountToAdd){
		return (LocalDate) plus(localDate, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static Date plusMonths(Date date, long amountToAdd){
		return plus(date, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static LocalDateTime plusMonths(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static LocalDate plusMonths(LocalDate localDate, long amountToAdd){
		return (LocalDate) plus(localDate, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static Date plusWeeks(Date date, long amountToAdd){
		return plus(date, ChronoUnit.WEEKS, amountToAdd);
	}
	
	public static LocalDateTime plusWeeks(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.WEEKS, amountToAdd);
	}
	
	public static LocalDate plusWeeks(LocalDate localDate, long amountToAdd){
		return (LocalDate) plus(localDate, ChronoUnit.WEEKS, amountToAdd);
	}	
	
	public static Date plusDays(Date date, long amountToAdd){
		return plus(date, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static LocalDateTime plusDays(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static LocalDate plusDays(LocalDate localDate, long amountToAdd){
		return (LocalDate) plus(localDate, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static Date plusHours(Date date, long amountToAdd){
		return plus(date, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static LocalDateTime plusHours(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static LocalTime plusHours(LocalTime localTime, long amountToAdd){
		return (LocalTime) plus(localTime, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static Date plusMinutes(Date date, long amountToAdd){
		return plus(date, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static LocalDateTime plusMinutes(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static LocalTime plusMinutes(LocalTime localTime, long amountToAdd){
		return (LocalTime) plus(localTime, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static Date plusSeconds(Date date, long amountToAdd){
		return plus(date, ChronoUnit.SECONDS, amountToAdd);
	}
	
	public static LocalDateTime plusSeconds(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) plus(localDateTime, ChronoUnit.SECONDS, amountToAdd);
	}
	
	public static LocalTime plusSeconds(LocalTime localTime, long amountToAdd){
		return (LocalTime) plus(localTime, ChronoUnit.SECONDS, amountToAdd);
	}	
	
	// minus two times
	
	public static Date minusYears(Date date, long amountToAdd){
		return minus(date, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static LocalDateTime minusYears(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static LocalDate minusYears(LocalDate localDate, long amountToAdd){
		return (LocalDate) minus(localDate, ChronoUnit.YEARS, amountToAdd);
	}
	
	public static Date minusMonths(Date date, long amountToAdd){
		return minus(date, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static LocalDateTime minusMonths(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static LocalDate minusMonths(LocalDate localDate, long amountToAdd){
		return (LocalDate) minus(localDate, ChronoUnit.MONTHS, amountToAdd);
	}
	
	public static Date minusWeeks(Date date, long amountToAdd){
		return minus(date, ChronoUnit.WEEKS, amountToAdd);
	}
	
	public static LocalDateTime minusWeeks(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.WEEKS, amountToAdd);
	}
	
	public static LocalDate minusWeeks(LocalDate localDate, long amountToAdd){
		return (LocalDate) minus(localDate, ChronoUnit.WEEKS, amountToAdd);
	}	
	
	public static Date minusDays(Date date, long amountToAdd){
		return minus(date, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static LocalDateTime minusDays(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static LocalDate minusDays(LocalDate localDate, long amountToAdd){
		return (LocalDate) minus(localDate, ChronoUnit.DAYS, amountToAdd);
	}
	
	public static Date minusHours(Date date, long amountToAdd){
		return minus(date, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static LocalDateTime minusHours(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static LocalTime minusHours(LocalTime localTime, long amountToAdd){
		return (LocalTime) minus(localTime, ChronoUnit.HOURS, amountToAdd);
	}
	
	public static Date minusMinutes(Date date, long amountToAdd){
		return minus(date, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static LocalDateTime minusMinutes(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static LocalTime minusMinutes(LocalTime localTime, long amountToAdd){
		return (LocalTime) minus(localTime, ChronoUnit.MINUTES, amountToAdd);
	}
	
	public static Date minusSeconds(Date date, long amountToAdd){
		return minus(date, ChronoUnit.SECONDS, amountToAdd);
	}
	
	public static LocalDateTime minusSeconds(LocalDateTime localDateTime, long amountToAdd){
		return (LocalDateTime) minus(localDateTime, ChronoUnit.SECONDS, amountToAdd);
	}
	
	public static LocalTime minusSeconds(LocalTime localTime, long amountToAdd){
		return (LocalTime) minus(localTime, ChronoUnit.SECONDS, amountToAdd);
	}

 

测试类:

@Test
	public void dateCalculatorPlusTest(){
		Date date = new Date();
		System.out.println(date);
		System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
		
		System.out.println(DateTimeCalculatorUtil.plusYears(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusMonths(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusWeeks(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusDays(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusHours(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusMinutes(date, 1));
		System.out.println(DateTimeCalculatorUtil.plusSeconds(date, 1));
	}
	
	@Test
	public void dateCalculatorMinusTest(){
		Date date = new Date();
		System.out.println(date);
		System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
		
		System.out.println(DateTimeCalculatorUtil.minusYears(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusMonths(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusWeeks(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusDays(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusHours(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusMinutes(date, 1));
		System.out.println(DateTimeCalculatorUtil.minusSeconds(date, 1));
	}
	
	@Test
	public void dateCalculatorPlusTest2(){
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);
		
		System.out.println(DateTimeCalculatorUtil.plusYears(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusMonths(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusWeeks(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusDays(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusHours(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusMinutes(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.plusSeconds(ldt, 1));
	}
	
	@Test
	public void dateCalculatorMinusTest2(){
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);
		
		System.out.println(DateTimeCalculatorUtil.minusYears(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusMonths(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusWeeks(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusDays(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusHours(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusMinutes(ldt, 1));
		System.out.println(DateTimeCalculatorUtil.minusSeconds(ldt, 1));
	}

 

输出:

Wed Feb 05 14:27:30 CST 2020
2020-02-05T14:27:30.398
Fri Feb 05 14:27:30 CST 2021
Thu Mar 05 14:27:30 CST 2020
Wed Feb 12 14:27:30 CST 2020
Thu Feb 06 14:27:30 CST 2020
Wed Feb 05 15:27:30 CST 2020
Wed Feb 05 14:28:30 CST 2020
Wed Feb 05 14:27:31 CST 2020

 

 

Wed Feb 05 14:27:43 CST 2020

2020-02-05T14:27:43.498

Tue Feb 05 14:27:43 CST 2019

Sun Jan 05 14:27:43 CST 2020

Wed Jan 29 14:27:43 CST 2020

Tue Feb 04 14:27:43 CST 2020

Wed Feb 05 13:27:43 CST 2020

Wed Feb 05 14:26:43 CST 2020

Wed Feb 05 14:27:42 CST 2020

 

 

 

2020-02-05T14:27:54.747

2021-02-05T14:27:54.747

2020-03-05T14:27:54.747

2020-02-12T14:27:54.747

2020-02-06T14:27:54.747

2020-02-05T15:27:54.747

2020-02-05T14:28:54.747

2020-02-05T14:27:55.747

 

 

2020-02-05T14:27:54.747

2021-02-05T14:27:54.747

2020-03-05T14:27:54.747

2020-02-12T14:27:54.747

2020-02-06T14:27:54.747

2020-02-05T15:27:54.747

2020-02-05T14:28:54.747

2020-02-05T14:27:55.747

 

 

源代码地址:https://github.com/xkzhangsan/xk-time

 

你可能感兴趣的:(Java日期时间API系列15-----Jdk8中java.time包中的新的日期时间API类,java日期计算2,年月日时分秒的加减等)