我使用的是 Joda 2.5
将 Joda LocalDate 转换为 LocalDateTime 时遇到问题。
因为,我可以将 LocalDate 转换为 DateTime,时间为 StartOfDay。我想要相同的功能,但通过 LocalDateTime 对象。
我的代码是:
Suppose Date is coming from different REST service , say
Date currentDate = new Date("2015-02-05");
And now this Date is passed to some another service as shown below :
funService(currentDate);
public funService(Date currentDate)
{
LocalDate localDateObject = new LocalDate(date);
// Now I have to convert this Date to LocalDateTime
LocalDateTime localDateTime = localDateObject.toDateTimeAtStartOfDay();
//But this returns DateTime Object.
// As I don't want to store the DateTime Object because it also stores the TimeZone
}
我想要 LocalDateTime 对象,因为我将此 LocalDateTime 作为时间戳存储在 mysql 中。
请提供将 LocalDate 转换为 LocalDateTime 对象的解决方案。
例如:“2015-02-01”到“2015-02-01 00:00:01” YYYY-MM-DD 至 YYYY-MM-DD HH:MM:SS:zzz
但不是 LocalDate 到 DateTime
最佳答案
一旦你摆脱了时区异常,你可能就可以假设每天都是从午夜开始的,所以使用:
LocalDateTime localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);
不清楚为什么您希望在午夜过后 1 秒结束。如果您确实想要这样,请创建自己的常量 LocalTime
来表示它,并在上面的代码中使用它而不是 MIDNIGHT
。
另请注意,如果您确实有一个日期
,则您可能已经丢失了信息。听起来传入的数据实际上是一个字符串 - 您最好直接将其解析为 LocalDate
以避免时区可能导致问题。
关于java - 将 Joda LocalDate 或 java.util.Date 转换为当天开始时的 LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341542/