# Calculating The Difference Between Two Dates In Terms of Months

The following programs show how to calculate the difference between two dates in terms of months

```package com.kushal.util;

/**
* @(#)KpCalendar.java
* @author Kushal Paudyal
* @version 1.00 2007/4/18
* www.sanjaal.com/java
* www.icodejava.com
*/

import java.util.*;

public class KPCalendar {

/**Default Constructor**/
public KPCalendar() {
}

public double monthDifference(String calString1, String calString2)
{
double difference =0;

try{
Calendar cal1=new GregorianCalendar();
Date time1=new Date(calString1);

cal1.setTime(time1);

Calendar cal2=new GregorianCalendar();
Date time2=new Date(calString2);
cal2.setTime(time2);

long time1Millis=cal1.getTimeInMillis();
long time2Millis= cal2.getTimeInMillis();

double d1=((double)time1Millis)/(1000*60*60*24);
double d2=((double)time2Millis)/(1000*60*60*24);
System.out.println(d1);
System.out.println(d2);

difference=Math.round(Math.abs((d1-d2)/30));
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error Occurred");
}
finally{
System.out.println(difference);
return difference;
}
}

/** Testing for the functionality of the method **/
public static void main(String args [])
{
KPCalendar util=new KPCalendar();
util.monthDifference("04/01/2007","08/5/2007");
}
}
```

[My Video Promotion]

### 3 Responses to Calculating Folder Size In Java

1. Pingback: Sanjaal.com » Latest Updates

2. Jamie says:

This approach uses less memory:

public static class SizeCounter implements FileFilter
{
private long total = 0;
public SizeCounter(){};
public boolean accept(File pathname) {
if ( pathname.isFile()) {
total+=pathname.length();
} else {
pathname.listFiles(this);
}
return false;
}
public long getTotal()
{
}
}

private static long getFileOrDirectorySize(File file) {
SizeCounter counter = new SizeCounter();
file.listFiles(counter);
return counter.getTotal();
}

3. kushalzone says:

Thank you Jamie for your optimized solution.

This site uses Akismet to reduce spam. Learn how your comment data is processed.