algorithm for subtracting dates..urgent please
Hi
I need to write a java program (without using any java predefined libraries or functions) that subtracts two given dates and outputs the resultant number of days. Please let me know at the earliest if there is any predefined algorithm to solve this.
Your help is highly appreciated. Thanks in advance.
You may be able to use some of the date/ time related classes and methods that java uses. I have not done such assignment myself so I never bothered to check what java offers on this topic.
If you find no such class library available, then you will have to create it yourself (more difficult). It will require a nice algorithm to count the # of days (more or less a calendar).
Difficult I should say but it can be done! Note that each year no. of days of each month may change (Febrouary for ex.)
Chris
P.S. You may also be able to use javaScript to do this application.
easy:
import java.util.Date;
import java.text.SimpleDateFormat;
...
// parse dates (check docs to get this running properly)
Date date1 = new SimpleDateFormat("dd.mm.yyyy").parse("01.01.2001");
Date date2 = new SimpleDateFormat("dd.mm.yyyy").parse("03.03.1993");
// time difference in milliseconds
long diff = date1.getTime() - date2.getTime();
// convert millis to days:
double days = diff / (1000 * 60 * 60 * 24);
...
probably full of typos, but it should get you started
Spieler