解析は
は、二つの入力のためにこれを行います。ドキュメンテーションでは、フォーマットがhh:mm(hhmmFormatterインスタンスで定義されている)と一致しない場合、このメソッドはDateTimeParseExceptionをスローします。
hhmmFormatterあなたはDateTimeFormatter.ofPatternを使って自分で作成することができますが、私は完全にあなたのケースに合った標準
DateTimeFormatter.ISO_LOCAL_TIME
を使用して、単純に推薦します。詳細ここhttps://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_TIME
は、だから、単に
try {
// NOTE: you can move these into their own try/catch so you can handle bad user input separately if you wish
final LocalTime time1 = LocalTime.parse(userInput1, DateTimeForamatter.ISO_LOCAL_TIME);
final LocalTime time2 = LocalTime.parse(userInput2, DateTimeForamatter.ISO_LOCAL_TIME);
final long differenceInNanos = (time2.toNanoOfDay() - time1.toNanoOfDay());
final long differenceInSeconds = TimeUnit.NANOSECONDS.toSeconds(differenceInNanos)
}
catch(DateTimeParseException e){
// TODO code to handle bad user input
}
(TimeUnitでhttps://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.htmlのドキュメント)あなたはどこのユーザーがかもしれない同棲を処理したい場合は、あなたの質問に注意していなかった
注を行うことができます日々の時間を指定して、私はあなたにLocalTimeを示しています。これを最も一般的なZonedDateTimeのケースに簡単に変換し、非常に似たようなことをすることができます。
なぜ、_long_に違いがあるのか、_Joda_ libraryを使用するのが最も良いのですか? – Piyush