2

org.joda.time.LocalDateTimeGroovy (または Java) を使用して、aを aに変換するにはどうすればよいjava.util.Dateですか?

import org.joda.time.*

Calendar cal = Calendar.instance
    cal.set(Calendar.DATE, 1)
    cal.set(Calendar.HOUR, 0)
    cal.set(Calendar.MINUTE, 0)
    cal.set(Calendar.SECOND, 0)
    cal.set(Calendar.MILLISECOND, 0)

Date startOfTheMonth = cal.time 

LocalDateTime localDateTime = new LocalDateTime()
    localDateTime = localDateTime.withDayOfMonth(1)
    localDateTime = localDateTime.withTime(0,0,0,0)
    localDateTime.minusMonths(6)

Date dateFromLocalDate = localDateTime.toDateTime().toDate()

println startOfTheMonth
println dateFromLocalDate

assert  startOfTheMonth.equals(dateFromLocalDate)

を使用すると、中部標準時 (GMT +6) で 6 時間オフになりますlocalDateTime.toDateTime().toDate()java.util.Date

時刻が一致LocalDateTimeするように日付を変換するにはどうすればよいですか?java.util.Date

4

2 に答える 2

4

変換中のどこかで、間違ったタイム ゾーンが使用されています。デフォルトのタイムゾーンとTimeZone.getDefault()Joda-Time のデフォルトを確認して、これをデバッグしますDateTimeZone.getDefault()

変換を行うときも、より明示的にすることができます。

localDateTime.toDateTime(yourDesiredZone).toDate()

于 2010-01-06T09:59:47.153 に答える
1

編集:

問題は、朝または午後の時間を示すCalendar.HOURの使用です。

次のいずれかを使用します。

cal.set(Calendar.HOUR_OF_DAY, 0)

また:

cal.set(Calendar.AM_PM, Calendar.AM)
cal.set(Calendar.HOUR, 0)
于 2010-01-06T04:00:03.280 に答える