同事让组个字符串,给一个插件用,用了一个小时拼了一下,就当练习了
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
@Log4j2
class TestGenArray {
public static void main(String args[]) {
String dateArray = genDateArray(1996, 2);
log.debug(dateArray.length());
}
private static String genDateArray(int startYear, int howManyYears) {
LocalDate today = LocalDate.now();
List<Map> yearsResult = new ArrayList();
List<String> daysList = new ArrayList();
for (int yearCount = 0; yearCount < howManyYears; yearCount++) {
Map<String, Map> yearMap = new TreeMap<>();
for (int everyYear = startYear; everyYear < startYear + howManyYears; everyYear++) {
Map<String, List> monthMap = new TreeMap<>();
log.debug(everyYear);
for (int monthsValue = (int)MONTH_OF_YEAR.range().getMinimum(); monthsValue <= MONTH_OF_YEAR.range().getMaximum(); monthsValue++) {
daysList.clear();
log.debug(monthsValue);
log.debug(today.withYear(everyYear).withMonth(monthsValue).lengthOfMonth());
for (int dayOfMonth = 1; dayOfMonth <= today.withYear(everyYear).withMonth(monthsValue).lengthOfMonth(); dayOfMonth++) {
daysList.add(String.valueOf(dayOfMonth));
}
monthMap.put(String.valueOf(monthsValue), daysList);
}
yearMap.put(String.valueOf(everyYear), monthMap);
log.debug(yearMap.keySet());
}
yearsResult.add(yearMap);
log.debug(yearsResult.size());
}
ObjectMapper mapperObj = new ObjectMapper();
String jsonStr = "";
try {
// get Employee object as a json string
jsonStr = mapperObj.writeValueAsString(yearsResult);
} catch (IOException e) {
log.error(e);
}
return jsonStr;
}
}