4

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.

    // 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 ?

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);
Greg
  • 1,690
  • 4
  • 26
  • 52
  • 6
    You can use the fully qualified name of the class, so your temporary fix is the correct way. – Steve Chaloner May 08 '15 at 09:46
  • *"I found a temporary fix but i do not think this is the right way ?"* And why do you think that? – Tom May 08 '15 at 10:46
  • Because in a large context it would be easy to get confused. So i thought there would be a better solution but as i mention in the comment of the acceptet answer i got rid of joda time and found the answers i needed here : [mscharhag/ava-8-datetime-api](http://www.mscharhag.com/2014/02/java-8-datetime-api.html) – Greg May 08 '15 at 10:54

2 Answers2

6

The author of both libraries writes on the 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).

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.

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):

With TemporalAccessor-Support you could write:

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.

Kariem
  • 4,398
  • 3
  • 44
  • 73
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • something like: `LocalDate.parse(myJodaDate.toString)` is less typing – Lucas Roberts Oct 01 '18 at 15:01
  • 1
    @LucasRoberts Agreed, less typing using `toString()` but: relying on that method in general is not such a good idea (although in this special case, Joda has finished status so the behaviour will not change). – Meno Hochschild Oct 01 '18 at 15:50
0

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

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.

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.

For a standard java.time.LocalDate, you can do : java.sql.Date sqlDate = java.sql.Date.valueOf(dueDate);

Tristan
  • 8,733
  • 7
  • 48
  • 96
  • Thx for the answer. i got rid of joda time and found the answers i needed here : [mscharhag/ava-8-datetime-api](http://www.mscharhag.com/2014/02/java-8-datetime-api.html) – Greg May 08 '15 at 10:45