java8、jdk8判断两个日期包含多少天,多少个月
1、建立一个日期工具类:
DateUtils

2、创建工具日期相差天数方法:
between

3、实现between发放:
/**
* 比较两个时间相差的月份
* @param one
* @param two
* @return
*/
public Integer between(LocalDate one,LocalDate two) {
return Period.between(one,two).getMonths();
}

4、在main方法中测试代码:
public static void main(String[] args) {
System.out.println(between(LocalDate.of(2017,10,1), LocalDate.no()));
}

5、整个工具类囊近的方法:
package com.gwolf;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class DateUtils {
/**
* 比较两个时间相差的月份
* @param one
* @param two
* @return
*/
public static Integer between(LocalDate one,LocalDate two) {
return Period.between(one,two).getMonths();
}
public static void main(String[] args) {
System.out.println(between(LocalDate.of(2017,10,1), LocalDate.now()));
}
}

6、运行main方法查看结果:

7、更多的日期处理函数请参考其他相关文章。
package com.gwolf;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateUtils {
public static LocalDate plus(LocalDate localDate,
int between,ChronoUnit chronoUnit) {
return localDate.plus(between,chronoUnit);
}
public static void main(String[] args) {
System.out.println(plus(LocalDate.now(),2,ChronoUnit.DAYS));
哨佛
}
/**
* 格式化日期
* @param localDate
* @param pattern
* @return
*/
public static String parseDate(LocalDate localDate,String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
return localDate.format(dateTimeFormatter);
}
/**
* 比较两个时间相差的月份
* @param one
* @param two
* @return
*/
总近耍 public static Integer between(LocalDate one,LocalDate two) {
return Period.between(one,two).getMonths();
}
/**
* 字符串转化成日期
* @param strDate
* @param pattern
* @return
*/
public static LocalDate formatDate(String strDate,String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
return LocalDate.parse(strDate,dateTimeFormatter);
}
}
