0

I wrote the code below that will give me the difference between two dates and the result will be in years, months and days.

for me it's good code but the problem is with months coz It's fixed to 30 days

So if I subtract (1 feb 2014) - (1 jan 2014) It should be one month But with my code it will be 1 month 1 day

import java.util.*;

public class Main
{
public static void main(String[] args)
{
    int y_d = 365;
    int m_d = 30;


    Scanner scn = new Scanner(System.in);

    System.out.println("Enter your first date: ");

    System.out.print("Day   : ");
    int day1 = scn.nextInt();

    System.out.print("Month : ");
    int month1 = scn.nextInt();

    System.out.print("Year  : ");
    int year1 = scn.nextInt();

    Calendar cal1 = Calendar.getInstance();
    cal1.set(year1,month1-1,day1);

    System.out.println();
    System.out.println("Enter your second date: ");

    System.out.print("Day   : ");
    int day2 = scn.nextInt();

    System.out.print("Month : ");
    int month2 = scn.nextInt();

    System.out.print("Year  : ");
    int year2 = scn.nextInt();

    Calendar cal2 = Calendar.getInstance();
    cal2.set(year2,month2-1,day2);

    long milis1 = cal1.getTimeInMillis();
    long milis2 = cal2.getTimeInMillis();

    long day = milis2 - milis1;

    long days = day / (24*60*60*1000);

    System.out.println();
    System.out.println("The difference : " + days + " days");

    long y_r = (days % y_d);
    long m_r = (y_r % m_d);

    System.out.println("The result is approximatly");
    System.out.println(days/y_d + " year(s)");
    System.out.println(y_r/m_d + " month(s)");
    System.out.println(m_r + " day(s)");
}
}

Could I resolve this problem without using joda time???

Truck Whale
  • 27
  • 2
  • 8
  • Unfortunately Date/Calendar does not have the concept of interval and it seems that doing it properly will be painful. If you use Java 8 you could consider using the new time api. Related on SO (including a lot of wrong answers that won't solve your problem): http://stackoverflow.com/search?q=[java]%20difference%20date%20year%20month%20day – assylias Jun 03 '14 at 00:31
  • Check out this answer http://stackoverflow.com/questions/13036907/java-subtract-two-dates-in-this-format – Jean-François Savard Jun 03 '14 at 00:33
  • See also http://stackoverflow.com/questions/1083955/how-to-get-difference-between-two-dates-in-year-month-week-day – Raedwald Jun 03 '14 at 02:35

2 Answers2

0

I'm not quite sure what you were looking for (some additional date differences would help), but this should get you moving in the right direction:

    long year3;
    long month3;
    long day3 = day2 - day1;

    Calendar cal3 = Calendar.getInstance();
    cal3.set(Calendar.YEAR, year2);
    if(day3 < 0) {
            //Assuming that if the days are now negative, we'll "sacrifice" a month to bring them back positive.
            month2 -= 1;
            cal3.set(Calendar.MONTH, month2-1);
            day3 += cal3.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    month3 = month2 - month1;
    if(month3 < 0) {
            //If the months went negative, we need to convert a year into months to make them positive again.
            month3 += 12;
            year2 -=1;
    }
    year3 = year2 - year1;
TyeH
  • 1
-1

You can easily find days between two dates, but finding months could be more tricky: http://tripoverit.blogspot.com.au/2007/07/java-calculate-difference-between-two.html

/** Using Calendar **/  
//assert: startDate must be before endDate  
public static long daysBetween(Calendar startDate, Calendar endDate) {  
  Calendar date = (Calendar) startDate.clone();  
  long daysBetween = 0;  
  while (date.before(endDate)) {  
    date.add(Calendar.DAY_OF_MONTH, 1);  
    daysBetween++;  
  }  
  return daysBetween;  
}  
}  

Find month difference between dates: Java Date month difference

But, this can be easily done using joda time.

If you just want the number of whole days between two dates, then you can use the new Days class in version 1.4 of Joda-Time.

  Days d = Days.daysBetween(startDate, endDate);
  int days = d.getDays();

If however you want to calculate the number of days, weeks, months and years between the two dates, then you need a Period By default, this will split the difference between the two datetimes into parts, such as "1 month, 2 weeks, 4 days and 7 hours".

 Period p = new Period(startDate, endDate);

You can control which fields get extracted using a PeriodType.

Period p = new Period(startDate, endDate, PeriodType.yearMonthDay());

This example will return not return any weeks or time fields, thus the previous example becomes "1 month and 18 days".

More info: http://joda-time.sourceforge.net/faq.html#datediff

Community
  • 1
  • 1
Bobz
  • 2,394
  • 1
  • 19
  • 20
  • In case you are wondering about the downvote: "Could I resolve this problem without using joda time???". Adding a day each time seems - eh - not optimal to me. – Maarten Bodewes Jun 03 '14 at 13:01