日付を次の形式でファイルに文字列として保存しています。
Sat Jul 21 23:31:55 EDT 2012
24 時間が経過したかどうかを確認するにはどうすればよいですか? 初心者なので簡単に教えてください(;_;)
日付を次の形式でファイルに文字列として保存しています。
Sat Jul 21 23:31:55 EDT 2012
24 時間が経過したかどうかを確認するにはどうすればよいですか? 初心者なので簡単に教えてください(;_;)
あなたはこのようなことをすることができます:
try {
// reading text...
Scanner scan = new Scanner( new FileInputStream( new File( "path to your file here..." ) ) );
String dateString = scan.nextLine();
// creating a formatter.
// to understand the format, take a look here: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
// EEE: Day name of week with 3 chars
// MMM: Month name of the year with 3 chars
// dd: day of month with 2 chars
// HH: hour of the day (0 to 23) with 2 chars
// mm: minute of the hour with 2 chars
// ss: second of the minute with 2 chars
// zzz: Timezone with 3 chars
// yyyy: year with 4 chars
DateFormat df = new SimpleDateFormat( "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US );
// parsing the date (using the format above, that matches with your date string)
Date date = df.parse( dateString );
// now!
Date now = new Date();
// gets the differente between the parsed date and the now date in milliseconds
long diffInMilliseconds = now.getTime() - date.getTime();
if ( diffInMilliseconds < 0 ) {
System.out.println( "the date that was read is in the future!" );
} else {
// calculating the difference in hours
// one hour have: 60 minutes or 3600 seconds or 3600000 milliseconds
double diffInHours = diffInMilliseconds / 3600000D;
System.out.printf( "%.2f hours have passed!", diffInHours );
}
} catch ( FileNotFoundException | ParseException exc ) {
exc.printStackTrace();
}
本当に 1 日または 24 時間のことですか? ナンセンスな夏時間のため、米国では 1 日の長さが 23 時間または 25 時間など、さまざまです。
その文字列形式は、日時のひどい表現です。解析するのは難しいです。3 文字のタイム ゾーン コードを使用しますが、このようなコードは標準化も一意でもありません。可能であれば、別の形式を選択してください。当然の選択はISO 8601です。例: 2014-07-08T04:17:01Z
.
適切なタイムゾーン名を使用してください。
Java にバンドルされている java.util.Date および .Calendar クラスは厄介なことで有名です。それらを避けてください。
代わりに、由緒あるJoda -Timeライブラリ、または Java 8 にバンドルされている (そして Joda-Time に触発された) 新しい java.time パッケージのいずれかを使用してください。
Joda-Time のコード例を次に示します。
現在の瞬間を取得します。
DateTime now = DateTime.now();
入力文字列を解析します。
String input = "Sat Jul 21 23:31:55 EDT 2012";
DateTime formatter = DateTimeFormat.forPattern( "EEE MMM dd HH:mm:ss zzz yyyy" ).with Locale( java.util.Locale.ENGLISH );
DateTime target = formatter.parseDateTime( input );
24 時間 (または翌日) を計算します。
DateTime twentyFourHoursLater = target.plusHours( 24 );
現在の瞬間が後に発生したかどうかをテストします。
boolean expired = now.isAfter( twentyFourHoursLater );
plusDays
または、24 時間ではなく翌日が必要な場合は、 plusHours
. 必要に応じて、目的のタイム ゾーンに調整します。タイム ゾーンは、曜日/日付を定義し、夏時間などの異常のルールを適用するため、非常に重要です。
DateTime targetAdjusted = target.withZone( DateTimeZone.forID( "Europe/Paris" ) );
…
DateTime aDayLater = targetAdjusted.plusDays( 1 ); // Go to next day, accounting for DST etc.
boolean expired = now.isAfter( aDayLater ); // Test if current moment happened after.
質問を完全に理解したかどうかわかりませんが、比較のために 2 つの日付がありますか、それとも 24 時間が経過したかどうかを定期的に確認しますか? 2 つの日付/時刻を比較する場合は、joda または date4j を調べることをお勧めします。joda を使用すると、2 つの日付間の間隔を使用して調べることができます。
Interval interval = new Interval(previousTime, new Instant());
前の時間はあなたが言及した時間です
java.util.Calendar
機能を持つとして情報を保存することをお勧めしますcompareTo ()
。
現在と現在の時刻を比較したい場合は、 を使用System.getCurrentTimeMillis()
して現在の時刻を取得できます。