如何同时使用 org.joda.time.LocalDate 和 java.time.LocalDate ;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30120488/
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
How can i use org.joda.time.LocalDate and java.time.LocalDate at the same time;
提问by Greg
I'm practicing on how to work with dates. But i got stuck when i want to use a javafx datepicker. since it only works with java.time.LocalDate i was trying something like this.
我正在练习如何处理日期。但是当我想使用 javafx 日期选择器时,我被卡住了。因为它只适用于 java.time.LocalDate 我正在尝试这样的事情。
// here i import java.time.LocalDate to get the date from the datepicker;
LocalDate dueDate = datepickerInvoiceDueDate.getValue();
int year = dueDate.getYear();
int month = dueDate.getMonthValue();
int day = dueDate.getDayOfMonth();
//but here i want to import org.joda.time.LocalDate;
LocalDate dueDatejt = new LocalDate(year, month, day);
Is there a workaround for this ? And is it possible to store Localdate (joda time) inside mysql database ?
有解决方法吗?是否可以在 mysql 数据库中存储 Localdate(joda 时间)?
I found a temporary fix but i do not think this is the right way ?
我找到了一个临时修复方法,但我认为这不是正确的方法?
java.time.LocalDate dueDate = datepickerInvoiceDueDate.getValue();
int year = dueDate.getYear();
int month = dueDate.getMonthValue();
int day = dueDate.getDayOfMonth();
//import org.joda.time.LocalDate;
LocalDate dueDatejt = new LocalDate(year, month, day);
采纳答案by Tristan
I think u mean LocalDate dueDatejt = new LocalDate(year, month, day);
in your "temporary fix" which is just correct code.
我想你的意思是LocalDate dueDatejt = new LocalDate(year, month, day);
在你的“临时修复”中,这只是正确的代码。
If u want some more definitive way of doing, I would suggest getting rid of Joda time dates, since new java 8 dates have been implemented by the creator of Joda time, you will find just the same functionnalities in a very similar API.
如果你想要一些更明确的方法,我建议去掉 Joda 时间日期,因为新的 java 8 日期已经由 Joda 时间的创建者实现,你会在非常相似的 API 中找到相同的功能。
About inserting dates into database, u can insert dates of any type, provided u convert them to java.sql.Date (or java.sql.Timestamp/java.sql.Time) before.
关于将日期插入数据库,您可以插入任何类型的日期,前提是您之前将它们转换为 java.sql.Date(或 java.sql.Timestamp/java.sql.Time)。
For a standard java.time.LocalDate, you can do : java.sql.Date sqlDate = java.sql.Date.valueOf(dueDate);
对于标准的 java.time.LocalDate,您可以执行以下操作: java.sql.Date sqlDate = java.sql.Date.valueOf(dueDate);
回答by Meno Hochschild
The author of both libraries writes on the Joda-Time-website:
这两个库的作者在Joda-Time-website上写道:
Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).
请注意,Joda-Time 被认为是一个很大程度上“已完成”的项目。没有计划进行重大改进。如果使用 Java SE 8,请迁移到 java.time (JSR-310)。
So it is clear you should use the workaround to fully qualify the classnames of Joda-Time only as temporary solution. Instead the intention and official recommendation is to migrate on Java-8-platforms.
所以很明显,您应该使用变通方法来完全限定 Joda-Time 的类名作为临时解决方案。相反,意图和官方建议是在 Java-8 平台上迁移。
Although there is usually no 1:1-migration (some effort is necessary!), the fact that Joda-Time will not be any longer in real development (abandoning many new features, just bugfixing) is a strong argument for migration. Another strong argument in favor of migration is the missing interoperability of Joda-Time with Java-8. The author had got the original plan to support low-level-interfaces like TemporalAccessor
but dropped it (probably due to lack of resources). What does this concept mean? Concrete example for interoperability (leaving aside the somehow annoying fact to write fully qualified class names):
尽管通常没有 1:1 迁移(需要付出一些努力!),但 Joda-Time 将不再用于实际开发(放弃许多新功能,只是修复错误)这一事实是迁移的有力论据。另一个支持迁移的有力论据是 Joda-Time 与 Java-8 缺少互操作性。作者本来有打算支持low-level-interface之类的,TemporalAccessor
但是放弃了(可能是资源不足)。这个概念是什么意思?互操作性的具体示例(撇开编写完全限定类名的烦人事实):
With TemporalAccessor
-Support you could write:
使用TemporalAccessor
-Support 你可以写:
org.joda.time.LocalDate joda = new org.joda.time.LocalDate(year, month, day);
java.time.LocalDate date = java.time.LocalDate.from(joda);
But actually you must write:
但实际上你必须写:
org.joda.time.LocalDate joda = new org.joda.time.LocalDate(year, month, day);
java.time.LocalDate date = java.time.LocalDate.of(joda.getYear(), joda.getMonthOfYear(), joda.getDayOfMonth());
So it is obvious that it is no good idea to support both libraries at the same time in your application.
所以很明显,在你的应用程序中同时支持这两个库并不是一个好主意。