如何在Java中将本地日期列表添加到myList
请找到以下代码,并检查我在将日期添加到myList时出错的地方
我声明列表为本地日期,现在需要转换为myModel
错误发生在// myList.addAll(employeeReportsModel.getReportFromDt());
@RequestMapping(value = "/getMusterRollDateBased", method = RequestMethod.GET)
public ModelAndView getMusterRollDateBased(EmployeeReportsModel employeeReportsModel) {
ModelAndView mv = new ModelAndView();
String tablePrefix = "at_hr_logs_";
try {
System.out.println("refort from date.."+employeeReportsModel.getReportFromDt());
System.out.println("refort to date.."+employeeReportsModel.getReportToDt());
String fromdate = employeeReportsModel.getReportFromDt();
String todate = employeeReportsModel.getReportToDt();
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(fromdate);
Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(todate);
System.out.println(date1 + "-----" + date2);
// parse the date into another format
SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
String fromdate1 = sdfDestination.format(date1);
String todate1 = sdfDestination.format(date2);
employeeReportsModel.setReportFromDt(fromdate1);
// get dates between two dates
String startString = fromdate1;
String endString = todate1;
LocalDate incrementingDate = LocalDate.parse(startString);
LocalDate endDate = LocalDate.parse(endString);
List<LocalDate> allDates = new ArrayList<>();
while (!incrementingDate.isAfter(endDate)) {
allDates.add(incrementingDate);
incrementingDate = incrementingDate.plusDays(1);
}
System.err.println(allDates);
List<EmployeeReportsModel> myList = null;
mv.addObject("allDates",allDates);
for (LocalDate date : allDates) {
//System.out.println();
System.out.println("dates is..." + date);
employeeReportsModel.setReportFromDt(date.toString());
String[] parts = date.toString().split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
System.out.println("year ....." + part1);
System.out.println("Month....." + part2);
String tableName = tablePrefix + part1 + '_' + part2;
System.out.println("hks table name is.." + tableName);
employeeReportsModel.setDynamicTableName(tableName);
myList.addAll(employeeReportsModel.getReportFromDt());
//here we get a problem to add
}
} catch (Exception e) {
e.printStackTrace();
}
mv.setViewName("/" + moduleName + "/employeeMusterRollBasedOnDateInter");
return mv;
}
答案 0 :(得分:3)
第一件事是您对一个空对象调用myList.addAll()
。您的列表应该像myList = new ArrayList()
这样正确初始化。
然后,您尝试将String类型存储在列表中,该列表需要一个EmployeeReportsModel类型的对象。
因此,您可以将myList更改为List<String>
的形式,或者更改getReportFromDt()
方法的返回类型。
我建议您重新考虑当前的代码。
当前,您基本上正在执行以下操作: 获取本地日期>使用它创建一个字符串>使用setReportFromDt存储它>调用getReportFromDt>尝试以字符串形式将LocalDate存储到EmployeeReportsModel类型的列表中