问题描述
使用Groovy(或Java)我如何将 org.joda.time.LocalDateTime
转换为 java.util.Date
$ $ p $ import org.joda.time。*
给我一个<$ c $ <$ $ $ $ $> c> java.util.Date 即6小时关闭我在中环标准时间(GMT +6)
日历cal =日历。实例
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)
日期startOfTheMonth = cal.time
LocalDateTime localDateTime = new LocalDateTime()
localDateTime = localDateTime.withDayOfMonth(1)
localDateTime = localDateTime.withTime(0,0,0,0)
localDateTime.minusMonths(6)
日期dateFromLocalDate = localDateTime.toDateTime()。toDate()
println startOfTheMonth
println dateFromLocalDate
assert startOfTheMonth.equals(dateFromLocalDate)
localDateTime.toDateTime()。toDate()
如何将我的 LocalDateTime
日期转换回一个 java.util.Date
这样的时间匹配?
编辑:
问题是使用,表示上午或下午的小时。
可以使用:
cal.set(Calendar.HOUR_OF_DAY, 0)
或:
cal.set(Calendar.AM_PM,Calendar.AM)
cal.set(Calendar.HOUR,0)
Using Groovy (or Java) how can I convert a org.joda.time.LocalDateTime
to 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)
using localDateTime.toDateTime().toDate()
gives me a java.util.Date
that is 6 hours off I'm in Central Standard time (GMT +6)
How do I convert my LocalDateTime
date back to a java.util.Date
such that the time matches?
EDIT:
The problem is the use of Calendar.HOUR which indicates the hour of the morning or afternoon.
Either use:
cal.set(Calendar.HOUR_OF_DAY, 0)
or:
cal.set(Calendar.AM_PM, Calendar.AM)
cal.set(Calendar.HOUR, 0)
这篇关于使用Groovy(或Java)如何将org.joda.time.LocalDateTime转换为java.util.date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!