-1

How add Local date list to myList in java

Please find the below code and check where I made a mistake to add dates to myList

I declare List as Local date now need to convert to myModel

error occurs in // 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;

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
chalam
  • 29
  • 5
  • What is the problem, do you have any error message ? – Arnaud Jun 25 '18 at 09:55
  • The method addAll(Collection extends EmployeeReportsModel>) in the type List is not applicable for the arguments (String) – chalam Jun 25 '18 at 09:57
  • 1
    When you can use the modern and better `java.time` API, for example `LocalDate`, there is no reason why you should also want to use the old, outdated and troublesome classes like `Date` and `SimpleDateFormat`. The modern API offers all the functionality you need. – Ole V.V. Jun 25 '18 at 10:25
  • java.lang.ClassCastException error occur – chalam Jun 25 '18 at 10:39
  • Thanks for providing more information, @chalam. When doing so, please [edit the question](https://stackoverflow.com/posts/51020665/edit) and at the information there. In the case of an exception, as here, include the stacktrace in the question and format it as code, then it will be nice and readable for everyone. Thanks for helping us help you. – Ole V.V. Jun 25 '18 at 13:51

1 Answers1

3

First thing is that you call myList.addAll() on a null object. Your list should be initialized properly like myList = new ArrayList().

Then you are trying to store a String type in your list which expects an object of type EmployeeReportsModel.

So you either change your myList to look like List<String> or change your return type of the getReportFromDt() method.

I suggest you to rethink your current code.

Currently you are basically doing the following: get a local date > create a String out of it > store it with setReportFromDt > call getReportFromDt > try to store LocalDate in string form into a List of type EmployeeReportsModel

Andreas Brunnet
  • 722
  • 6
  • 19