-1

I have two dates. Wants to get amount of months between dates DOD and DDO. Best if I do it in a for loop, increasing the date of every month. I need all the months, which are between date1 and date2.

For example, when I have a date: d1 = 2013-07-28, d2 = 2013-09-02 I want to get 3 (to July, August and September).

String przekazaneDataOd = "2013-10-26" ; 
String przekazaneDataDo = "2014-03-11" ;

SimpleDateFormat dfIn = new SimpleDateFormat("yyyy-MM-dd");

Date d1 = null;
Date d2 = null;
try {
    d1=dfIn.parse(przekazaneDataOd);
    d2=dfIn.parse(przekazaneDataDo);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

GregorianCalendar DOD = new GregorianCalendar();
DOD.setTime(d1);

GregorianCalendar DDO = new GregorianCalendar();
DDO.setTime(d2);
  • Dup: http://stackoverflow.com/questions/13084651/java-method-to-find-difference-between-2-date-objects-in-years-months-and-days?rq=1 – Brian Roach Dec 21 '13 at 23:29

2 Answers2

2

Date-Time Library

A good date-time library such as:

…makes this kind of work much easier and more reliable.

Joda-Time Example

In Joda-Time 2.3, basically one line of code… Months.monthsBetween( start, stop )

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

String przekazaneDataOd = "2013-10-26" ;
String przekazaneDataDo = "2014-03-11" ;

DateTime start = new DateTime( przekazaneDataOd );
DateTime stop = new DateTime( przekazaneDataDo );

// Exclusive of the months of the dates. Just the full months *between* the dates.
Months monthsBetween = Months.monthsBetween( start, stop );
int monthsNumber = monthsBetween.getMonths();

// Inclusive of the months of the dates.
DateTimeZone timeZone_Warsaw = DateTimeZone.forID("Europe/Warsaw" );
// Get first day of the month containing start date.
DateTime outside_begin = new DateTime( przekazaneDataOd, timeZone_Warsaw ).withDayOfMonth( 1 ).withTimeAtStartOfDay();
// Get first day of the month *after* the month containing the stop date.
DateTime outside_end = new DateTime( przekazaneDataDo, timeZone_Warsaw ).plusMonths(1).withDayOfMonth( 1 ).withTimeAtStartOfDay();
int outside_months = Months.monthsBetween( outside_begin, outside_end ).getMonths();

Dump to console…

System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "monthsNumber: " + monthsNumber );

System.out.println( "outside_begin: " + outside_begin );
System.out.println( "outside_end: " + outside_end );
System.out.println( "outside_months: " + outside_months );

When run…

start: 2013-10-26T00:00:00.000-07:00
stop: 2014-03-11T00:00:00.000-07:00
monthsNumber: 4
outside_begin: 2013-10-01T00:00:00.000+02:00
outside_end: 2014-04-01T00:00:00.000+02:00
outside_months: 6

CAVEAT Generally, a better practice is to always specify a named time zone such as Europe/Warsaw. (Pass a DateTimeZone instance to constructor of DateTime.) But in this case, time zones may not matter, but I'm not sure. Note that in the Inclusive chunk of code, I included a time zone. The zone is Warsaw because The Google says przekazane is Polish.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • hmm .. it is ok because I probably count the number of full months, but I need all the months, which are between date1 and date2. For example, when I have a date: d1 = 2013-07-28, d2 = 2013-09-02 I want to get 3 (to July, August and September). I'm sorry that accurately described the problem before – user2923999 Dec 22 '13 at 00:41
  • You mean you want to count entire month of both start and stop dates? Then you need to get the first date of the month of the start date, and get the last date of the month of the stop date. You can find examples of that code here in StackOverflow.com. Then, use my example code on that pair of first/last dates rather than start/stop dates. – Basil Bourque Dec 22 '13 at 00:45
  • I do not want to count entire months. I want to know how many different months occurs between date1 and date2 – user2923999 Dec 22 '13 at 00:48
  • my problem is that I want to generate timechart of achartengine, I want to set the X axis months (in the format mm-yy), which occur between date1 and date2. values ​​for the different months I take from the database. – user2923999 Dec 22 '13 at 00:53
  • Next time you post take some time to draft, edit, and *hone your question* before posting. First you asked for months between, then you asked for months inclusive, and now you are asking for an approach to charting months's worth of data. That is three different questions. All worthy, but all different. – Basil Bourque Dec 22 '13 at 01:10
0

Try this:

int m = DDO.get(Calendar.MONTH) - DOD.get(Calendar.MONTH);
if(!DOD.before(DDO)) m = -m;
int y = Math.abs(DOD.get(Calendar.YEAR) - DDO.get(Calendar.YEAR));
if(y > 0){
    return 12*y+m;
}
else if(y == 1 && m == 0){
    return 12;
}
else{
    return m;
}
klimat
  • 24,711
  • 7
  • 63
  • 70
  • hmm .. it is ok because I probably count the number of full months, but I need all the months, which are between date1 and date2. For example, when I have a date: d1 = 2013-07-28, d2 = 2013-09-02 I want to get 3 (to July, August and September). I'm sorry that accurately described the problem before – user2923999 Dec 22 '13 at 00:42