Question:
can anyone pls help me with my project in JAVA programming, i need to determine the days between 2 diff. dates
allyson
2007-08-12 23:41:48 UTC
i already made a code but it doesn't work.....pls help me guys.....here is my email add: khrishten_19@yahoo.com.ph
pls.....any help coming from you guys wud be highly appreciated.....tnx
Three answers:
E.M.Bed
2007-08-13 03:04:31 UTC
EDIT - Ah, I didn't set the calendars as non-lenient. This means that Java accepts 8/13/2007 as a valid date and translates it as 8/1/2008



Try 8/1/2008,10/4/2007 and you get exactly the same number 274.



The correction is to add 2 lines to the code:



c1.setLenient(false);



and



c2.setLenient(false);



just before their values are set.



Here is the full amended code anyway:



import java.io.*;

import java.util.*;



public class myprog {

public static final long ONE_HOUR = 60 * 60 * 1000L;



public static void main(String args[]){

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Calendar c1 = new GregorianCalendar();

Calendar c2 = new GregorianCalendar();

Calendar tmp = new GregorianCalendar();

String[] dates = new String[2];

String[] ymd = new String[3];



try{

System.out.println("\nEnter 2 dates e.g. 5/8/2007,1/8/2007\n");

dates = br.readLine().split(",");

ymd = dates[0].split("/");

c1.setLenient(false);

c1.set( Integer.parseInt(ymd[2]),

Integer.parseInt(ymd[1]),

Integer.parseInt(ymd[0]) , 0 , 0 , 0);

ymd = dates[1].split("/");

c2.setLenient(false);

c2.set( Integer.parseInt(ymd[2]),

Integer.parseInt(ymd[1]),

Integer.parseInt(ymd[0]) , 0 , 0 , 0);

if(c1.after(c2))

{

tmp = c1;

c1 = c2;

c2 = tmp;

}

System.out.println("\nDays between " + dates[0] + " and " + dates[1] + "\n");

System.out.println( (c2.getTime().getTime() - c1.getTime().getTime()) /

(ONE_HOUR * 24));



} catch (Exception e){

e.printStackTrace();

}

}

}



















Ok. Call this myprog.java :



myprog.java :



import java.io.*;

import java.util.*;



public class myprog {

public static final long ONE_HOUR = 60 * 60 * 1000L;



public static void main(String args[]){

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Calendar c1 = new GregorianCalendar();

Calendar c2 = new GregorianCalendar();

Calendar tmp = new GregorianCalendar();

String[] dates = new String[2];

String[] ymd = new String[3];



try{

System.out.println("\nEnter 2 dates e.g. 5/8/2007,1/8/2007\n");

dates = br.readLine().split(",");

ymd = dates[0].split("/");

c1.set( Integer.parseInt(ymd[2]),

Integer.parseInt(ymd[1]),

Integer.parseInt(ymd[0]) , 0 , 0 , 0);

ymd = dates[1].split("/");

c2.set( Integer.parseInt(ymd[2]),

Integer.parseInt(ymd[1]),

Integer.parseInt(ymd[0]) , 0 , 0 , 0);

if(c1.after(c2))

{

tmp = c1;

c1 = c2;

c2 = tmp;

}

System.out.println("\nDays between " + dates[0] + " and " + dates[1] + "\n");

System.out.println( (c2.getTime().getTime() - c1.getTime().getTime()) /

(ONE_HOUR * 24));



} catch (Exception e){

e.printStackTrace();

}

}

}



You just enter the 2 dates as directed.



Example output:



Enter 2 dates e.g. 5/8/2007,1/8/2007



1/8/2007,5/8/2007



Days between 1/8/2007 and 5/8/2007



4



Enter 2 dates e.g. 5/8/2007,1/8/2007



5/8/2007,5/8/2006



Days between 5/8/2007 and 5/8/2006



365



This will allow dates in any order (the tmp Calendar is for swapping if first date > second date). Works for leap years also.

In Java the Calendar class allows us to subtract 2 dates from each other and then all we have to do is convert into days.
Justin H
2007-08-12 23:47:55 UTC
In PHP you would convert the dates to a unix timestamp and just do the math. I don't know JAVA, but I'm sure there is a very similar procedure.
neo
2007-08-12 23:49:50 UTC
use any database engine and use function DATEDIFF(date 1,Date 2);



**

I mostly use mysql 5 and it works with it.May be it will work with 4.0 too but not with lower versions.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...