Java日期时间API系列16—–Jdk8中java.time包中的新的日期时间API类,java日期计算3,日期中年月日时分秒的属性值修改等

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

 

 

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

// modify property

public static Date withYear(Date date, long newValue){
return with(date, ChronoField.YEAR, newValue);
}

public static LocalDateTime withYear(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.YEAR, newValue);
}

public static LocalDate withYear(LocalDate localDate, long newValue){
return (LocalDate) with(localDate, ChronoField.YEAR, newValue);
}

public static Date withMonth(Date date, long newValue){
return with(date, ChronoField.MONTH_OF_YEAR, newValue);
}

public static LocalDateTime withMonth(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.MONTH_OF_YEAR, newValue);
}

public static LocalDate withMonth(LocalDate localDate, long newValue){
return (LocalDate) with(localDate, ChronoField.MONTH_OF_YEAR, newValue);
}

public static Date withDayOfMonth(Date date, long newValue){
return with(date, ChronoField.DAY_OF_MONTH, newValue);
}

public static LocalDateTime withDayOfMonth(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.DAY_OF_MONTH, newValue);
}

public static LocalDate withDayOfMonth(LocalDate localDate, long newValue){
return (LocalDate) with(localDate, ChronoField.DAY_OF_MONTH, newValue);
}

public static Date withDayOfYear(Date date, long newValue){
return with(date, ChronoField.DAY_OF_YEAR, newValue);
}

public static LocalDateTime withDayOfYear(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.DAY_OF_YEAR, newValue);
}

public static LocalDate withDayOfYear(LocalDate localDate, long newValue){
return (LocalDate) with(localDate, ChronoField.DAY_OF_YEAR, newValue);
}

public static Date withHour(Date date, long newValue){
return with(date, ChronoField.HOUR_OF_DAY, newValue);
}

public static LocalDateTime withHour(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.HOUR_OF_DAY, newValue);
}

public static LocalTime withHour(LocalTime localTime, long newValue){
return (LocalTime) with(localTime, ChronoField.HOUR_OF_DAY, newValue);
}

public static Date withMinute(Date date, long newValue){
return with(date, ChronoField.MINUTE_OF_HOUR, newValue);
}

public static LocalDateTime withMinute(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.MINUTE_OF_HOUR, newValue);
}

public static LocalTime withMinute(LocalTime localTime, long newValue){
return (LocalTime) with(localTime, ChronoField.MINUTE_OF_HOUR, newValue);
}

public static Date withSecond(Date date, long newValue){
return with(date, ChronoField.SECOND_OF_MINUTE, newValue);
}

public static LocalDateTime withSecond(LocalDateTime localDateTime, long newValue){
return (LocalDateTime) with(localDateTime, ChronoField.SECOND_OF_MINUTE, newValue);
}

public static LocalTime withSecond(LocalTime localTime, long newValue){
return (LocalTime) with(localTime, ChronoField.SECOND_OF_MINUTE, newValue);
}

 

测试类:

@Test
public void dateCalculatorWithTest(){
Date date = new Date();
System.out.println(date);
System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
System.out.println(DateTimeCalculatorUtil.getDayOfYear(date));

System.out.println(DateTimeCalculatorUtil.withYear(date, 2021));
System.out.println(DateTimeCalculatorUtil.withMonth(date, 3));
System.out.println(DateTimeCalculatorUtil.withDayOfMonth(date, 6));
System.out.println(DateTimeCalculatorUtil.withDayOfYear(date, 37));
System.out.println(DateTimeCalculatorUtil.withHour(date, 17));
System.out.println(DateTimeCalculatorUtil.withMinute(date, 30));
System.out.println(DateTimeCalculatorUtil.withSecond(date, 30));
}

@Test
public void dateCalculatorWithTest2(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
System.out.println(ldt.getDayOfYear());

System.out.println(DateTimeCalculatorUtil.withYear(ldt, 2021));
System.out.println(DateTimeCalculatorUtil.withMonth(ldt, 3));
System.out.println(DateTimeCalculatorUtil.withDayOfMonth(ldt, 6));
System.out.println(DateTimeCalculatorUtil.withDayOfYear(ldt, 37));
System.out.println(DateTimeCalculatorUtil.withHour(ldt, 17));
System.out.println(DateTimeCalculatorUtil.withMinute(ldt, 30));
System.out.println(DateTimeCalculatorUtil.withSecond(ldt, 30));
}

 

输出:

Wed Feb 05 16:20:47 CST 2020
2020-02-05T16:20:47.955
36
Fri Feb 05 16:20:47 CST 2021
Thu Mar 05 16:20:47 CST 2020
Thu Feb 06 16:20:47 CST 2020
Thu Feb 06 16:20:47 CST 2020
Wed Feb 05 17:20:47 CST 2020
Wed Feb 05 16:30:47 CST 2020
Wed Feb 05 16:20:30 CST 2020

 

 

2020-02-05T16:21:03.760
36
2021-02-05T16:21:03.760
2020-03-05T16:21:03.760
2020-02-06T16:21:03.760
2020-02-06T16:21:03.760
2020-02-05T17:21:03.760
2020-02-05T16:30:03.760
2020-02-05T16:21:30.760

 

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

 

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

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

  

 

包含了年月日,时分秒的加和减。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