Time.valueOf 方法返回错误值 [英] Time.valueOf method returning wrong value
问题描述
我使用 Time.valueOf 方法将字符串 "09:00:00" 转换为 Time 对象,如下所示:Time.valueOf (LocalTime.parse("09:00:00")).
I used the method Time.valueOf to convert the String "09:00:00" into Time object as follows: Time.valueOf (LocalTime.parse("09:00:00")).
当我调用 getTime() 来显示我得到的值时:28800000 毫秒而不是 32400000 毫秒(从计算器计算).
When I call getTime () to display the value I get: 28800000 ms instead of 32400000 ms (calculated from calculator).
我在使用 Time.value Of 时出错了吗?因为我不明白为什么我得到了错误的值.
Did I make an error when I used Time.value Of ? Because I don't understand why I get the wrong value.
谢谢.
推荐答案
坚持 java.time.LocalTime
我建议您坚持使用 java.time(现代 Java 日期和时间 API)中的 LocalTime
,不要使用 java.sql.Time
.后一个类的设计很差,确实是在已经设计得很差的 java.util.Date
类之上的一个真正的黑客.幸运的是,它也已经过时了.曾几何时,我们需要一个 Time
对象来将一天中的时间存储到数据类型 time
的数据库列中,或者将时间传输到使用该数据类型的 SQL 查询.从 JDBC 4.2 开始,情况不再如此.现在您的 JDBC 驱动程序接受一个 LocalTime
对象并将其时间值传递给数据库.
Stick to java.time.LocalTime
I recommend you stick to LocalTime
from java.time, the modern Java date and time API, and don’t use java.sql.Time
. The latter class is poorly designed, a true hack, indeed, on top of the already poorly designed java.util.Date
class. Fortunately it’s also long outdated. There was a time when we needed a Time
object for storing a time of day into a database column of datatype time
or transferring a time to an SQL query using that datatype. Since JDBC 4.2 this is no longer the case. Now your JDBC driver accepts a LocalTime
object and passes its time value on to the database.
因此,如果您有一个字符串,请按照您已经完成的方式将其解析为 LocalTime
对象:
So if you’ve got a string, parse it into a LocalTime
object the way you already did:
LocalTime time = LocalTime.parse("09:00:00");
如果不需要遍历字符串,可以使用of
工厂方法得到同样的结果,例如:
If you don’t need to go through a string, you may obtain the same result using the of
factory method, for example:
LocalTime time = LocalTime.of(9, 0);
我不知道你为什么要把它转换成毫秒,但你可以:
I don’t know why you should want to convert it to milliseconds, but you can:
int milliOfDay = time.get(ChronoField.MILLI_OF_DAY);
System.out.println(milliOfDay);
输出为:
32400000
这是您所说的预期价值.
This is the value you said you expected.
要将 LocalTime
插入您的数据库:
To insert the LocalTime
into your database:
PreparedStatement ps = yourDatabaseConnection.prepareStatement(
"insert into your_table(your_time_col) values (?)");
ps.setObject(1, time);
int rowsInserted = ps.executeUpdate();
注意使用setObject()
,而不是setTime()
.
如果您确实需要一个 Time
对象用于您现在不想升级的某些遗留 API,那么您所做的转换是正确的.
If you do need a Time
object for some legacy API that you don’t want to upgrade just now, the conversion you made is correct.
我在使用 Time.value Of 时出错了吗?因为我不明白为什么我得到了错误的值.
Did I make an error when I used Time.value Of ? Because I don't understand why I get the wrong value.
不,相反.你没有犯错.在这种情况下,您获得了正确的值.Time
糟糕而混乱的设计在欺骗你(我说过你不应该使用那个类).我不确定它是否已记录在案,但是 Time.valueOf (LocalTime.parse("09:00:00"))
为您提供了一个 Time
对象,该对象在内部包含一个点时间 1970 年 1 月 1 日 09:00 在您的 JVM 时区.从你得到的毫秒值 28 800 000 来看,这个时间似乎等于 08:00 UTC.您的时区是否在 1970 年冬天的 UTC 偏移量 +01:00?getTime()
方法返回自 1970 年 1 月 1 日 00:00 UTC 以来的毫秒数.
No, the other way around. You made no error. And in this case you got the correct value. It’s the poor and confusing design of Time
playing a trick on you (I said you shouldn’t want to use that class). I am not sure it’s documented, but Time.valueOf (LocalTime.parse("09:00:00"))
gives you a Time
object that internally holds a point in time of Jan 1, 1970 09:00 in the time zone of your JVM. From the millisecond value you got, 28 800 000, it seems that this time is equal to 08:00 UTC. Was your time zone at UTC offset +01:00 in the winter of 1970? The getTime()
method returns the number of milliseconds since Jan 1, 1970 00:00 UTC.
这篇关于Time.valueOf 方法返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!