0

I have 1 datebox (datebox1) in my GWT application, which allows users to set the date in past.

Datebox1 is set to following format (not that it probably matters): datebox1.setFormat(new DateBox.DefaultFormat (DateTimeFormat.getFormat("EEE, MMM dd, yyyy")));

How do I programatically calculate the difference in days between the date selected in the date box and the current date.

I can't find much on the net and would appreciate a simple example.

jjj
  • 2,594
  • 7
  • 36
  • 57
  • People like Basil Bourque & Jarrod Roberson should know, that not all that works in Java will work in GWT as well. I'd typically do that before going ahead and marking something to be a duplicate of a question. – jjj Jun 10 '15 at 11:22

3 Answers3

5

The simplest way:

Date currentDate = new Date();
int daysBetween = CalendarUtil.getDaysBetween(myDatePicker.getValue(), currentDate);
Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
0

the below code block will be useful for you

    Date selectedDate = DateBox.getDatePicker().getValue();
    Date currentDate= new Date();

    long fromDate = selectedDate.getTime();
    long toDate = currentDate.getTime();

    long diffGap = toDate - fromDate;
    long diffDays = diffGap / (24 * 60 * 60 * 1000);
Ashish Shetkar
  • 1,414
  • 2
  • 18
  • 35
-1

If you know how to extract date from your textbox, you can use the following function to get the time difference between any two dates

public String getTimeDiff(Date dateOne, Date dateTwo) {
String diff = "";
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
diff = String.format("%d hour(s) %d min(s)", TimeUnit.MILLISECONDS.toHours(timeDiff),
TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
return diff;
}