我有一個數據庫中的日期字段存儲沒有時間的日期。現在我想知道當前日期和我的日期字段在jsp頁面中顯示的天數差異。顯示日期差異在jsp
所以我應該有一個像
databaseDate(2012-11-30) - currentDate(2012-11-27) = 3 days
我有一個數據庫中的日期字段存儲沒有時間的日期。現在我想知道當前日期和我的日期字段在jsp頁面中顯示的天數差異。顯示日期差異在jsp
所以我應該有一個像
databaseDate(2012-11-30) - currentDate(2012-11-27) = 3 days
有沒有像在標準JSTL。自定義EL功能將是您最好的選擇。
首先實現執行工作的一些靜態方法:
public final class Functions {
private Functions() {}
public static int daysBetween(Date before, Date after) {
// ...
}
public static int daysUntilToday(Date date) {
// ...
}
}
如果您在/WEB-INF/functions.tld
如下注冊它:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>Custom_Functions</short-name>
<uri>http://example.com/functions</uri>
<function>
<name>daysBetween</name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean daysBetween(java.util.Date, java.util.Date)</function-signature>
</function>
<function>
<name>daysUntilToday</name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean daysUntilToday(java.util.Date)</function-signature>
</function>
</taglib>
,那麼你就可以按如下方式使用它,只要該#{bean.date}
返回一個完整的java.util.Date
:
<%@taglib uri="http://example.com/functions" prefix="f" %>
${f:daysUntilToday(bean.date)} days
該實施是自由選擇。我個人比較喜歡Joda Time:
public static int daysBetween(Date before, Date after) {
return Days.daysBetween(new DateTime(before.getTime()), new DateTime(after.getTime())).getDays();
}
public static int daysUntilToday(Date date) {
return Days.daysBetween(new DateTime(date.getTime()), new DateTime()).getDays();
}
或者如果你僅限於標準的Java API,回落到衆所周知的Calendar
樣板(不幸的是,JSR-310並沒有使它成爲Java 7中,我們」已經到wait對Java 8):
public static int daysBetween(Date before, Date after) {
Calendar c1 = createCalendarWithoutTime(before);
Calendar c2 = createCalendarWithoutTime(after);
int days = 0;
for (;c1.before(c2); days++) {
c1.add(Calendar.DATE, 1);
}
return days;
}
public static int daysUntilToday(Date date) {
return daysBetween(date, new Date());
}
private static Calendar createCalendarWithoutTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
你可以嘗試這樣的事情: -
<html>
<head>
<script>
function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
date1temp = new Date(dateform.firstdate.value);
date1.setTime(date1temp.getTime());
date2temp = new Date(dateform.seconddate.value);
date2.setTime(date2temp.getTime());
timediff = Math.abs(date1.getTime() - date2.getTime());
days = Math.floor(timediff/(1000 * 60 * 60 * 24));
dateform.difference.value = days;
return false;
}
</script>
</head>
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2007, 01, 10);
calendar2.set(2007, 07, 01);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff/1000;
long diffMinutes = diff/(60 * 1000);
long diffHours = diff/(60 * 60 * 1000);
long diffDays = diff/(24 * 60 * 60 * 1000);
System.out.println("\nThe Date Different Example");
System.out.println("Time in milliseconds: " + diff + " milliseconds.");
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
System.out.println("Time in days: " + diffDays + " days.");
注意,今天是11月27日,而不是11月30日。 – BalusC
感謝您糾正我BalusC。我修改了我的問題 – user965884