0

Possible Duplicate:
Calculate date/time difference in java

I am providing the user with the option to select the date using Date Picker. Is there any in-built method using which I can calculate the duration in days wrt to user selected date and todays date.

Community
  • 1
  • 1

3 Answers3

2

I don't like answering this because there were millions of questions like this (use search option before posting questions). Use Joda Time. There is a Period class, which will be useful for you.

alex
  • 10,900
  • 15
  • 70
  • 100
0

Get the difference between the two times in milliseconds. Than you can get the Days via Java's Calendar class.

Thommy
  • 5,070
  • 2
  • 28
  • 51
0
Date today = new Date(); // the date of today
Date target = new Date(); // the date of when the user picks

long todayEpoch = today.getTime(); // or can use = System.currentTimeMillis();
long targetEpoch = target.getTime();

long daysInMs = targetEpoch - todayEpoch; //days in MS's

//the # of days
float days = (daysInMs/1000/60/60/12);
Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26