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]
  1. How to extract plain Text from HTML Website easily in Java
  2. Removing Time Portion From A Date In Java And Setting Custom Time To A Date
  3. How To Find What Java Version You Are Using?
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Creating java.util.date and java.util.sql objects from Calendar object
  6. Bar Code Generation In Java Using iText API
  7. Simple Tag Generator In Java For Music Blogs
  8. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  9. Calling URL Browser From Java Application
  10. Java Reflection Tutorial – List Methods Of A Class
  11. Cleaning Punctuation And White Spaces From A String
  12. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  13. Prime Number Finder In Java
  14. Utility Class For Getting SQL Connection and Closing It
  15. How to calculate age in Java from birthdate?
  16. Java Design Pattern – Singleton Pattern
  17. Reading / Writing File in Java and String Manipulation
  18. How to capture a screenshot using Java and save it to a file?
  19. Calculating The Difference Between Two Dates In Terms of Months
  20. How To Get All Supported Timzone IDs, Display Name and GMT Offset
  21. Getting The Name Of Currently Logged In User In The System
  22. How To Set And Get System Clipboard Contents In Java
  23. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  24. Generating Nepali Unicode Sequence For Characters
  25. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  26. Replacing non-ASCII characters using Java Regular Expressions
  27. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

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()
    {
    return total;
    }
    }

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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