需求描述:
根据两个日期,计算两日期之前所有日期
根据两个月份,计算出两个月份之间所有月份
解决方案:
package com.ems.openflow.utils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CountDate {
private static transient int gregorianCutoverYear = 1582;
/** 闰年中每月天数 */
private static final int[] DAYS_P_MONTH_LY= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/** 非闰年中每月天数 */
private static final int[] DAYS_P_MONTH_CY= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/** 代表数组里的年、月、日 */
private static final int Y = 0, M = 1, D = 2;
/**
* 将代表日期的字符串分割为代表年月日的整形数组
* @param date
* @return
*/
public static int[] splitYMD(String date){
date = date.replace("-", "");
int[] ymd = {0, 0, 0};
ymd[Y] = Integer.parseInt(date.substring(0, 4));
ymd[M] = Integer.parseInt(date.substring(4, 6));
ymd[D] = Integer.parseInt(date.substring(6, 8));
return ymd;
}
/**
* 检查传入的参数代表的年份是否为闰年
* @param year
* @return
*/
public static boolean isLeapYear(int year) {
return year >= gregorianCutoverYear ?
((year%4 == 0) && ((year%100 != 0) || (year%400 == 0))) : (year%4 == 0);
}
/**
* 日期加1天
* @param year
* @param month
* @param day
* @return
*/
private static int[] addOneDay(int year, int month, int day){
if(isLeapYear( year )){
day++;
if( day > DAYS_P_MONTH_LY[month -1 ] ){
month++;
if(month > 12){
year++;
month = 1;
}
day = 1;
}
}else{
day++;
if( day > DAYS_P_MONTH_CY[month -1 ] ){
month++;
if(month > 12){
year++;
month = 1;
}
day = 1;
}
}
int[] ymd = {year, month, day};
return ymd;
}
/**
* 将不足两位的月份或日期补足为两位
* @param decimal
* @return
*/
public static String formatMonthDay(int decimal){
DecimalFormat df = new DecimalFormat("00");
return df.format( decimal );
}
/**
* 将不足四位的年份补足为四位
* @param decimal
* @return
*/
public static String formatYear(int decimal){
DecimalFormat df = new DecimalFormat("0000");
return df.format( decimal );
}
/**
* 计算两个日期之间相隔的天数
* @param begin
* @param end
* @return
* @throws ParseException
*/
public static long countDay(String begin,String end){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate , endDate;
long day = 0;
try {
beginDate= format.parse(begin);
endDate= format.parse(end);
day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
/**
* 以循环的方式计算日期
* @param beginDate endDate
* @param days
* @return
*/
public static List<String> getEveryday(String beginDate , String endDate){
long days = countDay(beginDate, endDate);
int[] ymd = splitYMD( beginDate );
List<String> everyDays = new ArrayList<String>();
everyDays.add(beginDate);
for(int i = 0; i < days; i++){
ymd = addOneDay(ymd[Y], ymd[M], ymd[D]);
everyDays.add(formatYear(ymd[Y])+"-"+formatMonthDay(ymd[M])+"-"+formatMonthDay(ymd[D]));
}
return everyDays;
}
/**
* 计算日期内所有月份
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getEveryMonth(String beginDate , String endDate) throws ParseException {
Date start = new SimpleDateFormat("yyyy-MM").parse(beginDate);
Date end = new SimpleDateFormat("yyyy-MM").parse(endDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
// 获取开始年份和开始月份
int startYear = calendar.get(Calendar.YEAR);
int startMonth = calendar.get(Calendar.MONTH);
// 获取结束年份和结束月份
calendar.setTime(end);
int endYear = calendar.get(Calendar.YEAR);
int endMonth = calendar.get(Calendar.MONTH);
//
List<String> list = new ArrayList<String>();
for (int i = startYear; i <= endYear; i++) {
String date = "";
if (startYear == endYear) {
for (int j = startMonth; j <= endMonth; j++) {
if (j < 9) {
date = i + "-0" + (j + 1);
} else {
date = i + "-" + (j + 1);
}
list.add(date);
}
} else {
if (i == startYear) {
for (int j = startMonth; j < 12; j++) {
if (j < 9) {
date = i + "-0" + (j + 1);
} else {
date = i + "-" + (j + 1);
}
list.add(date);
}
} else if (i == endYear) {
for (int j = 0; j <= endMonth; j++) {
if (j < 9) {
date = i + "-0" + (j + 1);
} else {
date = i + "-" + (j + 1);
}
list.add(date);
}
} else {
for (int j = 0; j < 12; j++) {
if (j < 9) {
date = i + "-0" + (j + 1);
} else {
date = i + "-" + (j + 1);
}
list.add(date);
}
}
}
}
return list;
}
/** 获取一个月的最后一天 */
public static String getLastDay(String date) throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM");
Date da = null;
da = d.parse(date);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(da);
int lastDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, lastDays);
return df.format(calendar.getTime());
}
/** 获取一个月的第一天 */
public static String getFirstDay(String date) throws ParseException {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM");
Date da = null;
da = d.parse(date);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(da);
//calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
String time = df.format(calendar.getTime());
return time;
}
public static void main(String[] args) throws ParseException {
List<String> list1 = CountDate.getEveryday("2021-07-01", "2021-09-02");
for (String result : list1) {
System.out.println(result);
}
List<String> list2 = CountDate.getEveryMonth("2021-02", "2021-09");
for (String result : list2) {
System.out.println(result);
}
}
}