将 java.sql.Date & java.util.Date 转换为 org.joda.time.LocalDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20736449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting java.sql.Date & java.util.Date to org.joda.time.LocalDate
提问by ryvantage
I am trying to carefully and meticulously clean up some of my older (production) code. One thing I am trying to do is convert all my usages of java.util.Date
to LocalDate
and DateTime
.
我正在尝试小心谨慎地清理我的一些旧(生产)代码。我想做的一件事是将我所有的java.util.Date
toLocalDate
和用法转换为DateTime
。
However, I noticed one big obstacle tonight as I was working. I had this code:
然而,今晚我在工作时注意到了一个很大的障碍。我有这个代码:
ResultSet results = stmt.executeQuery();
Date last = results.getDate("LAST_DELIVERY_DATE");
Date next = results.getDate("NEXT_DELIVERY_DATE");
boolean received;
if (last == null && next == null) {
received = true; // order is not open
} else if (last == null) {
received = false;
} else {
received = true;
}
I converted last
and next
to:
我转换last
和next
到:
LocalDate last = new LocalDate(results.getDate("LAST_DELIVERY_DATE"));
LocalDate next = new LocalDate(results.getDate("NEXT_DELIVERY_DATE"));
and Netbeans underlined the if == null
and said:
和 Netbeans 下划线if == null
并说:
Unnecessary test for null - the expression is never null
This makes sense, because a new LocalDate
instance won't be null (no new Object()
can be).
这是有道理的,因为新LocalDate
实例不会为空(不可以new Object()
)。
BUT, in this case and in many cases throughout my program, a null
date communicates some essential information. In this case, it shows whether the order 1) is open (or not), 2) has been received (or not).
但是,在这种情况下以及在整个程序中的许多情况下,null
日期传达了一些基本信息。在这种情况下,它显示订单 1) 是否打开(或未打开),2)是否已收到(或未收到)。
So, trying to find ways around it, I figured I could instead use this code:
因此,试图找到解决方法,我想我可以改用以下代码:
LocalDate last = results.getDate("LAST_DELIVERY_DATE") == null? null : new LocalDate(results.getDate("LAST_DELIVERY_DATE"));
LocalDate next = results.getDate("NEXT_DELIVERY_DATE") == null? null : new LocalDate(results.getDate("NEXT_DELIVERY_DATE"));
But, this just seems ugly to me? Plus, it calls the "ResultSet#getDate()" function twice, which.... correct me if I'm wrong... makes two calls to the database, right?. So, now to convert my code to joda-time I am essentially doubling the time it takes to get java.sql.Date
objects from the database...
但是,这对我来说似乎很丑陋?另外,它两次调用“ResultSet#getDate()”函数,这......如果我错了,请纠正我......对数据库进行两次调用,对吧?。所以,现在将我的代码转换为 joda-time 我实际上是java.sql.Date
从数据库中获取对象所需的时间加倍......
LocalDate last = LocalDate.fromDateFields(results.getDate("LAST_DELIVERY_DATE"));
LocalDate next = LocalDate.fromDateFields(results.getDate("NEXT_DELIVERY_DATE"));
doesn't work, either, because fromDateFields
throws a NullPointerException
when it gets a null
value.
也不起作用,因为在获得值时fromDateFields
抛出 a 。NullPointerException
null
So, my question is: how do you best handle null dates when your program necessitates havingnull dates and joda-time? Am I missing something? Is there an easier way to accomplish what I'm after?
所以,我的问题是:如何才能最好处理空日期时,你的程序就必须有空日期和乔达时间?我错过了什么吗?有没有更简单的方法来完成我所追求的?
采纳答案by djechlin
Your code using the ternary operator cannot be that terse precisely because you make two trips to the database. Consider writing a dateutil
library with a method like this:
您使用三元运算符的代码不能那么简洁,因为您要访问数据库两次。考虑dateutil
用这样的方法编写一个库:
LocalDate convertToLocalDate(Date date) {
if(date == null) return null;
return new LocalDate(date);
}
IMO making this code clean up at the expense of an often used lightweight static method is a good trade.
IMO 以牺牲常用的轻量级静态方法为代价来清理此代码是一个很好的交易。
Also consider not using Java. In Javascript you could just use ||
and not have this problem, for instance. I also hear Scala is a good language that solves this with more explicit support for Nullable
types. As long as you're cleaning up old code, may as well do it right.
还要考虑不使用 Java。例如,在 Javascript 中,您可以直接使用||
而不会遇到此问题。我也听说 Scala 是一种很好的语言,它通过更明确的Nullable
类型支持解决了这个问题。只要你在清理旧代码,不妨把它做对。
回答by Runcorn
To Convert java.util.Date to org.joda.time.LocalDate
将 java.util.Date 转换为 org.joda.time.LocalDate
public static LocalDate convertUtilDateToLocalDate(Date date) {
if(date==null) return null;
DateTime dt = new DateTime(date);
return dt.toLocalDate();
}