2013-03-21 63 views
1

我想将Joda LocalTime转换为java.util.Date,并且不是 LocalDate。如果这有帮助,我已经有了一个LocalTime对象。显然,LocalTime中没有“日期”部分。所以我无法直接将LocalTime转换为Date。有没有简单的方法来做到这一点?如何将Joda时间LocalTime转换为java.util.Date?

步骤 -

LocalTime loct 
LocalDate locd = Todays date + loct 
Date da = locd.toDate(); 

回答

8
Date da = loct.toDateTimeToday().toDate(); 
+0

不要忘了决定你想使用哪个时区,否则在夏令时日期你可能会有很多乐趣。该表达式使用JVM默认tz。 – kan 2013-03-21 09:19:12

+0

@kan或特定的TZ,由DateTimeZone#setDefault(DateTimeZone)设置' – Ilya 2013-03-21 09:31:34

1

你试过

locd.withFields(loct).toDate(); 
0

您还可以使用this--

Date dtUtil = DateTime.now(DateTimeZone.getDefault()).toDate(); 

通用的办法是 -

Date dtUtil = DateTime.now(DateTimeZone.forID("TimeZoneString")).toDate(); 

其中“TimeZoneString”是您想要获取时间的时区ID。

DateTimeZone.getDefault()将返回系统的本地区域。

相关问题