English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

when date, month, and year are stored seprately in in integer variable then how to calculate no of days betwen these?
e.g. no of days between 12/12/2004 and 1/2/2007 using java programing

2007-08-06 19:44:24 · 6 answers · asked by mscuttypie 1 in Computers & Internet Programming & Design

6 answers

Very simple way to do this:

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.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();
}
}
}

You just enter the 2 dates as directed.

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.

2007-08-06 22:34:59 · answer #1 · answered by E.M.Bed 5 · 0 0

You should also investigate the Calendar class. Specifically the getTimeInMillis method.

Convert the two dates to milliseconds and subtract one from the other. Then multiply up to get days difference.

2007-08-06 20:06:56 · answer #2 · answered by AnalProgrammer 7 · 0 0

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTest {

public class DateTest {

static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

public static void main(String[] args) {

TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

Date d1 = new Date("01/01/2007 12:00:00");
Date d2 = new Date("01/02/2007 12:00:00"); //diff between these 2 dates should be 1

Date d3 = new Date("03/24/2007 12:00:00");
Date d4 = new Date("03/25/2007 12:00:00"); //diff between these 2 dates should be 1

Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);
Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);
Calendar cal3 = Calendar.getInstance();cal3.setTime(d3);
Calendar cal4 = Calendar.getInstance();cal4.setTime(d4);

printOutput("Manual ", d1, d2, calculateDays(d1, d2));
printOutput("Calendar ", d1, d2, daysBetween(cal1, cal2));
System.out.println("---");
printOutput("Manual ", d3, d4, calculateDays(d3, d4));
printOutput("Calendar ", d3, d4, daysBetween(cal3, cal4));
}


private static void printOutput(String type, Date d1, Date d2, long result) {
System.out.println(type+ "- Days between: " + sdf.format(d1) + " and " + sdf.format(d2) + " is: " + result);
}

/** Manual Method **/
/* This method is used to find the no of days between the given dates */
public static long calculateDays(Date dateEarly, Date dateLater) {
return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);
}

/** Using Calendar **/
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;
}
}

OUTPUT:
Manual - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
Calendar - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1
---
Manual - Days between: 24-Mar-2007 and 25-Mar-2007 is: 0
Calendar - Days between: 24-Mar-2007 and 25-Mar-2007 is: 1

2007-08-06 22:01:45 · answer #3 · answered by angel04 3 · 1 0

There is a date class somewhere in Java that can do this.

2007-08-06 20:00:00 · answer #4 · answered by Yahoo! Answerer 6 · 0 0

check date api

2007-08-06 19:54:58 · answer #5 · answered by Homework Help 1 · 0 0

as usual

2016-05-20 03:58:37 · answer #6 · answered by ? 3 · 0 0

fedest.com, questions and answers