通过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