0

タイムゾーンをとして設定したユーザーがいますAmerica/New_York。私は彼のために、彼の真夜中に開始し、24時間後(次の真夜中)に終了するイベントをスケジュールする必要があります。しかし、私はデータベースに日付を保存したいと思いますUTC

そこで、JodaDateTimeを使用して次のスニペットを作成しました。

DateTime dateTime = new DateTime(DateTimeZone.forID(user.getTimezone()));
DateTime todayMidnight = dateTime.toDateMidnight().toDateTime();
// now setting the event start and end time
event.setStartTime(todayMidnight.toDate());
event.setEndTime(todayMidnight.plusDays(1).toDate());

私のサーバーはUTCタイムゾーンで実行されていることに注意してください。

America/New_YorkUTC-5なので、開始日はそうなると思います4th Feb 2013 5:0:0が、私にとっては開始日を次のように表示します。3rd Feb 2013 23:0:0

上記のコードに何か問題がありますか?

4

1 に答える 1

1

完全に使用しないことをお勧めします。DateMidnight(ニューヨークではおそらく問題ありませんが、他のタイム ゾーンでは、夏時間の変更により真夜中が存在しない日があります。)LocalDate日付を表すために使用します。

例えば:

DateTimeZone zone = DateTimeZone.forID(user.getTimezone());
// Defaults to the current time. I'm not a fan of this - I'd pass in the
// relevant instant explicitly...
DateTime nowInZone = new DateTime(zone);
LocalDate today = nowInZone.toLocalDate();
DateTime startOfToday = today.toDateTimeAtStartOfDay(zone);
DateTime startOfTomorrow = today.plusDays(1).toDateTimeAtStartOfDay(zone);

event.setStartTime(startOfToday.toDate());
event.setEndTime(startOfTomorrow.toDate());
于 2013-02-04T07:07:17.970 に答える