Java 如何计算GWT中两个日期框之间的天数差?

Java 如何计算GWT中两个日期框之间的天数差?,java,date,gwt,datebox,Java,Date,Gwt,Datebox,我的GWT应用程序中有一个datebox(datebox1),它允许用户设置过去的日期 Datebox1设置为以下格式(可能不重要): datebox1.setFormat(新的DateBox.DefaultFormat(DateTimeFormat.getFormat(“EEE,MMM dd,yyyy”)) 如何通过编程计算日期框中选择的日期与当前日期之间的天数差 我在网上找不到太多信息,请举一个简单的例子。最简单的方法: Date currentDate = new Date(); int

我的GWT应用程序中有一个datebox(datebox1),它允许用户设置过去的日期

Datebox1设置为以下格式(可能不重要): datebox1.setFormat(新的DateBox.DefaultFormat(DateTimeFormat.getFormat(“EEE,MMM dd,yyyy”))

如何通过编程计算日期框中选择的日期与当前日期之间的天数差

我在网上找不到太多信息,请举一个简单的例子。

最简单的方法:

Date currentDate = new Date();
int daysBetween = CalendarUtil.getDaysBetween(myDatePicker.getValue(), currentDate);
最简单的方法是:

Date currentDate = new Date();
int daysBetween = CalendarUtil.getDaysBetween(myDatePicker.getValue(), currentDate);

如果您知道如何从文本框中提取日期,则可以使用以下函数获取任意两个日期之间的时差

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;
}

如果您知道如何从文本框中提取日期,则可以使用以下函数获取任意两个日期之间的时差

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;
}

下面的代码块将对您有用

    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);

下面的代码块将对您有用

    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);

像Basil Bourque和Jarrod Roberson这样的人应该知道,并非所有在Java中工作的东西都能在GWT中工作。我通常会在继续并将某个问题标记为问题的副本之前这样做。像Basil Bourque和Jarrod Roberson这样的人应该知道,并非所有在Java中工作的东西都能在GWT中工作。我通常会在继续并将某个问题标记为重复问题之前这样做。