Home » Java » local time parser doesnt work [java]

local time parser doesnt work [java]

Posted by: admin July 12, 2018 Leave a comment

Questions:

Beneath the question I have put my code.For some reason it can’t parse 6:00 and 17:30 to localtime and then put it as boolean in the ifstatetement, if it is before 6:00 put the values in the nachtMap, if it is after 17:30 , put it as well in the NachtMap, otherwise in Daymap (its basically all in the for loop).
Any one who knows whats wron with my code?

// for each license plate
            for (int i = 1, n = licenseplateList.size(); i < n; i++) {

                //first look at whether it is considered as day or night time


                try {

                    String time1Str = begintimeList.get(i);
                String time2Str = endingtimeList.get(i);

                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
                LocalTime time1 = LocalTime.parse(time1Str, formatter);
                LocalTime time2 = LocalTime.parse(time2Str, formatter);

                // add date value: start with today
                LocalDate today = LocalDate.now();
                // lower value will be assigned to today
                LocalDateTime dateTime1 = LocalDateTime.of(today, time1);
                // upper value will be assigned same day if it is after lower value
                // otherwise (we crossed date boundary) assign it to tomorrow
                LocalDateTime dateTime2 =   time2.isAfter(time1) ?
                        LocalDateTime.of(today, time2) : LocalDateTime.of(today.plusDays(1), time2);

                Duration duration = Duration.between(dateTime1, dateTime2);
                Duration halfDuration = duration.dividedBy(2);
                LocalTime result = LocalTime.from(halfDuration.addTo(time1));

                System.out.println(result.format(formatter));

                LocalTime calc = LocalTime.parse("6:00");
                LocalTime calc2 = LocalTime.parse("17:30");
                boolean before = result.format(formatter).before(calc);
                boolean after = result.format(formatter).after(calc2);

                if (before == true) {
                    nachtMap.put(licenseplateList.get(i), kmperlineList.get(i)); 

                }
                else {
                    if(after == true) {

                        nachtMap.put(licenseplateList.get(i), kmperlineList.get(i));
                    }
                    else {
                        dagMap.put(licenseplateList.get(i), kmperlineList.get(i));
                    }
                }

            }catch (Exception e) {
            }
Answers: