public class TestSubmitDate {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
enum TYPE{
DAY, WEEKLY, FORTNIGHTLY, MONTHLY,MONTHDOUBLE,QUARTER
}
public static void main(String[] args) throws ParseException {
final int day = 2;
final TYPE type = TYPE.QUARTER;
final int duration = 12;
Calendar startDate = Calendar.getInstance();
startDate.set(2020, 1, 15);
StringBuilder log = new StringBuilder();
Map<String, Date> dateMap = getDateMap(type, startDate, duration, day,log);
System.out.println(log.toString());
Set set = dateMap.keySet();
Iterator iterator = set.iterator();
System.out.println("提报日期为:");
while (iterator.hasNext()) {
String key = iterator.next().toString();
Date value = dateMap.get(key);
System.out.println(sdf.format(value));
}
}
public static Map<String, Date> getByDays(Calendar startDate, int duration, int day) {
final Date firstDate = startDate.getTime();
int dayOfMonth = DateUtils.getDayOfMonth(startDate.getTime());
int circle = duration * dayOfMonth;
LinkedHashMap<String, Date> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("key00",firstDate);
for (int i = 0; i < circle; i++) {
startDate.add(Calendar.DAY_OF_MONTH, 1);
linkedHashMap.put("key" + i, startDate.getTime());
}
return linkedHashMap;
}
public static Map getByWeek(Calendar startDate, int duration, int day) throws ParseException {
final Date firstDate = startDate.getTime();
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate.getTime());
endDate.add(Calendar.MONTH, duration);
Long d = ((endDate.getTime().getTime() - startDate.getTime().getTime()) / (1000 * 60 * 60 * 24));
LinkedHashMap<String, Date> weekMap = new LinkedHashMap();
for (int i = 0; i < d.intValue(); i++) {
startDate.add(Calendar.DAY_OF_MONTH, 1);
int weekNum = DateUtils.getWeekOfYear(startDate.getTime());
String datekey = "week" + weekNum;
Date mondayOfWeek = DateUtils.getMondayOfWeek(startDate.getTime());
if (!weekMap.containsKey(datekey) && !weekMap.containsValue(mondayOfWeek)) {
weekMap.put(datekey, mondayOfWeek);
}
}
Set<String> strings = weekMap.keySet();
Iterator<String> iterator = strings.iterator();
LinkedHashMap linkedHashMap = new LinkedHashMap();
linkedHashMap.put("key00",firstDate);
while (iterator.hasNext()) {
String key = iterator.next();
Date value = weekMap.get(key);
Calendar c = Calendar.getInstance();
c.setTime(value);
if (day > 1) {
c.add(Calendar.DAY_OF_MONTH, day - 1);
}
if (c.getTimeInMillis() > endDate.getTimeInMillis()) {
} else {
if (c.getTimeInMillis() > firstDate.getTime()) {
linkedHashMap.put(key, c.getTime());
}
}
}
return linkedHashMap;
}
public static Map getByWeekDouble(Calendar startDate, int duration, int day) {
final Date firstDate = startDate.getTime();
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate.getTime());
endDate.add(Calendar.MONTH, duration);
Long d = ((endDate.getTime().getTime() - startDate.getTime().getTime()) / (1000 * 60 * 60 * 24));
LinkedHashMap<String, Date> weekMap = new LinkedHashMap();
for (int i = 0; i < d.intValue(); i++) {
startDate.add(Calendar.DAY_OF_MONTH, 1);
int weekNum = DateUtils.getWeekOfYear(startDate.getTime());
String datekey = "week" + weekNum;
Date mondayOfWeek = DateUtils.getMondayOfWeek(startDate.getTime());
if (!weekMap.containsKey(datekey) && !weekMap.containsValue(mondayOfWeek)) {
weekMap.put(datekey, mondayOfWeek);
}
}
Set<String> strings = weekMap.keySet();
Iterator<String> iterator = strings.iterator();
LinkedHashMap linkedHashMap = new LinkedHashMap();
linkedHashMap.put("key00",firstDate);
int i = 0;
while (iterator.hasNext()) {
String key = iterator.next();
Date value = weekMap.get(key);
Calendar c = Calendar.getInstance();
c.setTime(value);
if (day > 1) {
c.add(Calendar.DAY_OF_MONTH, day - 1);
}
if (i % 2 == 1) {
if (c.getTimeInMillis() > endDate.getTimeInMillis()) {
} else {
if (c.getTimeInMillis() > firstDate.getTime()) {
linkedHashMap.put(key, c.getTime());
}
}
}
i++;
}
return linkedHashMap;
}
public static Map getByMonth(Calendar startDate, int duration, int day) {
final Date firstDate = startDate.getTime();
LinkedHashMap<String, Date> linkedHashMap = new LinkedHashMap();
for (Integer i = 0; i < duration; i++) {
startDate.add(Calendar.MONTH, 1);
linkedHashMap.put("key" + i, startDate.getTime());
}
LinkedHashMap<String, Date> resultLinkedHashMap = new LinkedHashMap<>();
resultLinkedHashMap.put("key00",firstDate);
Iterator iterator = linkedHashMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
Date value = linkedHashMap.get(key);
Date firstDateOfMonth = DateUtils.getFirstDateOfMonth(value);
Calendar calendar = Calendar.getInstance();
calendar.setTime(firstDateOfMonth);
calendar.add(Calendar.DAY_OF_MONTH, day - 1);
resultLinkedHashMap.put(key, calendar.getTime());
}
return resultLinkedHashMap;
}
public static Map getByMonthDouble(Calendar startDate, int duration, int day) {
final Date firstDate = startDate.getTime();
LinkedHashMap<String, Date> linkedHashMap = new LinkedHashMap();
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate.getTime());
endDate.add(Calendar.MONTH, duration);
Integer diffMonth = getDiffMonth(startDate.getTime(), endDate.getTime());
for (Integer i = 0; i < diffMonth; i++) {
startDate.add(Calendar.MONTH, 1);
if (i % 2 == 0) {
linkedHashMap.put("key" + i, DateUtils.getFirstDateOfMonth(startDate.getTime()));
}
}
LinkedHashMap<String, Date> resultLinkedHashMap = new LinkedHashMap();
resultLinkedHashMap.put("key00", firstDate);
Iterator<String> iterator = linkedHashMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Date value = linkedHashMap.get(key);
Calendar calendar = Calendar.getInstance();
calendar.setTime(value);
calendar.add(Calendar.DAY_OF_MONTH, day - 1);
resultLinkedHashMap.put(key, calendar.getTime());
}
return resultLinkedHashMap;
}
public static Integer getDiffMonth(Date startDate, Date endDate) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
return Math.abs(month + result);
}
public static Map<String, Date> getByQuarter(Calendar startDate, int duration, int day) throws ParseException {
final Date firstDate = startDate.getTime();
Calendar endDate = Calendar.getInstance();
endDate.setTime(startDate.getTime());
endDate.add(Calendar.MONTH, duration);
ArrayDeque monthQueue = new ArrayDeque();
for (int i = 0; i < duration; i++) {
String firstDateOfSeason = sdf.format(DateUtils.getFirstDateOfSeason(startDate.getTime()));
if (!monthQueue.contains(firstDateOfSeason)) {
monthQueue.offer(firstDateOfSeason);
}
startDate.add(Calendar.MONTH, 1);
}
monthQueue.removeFirst();
int keyIndex = 0;
LinkedHashMap<String, Date> resultLinkedHashMap = new LinkedHashMap();
resultLinkedHashMap.put("key00", firstDate);
Iterator iterator = monthQueue.iterator();
while (iterator.hasNext()) {
Date parse = sdf.parse(iterator.next().toString());
Calendar c = Calendar.getInstance();
c.setTime(parse);
c.add(Calendar.DATE, day-1);
resultLinkedHashMap.put("key" + keyIndex, c.getTime());
keyIndex++;
}
return resultLinkedHashMap;
}
public static Map getDateMap(TYPE type, Calendar startDate, int duration, int day,StringBuilder log) throws ParseException {
log.append("起始日:"+sdf.format(startDate.getTime()));
log.append(",持续时长(月):"+duration);
log.append(",第"+day+"天提报");
switch (type) {
case DAY:
log.append(",提报频率:每日");
return getByDays(startDate, duration, day);
case WEEKLY:
log.append(",提报频率:每周");
return getByWeek(startDate, duration, day);
case FORTNIGHTLY:
log.append(",提报频率:每两周");
return getByWeekDouble(startDate, duration, day);
case MONTHLY:
log.append(",提报频率:每月");
return getByMonth(startDate, duration, day);
case MONTHDOUBLE:
log.append(",提报频率:每两月");
return getByMonthDouble(startDate, duration, day);
case QUARTER:
log.append(",提报频率:每季度");
return getByQuarter(startDate, duration, day);
}
return null;
}