java.util.Date
を受け取り、それから時間、分、ミリ秒を削除する関数を書く必要があります。数学のみを使用します (日付フォーマッタ、カレンダー オブジェクトなどはありません)。
private Date getJustDateFrom(Date d) {
//remove hours, minutes, and seconds, then return the date
}
このメソッドの目的は、時間なしでミリ秒値から日付を取得することです。
これが私がこれまでに持っているものです:
private Date getJustDateFrom(Date d) {
long milliseconds = d.getTime();
return new Date(milliseconds - (milliseconds%(1000*60*60)));
}
問題は、これで分と秒しか削除されないことです。時間を削除する方法がわかりません。
milliseconds - (milliseconds%(1000*60*60*23))
すると、前日の23:00に戻ります。
編集:
別の解決策は次のとおりです。
public static Date getJustDateFrom(Date d) {
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
return c.getTime();
}
このソリューションは、アプリのクライアント側とサーバー側のタイム ゾーンの違いの影響を受けますか?