简体   繁体   中英

Finding difference between two dates using Joda in Java

I'm using Joda-Time and trying to find the difference between dates.

Is there something wrong with my code?

Here's my code :

LocalDate startDate = new LocalDate (19990-7-18);
LocalDate endDate = new LocalDate (2013-07-18);
Years Age = Years.yearsBetween(startDate, endDate);
int Age1 = Age.getYears();
String Age2 = new Integer(Age1).toString();

I'm using JOptionPane to view the result, and its telling I got 0 on (age1).

You need to add quotation marks " to your LocalDate definitions to use the constructor that takes a String parameter:

LocalDate startDate = new LocalDate("1999-07-18");
LocalDate endDate = new LocalDate("2013-07-18");

What you have written evaluates to long s, and the two dates constructed will in fact be 0 years apart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM