Под въпроса, който поставих кода си.По някаква причина не може да "анализира 6:00 и 17:30 до местно време и след това да го постави като boolean в ifstatetement, ако преди 6:00 е поставено стойностите в nachtMap, ако е след 17:30 също така и в NachtMap, иначе в Daymap (всъщност всичко е в for loop). Всеки, който знае какво е wron с моя код?
// 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) {
}
Отговори:
0 за отговор № 1Може би трябва да използвате H:mm
вместо HH:mm
, тя може да анализира и двете 06:00
и 6:00
@Test
public void test() {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm");
Assert.assertEquals(
LocalTime.parse("6:00", formatter),
LocalTime.parse("06:00", formatter)
);
}