1

I have 2 dates in the format below where the month is a 3 letter string

1988-Jul-21

2016-Dec-18

How do I calculate the number of years between the dates in the above format?

I was able to calculate the difference between the dates in years when the month is an integer using Joda time like below

val y = Years.yearsBetween(new LocalDate("1988-12-21"), new LocalDate("2016-1-18"))
println( y.getYears)
Output: 27
yAsH
  • 3,367
  • 8
  • 36
  • 67
  • Use a `DateFormatter` to parse the string appropriately. – Andy Turner May 13 '16 at 14:59
  • Your question is actually: how do I create a Joda LocalDate object from a string representing a date, right? And that makes it a duplicate of http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library – fvu May 13 '16 at 15:00
  • 2
    @fvu I do not believe so no. The question you linked to is a **String representation of an integer** where as this question, he actually has the month has a 3 char String. They are in fact different. – DjangoBlockchain May 13 '16 at 15:03
  • 1
    @Childishforlife Actually, the accepted answer explains the usage of DateTimeFormat, and links to the doc page of that function. There each of the many possible pattern letters and their behavior is explained, DateTimeFormat works for **many** date and time representations (), I think one generic answer pointing to DateTimeFormat is enough, no need for one covering the dozens of possibilities offered by that function. It's all very well and in great detail explained in the documentation. – fvu May 13 '16 at 15:10
  • @fvu Ahhh I read the documentation and I see what you mean. My apologies. – DjangoBlockchain May 13 '16 at 15:16

3 Answers3

3

You use a DateTimeFormat to invoke the LocalDate.parse(String, DateTimeFormatter) method. Something like,

val y = Years.yearsBetween(
        LocalDate.parse("1988-Jul-21", DateTimeFormat.forPattern("yyyy-MMM-dd")),
        LocalDate.parse("2016-Dec-18", DateTimeFormat.forPattern("yyyy-MMM-dd")))
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Here is how to proceed with pure Java 8 using a DateTimeFormatter to parse the dates:

// The formatter to use when parsing the dates with a locale 
// defined to prevent failures on non english environment
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd", Locale.US);
System.out.println(
    ChronoUnit.YEARS.between(
        LocalDate.parse("1988-Jul-21", formatter), 
        LocalDate.parse("2016-Dec-18", formatter)
    )
);

Output:

28
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

A brute force method you could use is simply converting the letter string into the correct int value. You could use an array, like this.

String[11] months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

You can then loop through and find when your current month is a match to one of the months, and take the i val + 1 (As it is zero based).

This is better, in my opinion, than having to write out 12 if statements.

for (int i = 0; i < months.length; i++) {
     //where monthStr is a String that is the 3 letter month representaton
     if (monthStr = months[i]) {
         int monthInt = i + 1;
     }

}

DjangoBlockchain
  • 534
  • 2
  • 17