我怎样才能同时使用org.joda.time.LocalDate和java.time.LocalDate; [重复]

时间:2021-07-29 16:03:57

This question already has an answer here:

这个问题在这里已有答案:

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 datepicker时我陷入困境。因为它只适用于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);

2 个解决方案

#1


I think u mean LocalDate dueDatejt = new LocalDate(year, month, day); in your "temporary fix" which is just correct code.

我想你的意思是LocalDate dueDatejt = new LocalDate(年,月,日);在你的“临时修复”中,这只是正确的代码。

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的时间日期,因为Joda时间的创建者已经实现了新的java 8日期,你会在非常相似的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);

#2


The author of both libraries writes on the Joda-Time-website:

这两个图书馆的作者在Joda-Time网站上写道:

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的互操作性缺失。作者得到了最初的计划来支持像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.getMonth(), joda.getDayOfMonth());

So it is obvious that it is no good idea to support both libraries at the same time in your application.

因此很明显,在您的应用程序中同时支持这两个库并不是一个好主意。

#1


I think u mean LocalDate dueDatejt = new LocalDate(year, month, day); in your "temporary fix" which is just correct code.

我想你的意思是LocalDate dueDatejt = new LocalDate(年,月,日);在你的“临时修复”中,这只是正确的代码。

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的时间日期,因为Joda时间的创建者已经实现了新的java 8日期,你会在非常相似的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);

#2


The author of both libraries writes on the Joda-Time-website:

这两个图书馆的作者在Joda-Time网站上写道:

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的互操作性缺失。作者得到了最初的计划来支持像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.getMonth(), joda.getDayOfMonth());

So it is obvious that it is no good idea to support both libraries at the same time in your application.

因此很明显,在您的应用程序中同时支持这两个库并不是一个好主意。