Debajo de la pregunta he puesto mi código.Por alguna razón, no se puede analizar las 6:00 y 17:30 a la hora local y luego ponerlo como booleano en la declaración if, si es antes de las 6:00, poner los valores en el nachtMap, si es después de las 17:30, poner también en NachtMap, de lo contrario en Daymap (básicamente todo en el bucle for). ¿Alguien que sepa qué está mal con mi código?
// 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) {
}
Respuestas
0 para la respuesta № 1Quizás deberías usar H:mm
en lugar de HH:mm
, puede analizar ambos 06:00
y 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)
);
}