英文:
Getting the value of time from a LocalDateTime class wont compile
问题
我试图从java.time.LocalDateTime
类的Date方法中获取日期和时间值。我已经将该值存储在一个变量中,并希望将其用作SQL查询的参数,但我无法提取该值。代码无法编译。我尝试了以下代码:
LocalDateTime dt = LocalDateTime.now();
query.setParameter("test", Date.valueOf(dt)); // 这里抛出错误。我想提取存储在dt变量中的当前日期和时间(小时、分钟、秒)值
英文:
I am trying to get the date and time value from a Date method in the java.time.LocalDateTime
class. I have stored that value in a variable and I want to use it to set the parameter for an SQL query, but I am unable to extract the value. The code won't compile. I tried this:
LocalDateTime dt = LocalDateTime.now();
query.setParameter("test", Date.valueOf(dt)); //error thrown here. I want to extract the current date and time ( hours, minutes, seconds) value stored in the dt variable
答案1
得分: 1
只有Date.valueOf(LocalDate)
,没有Date.valueOf(LocalDateTime)
。
您需要直接使用LocalDate
,或者通过在LocalDateTime
上调用toLocalDate
来将其转换为LocalDate
。
请注意,java.sql.Date
实际上不用于表示时间组件,如小时、分钟、秒等。如果您需要同时表示日期和时间,那么可以使用java.sql.Timestamp
,它甚至具有您所需的方法:Timestamp.valueOf(LocalDateTime)
。
另外,请注意:仅表示时间的类是java.sql.Time
。
英文:
There is no Date.valueOf(LocalDateTime)
, there is only Date.valueOf(LocalDate)
.
You need to use LocalDate
directly or you can convert the LocalDateTime
to LocalDate
by calling toLocalDate
on it.
Notice that java.sql.Date
is actually not used for representing time-components like hours, minutes, seconds and so on. If you want both (date and time), then use java.sql.Timestamp
, which even has the method that you want: Timestamp.valueOf(LocalDateTime)
.
Just a side note: The class for time-only is java.sql.Time
.
答案2
得分: 1
你应该使用纯粹的 java.time
解决方案,与过时的 java.util.Date
API 没有关系。Date.valueOf(LocalDate)
只是为了使遗留代码与现代日期时间 API(即 java.time
)兼容而存在。
由于你没有明确提到你的问题涉及的具体时间,我可以展示不同的可能性。
请注意,LocalDateTime
不适合捕获时间戳,因为它不包含关于时区或偏移的信息,但如果需要,你可以添加这些信息:
public static void main(String[] args) {
// 获取没有时区或偏移信息的“现在”
LocalDateTime now = LocalDateTime.now();
// 提取日期部分
LocalDate today = now.toLocalDate();
// 提取时间部分
LocalTime timeOfNow = now.toLocalTime();
// 然后打印单独的部分(日期和时间)
System.out.println("今天是 " + today + ",现在是 " + timeOfNow + " 那一天");
// 打印完整的时间戳
System.out.println("完整的日期和时间现在是 " + now);
// 或者打印纪元毫秒数(需要时区或偏移)
System.out.println("此刻的时间是 "
+ now.toInstant(ZoneOffset.UTC).toEpochMilli() + " 在 UTC 中");
}
这个代码的输出是(查看执行时间的输出;-))
今天是 2020-08-13,现在是 15:04:46.728 那一天
完整的日期和时间现在是 2020-08-13T15:04:46.728
此刻的时间是 1597331086728 在 UTC 中
英文:
You should be using a plain java.time
solution without any relation to the outdated API around java.util.Date
. Date.valueOf(LocalDate)
just exists to make legacy code compatible with the modern datetime API (that is java.time
).
Since you haven't clarified which time your question is about, I can just show different possibilities.
Please note that LocalDateTime
is not suitable for catching timestamps because it does not contain information about your time zone or an offset, but you can add one if necessary:
public static void main(String[] args) {
// get "now" without any time zone or offset information
LocalDateTime now = LocalDateTime.now();
// extract the date part
LocalDate today = now.toLocalDate();
// extract the time-of-day part
LocalTime timeOfNow = now.toLocalTime();
// then print the single parts (date and time of day)
System.out.println("Today is " + today + " and now is " + timeOfNow + " that day");
// print the full timestamp
System.out.println("Full date and time are now " + now);
// or print the epoch milliseconds (A ZONE OR AN OFFSET IS NEEDED THEN)
System.out.println("Moment in time of now is "
+ now.toInstant(ZoneOffset.UTC).toEpochMilli() + " in UTC");
}
The output of this is (see the output for execution time )
Today is 2020-08-13 and now is 15:04:46.728 that day
Full date and time are now 2020-08-13T15:04:46.728
Moment in time of now is 1597331086728 in UTC
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论