依赖:hutool工具类DateUtil
获取日期中的年、月、日
判断两个年份之间有多少个闰年
/**
* yyyy-MM-dd
*/
public static String formatDate(Date date) {
return formatDate(date, GlobalConstant.DateFormat.DATE_FORMAT);
}
public static void getYearMonthDay(Date date) {
String dateFormat = DateUtil.formatDate(date);
String[] dateArr = dateFormat.split("-");
int year = Integer.parseInt(dateArr[0]);
int month = Integer.parseInt(dateArr[1]);
int day = Integer.parseInt(dateArr[2]);
}
private static Date getSpecialDate(int year) {
String specialDate = (year) + "0228";
return new DateTime(specialDate);
}
private static int getLeapYears(List<Integer> years) {
int[] ret = new int[]{0};
years.forEach(y -> {
if (cn.hutool.core.date.DateUtil.isLeapYear(y))
ret[0] = ret[0] + 1;
});
return ret[0];
}
求两个日期间隔多少天
/**
* 判断是否合法的日期
*/
public static boolean isLegalDate(int year, int month, int day) {
if (year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
return false;
}
if (month == 2) {
if ((year % 4 != 0) && day > 28) {
return false;
}
if (year % 4 == 0) {
if (year % 100 == 0 && year % 400 != 0) {
if (day > 28)
return false;
} else if (day > 29) {
return false;
}
}
}
return true;
}
/**
* 判断是否合法的日期
*/
public static boolean isLegalDate(Date date) {
String dateFormat = DateUtil.formatDate(date);
String[] dateArr = dateFormat.split("-");
int year = Integer.parseInt(dateArr[0]);
int month = Integer.parseInt(dateArr[1]);
int day = Integer.parseInt(dateArr[2]);
return isLegalDate(year, month, day);
}
public static boolean isLeapYear(Date date) {
int year = getYear(date);
return (new GregorianCalendar()).isLeapYear(year);
}
public static int getYear(Date date) {
String dateFormat = DateUtil.formatDate(date);
String[] dateArr = dateFormat.split("-");
return Integer.parseInt(dateArr[0]);
}
public static int getMonth(Date date) {
String dateFormat = DateUtil.formatDate(date);
String[] dateArr = dateFormat.split("-");
return Integer.parseInt(dateArr[1]);
}
public static int getDay(Date date) {
String dateFormat = DateUtil.formatDate(date);
String[] dateArr = dateFormat.split("-");
return Integer.parseInt(dateArr[2]);
}
/**
* 獲取兩個日期間有多個2月29日
*
* @param start
* @param end
* @return
*/
public static int leapTimes(Date start, Date end) {
List<Integer> year = new ArrayList<>();
int yearStart = getYear(start);
int yearEnd = getYear(end);
int yearGap = yearEnd - yearStart;
year.add(yearStart);
if (yearGap > 0) {
for (int i = 1; i < yearGap; i++) {
int temp = yearStart + i;
year.add(temp);
}
}
int leapDays = 0;
if (cn.hutool.core.date.DateUtil.isLeapYear(yearStart) &&
start.compareTo(getSpecialDate(yearStart)) < 1 &&
end.compareTo(getSpecialDate(yearStart)) > -1) {
leapDays = leapDays + 1;
year = year.subList(1, year.size());
}
leapDays = leapDays + getLeapYears(year);
if (isLeapYear(yearEnd) && end.compareTo(getSpecialDate(yearEnd)) < 1) {
leapDays = leapDays - 1;
}
return leapDays;
}
/**
* 計算年份間有多少個閏年
* @param years
* @return
*/
private static int getLeapYears(List<Integer> years) {
int[] ret = new int[]{0};
years.forEach(y -> {
if (isLeapYear(y))
ret[0] = ret[0] + 1;
});
return ret[0];
}
/**
* 計算年份間有多少個閏年
* @param start
* @param end
* @return
*/
private static int getLeapYears(int start, int end) {
if (start < end) {
int temp = start;
start = end;
end = temp;
}
List<Integer> years = new ArrayList<>();
int yearGap = end - start;
years.add(start);
if (yearGap > 0) {
for (int i = 1; i < yearGap; i++) {
int temp = start + i;
years.add(temp);
}
}
return getLeapYears(years);
}
private static Date getSpecialDate(int year) {
String specialDate = (year) + "0228";
return new DateTime(specialDate);
}
/**
* 判斷年份是否是閏年
* @param year
* @return
*/
private static boolean isLeapYear(int year) {
return (new GregorianCalendar()).isLeapYear(year);
}