从 java.sql.Time 创建 jodatime LocalDate
我是 joda-time 的新手,我没有找到任何可以做一些简单事情的例子。
我想创建一个对象来保存从数据库表中读取的时间值(java.sql.Time
- 例如“18:30:00”
)我不关心时区,所以我认为我需要 LocalDate
。但问题是我无法基于该 Time
对象创建 LocalDate
对象。
我尝试了LocalDate.fromDateFields()
、DateTimeParser.parseInto()
、DateTimeParser.parseDateTime()
但没有成功。
编辑: 我应该使用LocalTime
。 这些工作:
java.sql.Time time = Time.valueOf("18:30:00");
LocalTime lt1 = LocalTime.fromDateFields(time);
LocalTime lt2 = new LocalTime(time);
I'm new to joda-time and I didn't find anywhere examples to do some simple things.
I want to make an object where to save a time value read from a table in a database (a java.sql.Time
- e.g. "18:30:00"
) I don't care about time zone, so I think that I need LocalDate
. But the problem is that I couldn't create a LocalDate
object based on that Time
object.
I tried with no success LocalDate.fromDateFields()
, DateTimeParser.parseInto()
, DateTimeParser.parseDateTime()
.
EDIT:
I should have used LocalTime
.
These work:
java.sql.Time time = Time.valueOf("18:30:00");
LocalTime lt1 = LocalTime.fromDateFields(time);
LocalTime lt2 = new LocalTime(time);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 根据文档,您应该能够通过向 LocalDate 传递 java.util.Date 作为唯一的构造函数参数来直接构造 LocalDate。由于 java.sql.Time 扩展了 java.util.Date,因此您应该能够
这对我有用:
另一方面,这不是一件有意义的事情,因为您总是会得到 1970 年 1 月 1 日。想象你知道自己在做什么。
According to the documentation, you should be able to construct a LocalDate directly by passing it a java.util.Date as the sole constructor argument. Since a java.sql.Time extends java.util.Date, you should be able to
This works for me:
On the other hand, it's not a meaningful thing to do, since you'll always get January 1, 1970. But I imagine you know what you're doing.