を見てみましょう。このソリューションではなくジョダ時間のjava.time
APIを使用していますが、あなたはそれが必要だと思うならば、それを自分で書き換えする方法を見つけ出すことができるはずです。ナノ秒の精度で
String enter = "2017-08-14 15:00:00";
String leave = "2017-08-17 12:00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime start = LocalDateTime.parse(enter, formatter);
LocalDateTime end = LocalDateTime.parse(leave, formatter);
Duration duration = Duration.between(start, end);
int duringDay = 0;
int duringNight = 0;
// take care of all full days
long days;
if ((days = duration.toHours()/24L) > 0) {
duringDay += 15 * days;
duringNight += 9 * days;
}
// take care of the remainder
for (int i = 1; i <= duration.toHours() % 24; i++) {
LocalDateTime ldt = start.plusHours(i);
if (ldt.getHour() <= 6 || ldt.getHour() > 21) {
duringNight++;
} else {
duringDay++;
}
}
System.out.println("Hours during day: " + duringDay);
System.out.println("Hours during night: " + duringNight);
それは少し複雑になる:
String enter = "2017-08-13 15:30:30";
String leave = "2017-08-17 22:00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime start = LocalDateTime.parse(enter, formatter);
LocalDateTime end = LocalDateTime.parse(leave, formatter);
Duration duration = Duration.between(LocalDateTime.of(start.toLocalDate().plusDays(1), LocalTime.of(0, 0)), LocalDateTime.of(end.toLocalDate(), LocalTime.of(0, 0)));
Duration timeDuringDay = Duration.ofDays(0);
Duration timeDuringNight = Duration.ofDays(0);
// take care of all full days
long days;
if ((days = duration.toHours()/24L) > 0) {
timeDuringDay = timeDuringDay.plusHours(15 * days);
timeDuringNight = timeDuringNight.plusHours(9 * days);
}
// take care of the first day
if (start.isBefore(LocalDateTime.of(start.toLocalDate(), LocalTime.of(6, 0)))) {
timeDuringNight = timeDuringNight.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate(), LocalTime.of(6, 0))));
timeDuringNight = timeDuringNight.plus(Duration.ofHours(3));
timeDuringDay = timeDuringDay.plusHours(15);
} else if (start.isAfter(LocalDateTime.of(start.toLocalDate(), LocalTime.of(21, 0)))) {
timeDuringNight = timeDuringNight.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate().plusDays(1), LocalTime.of(0, 0))));
} else {
timeDuringDay = timeDuringDay.plus(Duration.between(start, LocalDateTime.of(start.toLocalDate(), LocalTime.of(21, 0))));
timeDuringNight = timeDuringNight.plusHours(3);
}
// take care of the last day
if (end.isBefore(LocalDateTime.of(end.toLocalDate(), LocalTime.of(6, 0)))) {
timeDuringNight = timeDuringNight.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(0, 0)), end));
} else if (end.isAfter(LocalDateTime.of(end.toLocalDate(), LocalTime.of(21, 0)))) {
timeDuringNight = timeDuringNight.plusHours(6);
timeDuringNight = timeDuringNight.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(21, 0)), end));
timeDuringDay = timeDuringDay.plusHours(15);
} else {
timeDuringNight = timeDuringNight.plusHours(6);
timeDuringDay = timeDuringDay.plus(Duration.between(LocalDateTime.of(end.toLocalDate(), LocalTime.of(6, 0)), end));
}
System.out.println("Time during day: " + timeDuringDay);
System.out.println("Time during night: " + timeDuringNight);
これは私が探していたものです。 TNX a lootoot !!!ロジックはとてもシンプルです!!! TNXもう一度たくさん!!!!! – simenesky
OPは時間精度のみで満足されるようです。そうであれば、あなたの答えは十分です。 –