本文介绍了将Joda LocalDate或java.util.Date转换为在当天开始时有时间的LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Joda 2.5

I am using Joda 2.5

在将Joda LocalDate转换为LocalDateTime时遇到问题。

Facing an issue while converting the Joda LocalDate to LocalDateTime.

As ,我可以使用Time atStartOfDay将LocalDate转换为DateTime。我想通过LocalDateTime对象获得相同的功能。

As,I am able to convert LocalDate to DateTime with Time atStartOfDay. I wanted the same feature but via LocalDateTime object.

我的代码是:



Date currentDate = new Date("2015-02-05");





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中。

I want the LocalDateTime Object as I am storing this LocalDateTime as Timestamp in the mysql.

请提供将LocalDate转换为LocalDateTime对象的解决方案。

例如:'2015-02-01'到'2015-02 -01 00:00:01'
YYYY-MM-DD到YYYY-MM-DD HH:MM:SS:zzz

Eg : '2015-02-01' to '2015-02-01 00:00:01' YYYY-MM-DD to YYYY-MM-DD HH:MM:SS:zzz

但不是LocalDate到DateTime

But not LocalDate to DateTime

推荐答案

一旦你摆脱了时区异常,你可以假设每天都在午夜开始,所以使用:

Well once you get rid of time zone anomalies, you can probably just assume that every day starts at midnight, so use:

LocalDateTime localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);

目前尚不清楚为什么你想在1秒内过去午夜结束。如果你真的想要它,创建你自己的常量 LocalTime 表示,并在上面的代码中使用它代替 MIDNIGHT

It's not clear why you wanted to end up at 1 second past midnight. If you really want that, create your own constant LocalTime representing that, and use it instead of MIDNIGHT in the above code.

另请注意,如果您实际上有一个日期,您可能已经丢失了信息。听起来传入的数据实际上是一个字符串 - 您最好直接将其解析为 LocalDate ,以避免时区可能导致问题。

Also note that if you've actually got a Date, you may already have lost information. It sounds like the incoming data is really a string - you'd be best off parsing that directly as a LocalDate to avoid time zones potentially causing problems.

这篇关于将Joda LocalDate或java.util.Date转换为在当天开始时有时间的LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:18