0

I need to get the exact difference in terms of days and months and years between two joda date time. I am using the code below:

DateTime jodaStartTime=new DateTime(startTime);
DateTime jodaEndTime=new DateTime(endTime);

Period period = new Period(jodaStartTime, jodaEndTime);

System.out.print(period.getYears() + " years, ");
System.out.print(period.getMonths() + " months, ");

However, I need to get exact years for example instead of 2 years, I shoud get 2010,2011 or instead of 18 months (covering all months), I need to get the range between 1 to 12.

First, I want to change this code so I can use Java 8 time, so how to do that with Java 8 time?

mert dökümcü
  • 761
  • 3
  • 9
  • 31
Luckylukee
  • 575
  • 2
  • 9
  • 27
  • The range requirement in years makes sense to me, but not the range in months or days. If two dates were 18 months apart, you would report perhaps 1 year, but then _which_ months would you report? – Tim Biegeleisen May 05 '17 at 01:29
  • 1
    By the way, JodaTime is now in maintenance mode and you might want to use Java 8's date API instead, assuming you are using Java 8. – Tim Biegeleisen May 05 '17 at 01:30
  • It turns out that "midnight/start of day" sometimes means 1 am (daylight savings happen this way in some places), which Days.days Between doesn't handle properly. – Fady Saad May 05 '17 at 01:36
  • Hi, I edited my question, hoe to do above using java 8 and java.time modules. – Luckylukee May 05 '17 at 02:20
  • And a duplicate of the 2009 Answer: [Calculating the difference between two Java date instances](http://stackoverflow.com/q/1555262/642706). – Basil Bourque May 05 '17 at 03:16
  • On the Question linked as the duplicate, I added [an Answer specifically using the modern java.time classes](http://stackoverflow.com/a/43795903/642706). `Period.between( x , y ).getYears()` & `.getMonths()` & `.getDays()`. – Basil Bourque May 05 '17 at 03:23

1 Answers1

0

You can keep a list and fill it depending on the difference you have. I have implemented a method as for year case to give an idea:

private static LinkedList yearLister(int yearCount, int startingYear, int endYear){
    LinkedList years = new LinkedList();
    if (yearCount < 0){ yearCount = -yearCount; } 
    // yearCount can be negative. 
    // When that is the case, you won't be able to add elements to your   
    // list. This statement deals with that.

    if(startingYear > endYear){         
        for(int i = 0; i < yearCount; i++){
            years.add(endYear + i + 1 );
        }
    } else if (startingYear < endYear) {            
        for(int i = 0; i < yearCount - 1; i++){
            years.add(startingYear + i + 1);
            }
        }

    System.out.println(years);
    return years;
    }

In the main, with creating simple dates for years 2022 and 2017, for example:

Date d1,d2 = null; // need these for formatting
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");

String dateStart = "01/01/2022 05:30";
String dateStop = "02/2/2017 06:31";            
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

DateTime jodaStartTime=new DateTime(d1);
DateTime jodaEndTime=new DateTime(d2);

int startingYear = jodaStartTime.getYear();
int endYear = jodaEndTime.getYear();

When you add the following line to the main:

yearLister(yearCount, startingYear, endYear);

The output is: [2018, 2019, 2020, 2021]

mert dökümcü
  • 761
  • 3
  • 9
  • 31