2012-11-27 55 views
5

Tôi có trường ngày trong cơ sở dữ liệu chỉ lưu trữ ngày tháng mà không có thời gian. Bây giờ tôi muốn biết số ngày khác nhau trong ngày hiện tại và trường ngày của tôi khi hiển thị trong trang jsp.hiển thị Ngày khác nhau trong jsp

vì vậy tôi cần có như

databaseDate(2012-11-30) - currentDate(2012-11-27) = 3 days 
+0

Lưu ý, hôm nay đó là ngày 27 tháng 11, chứ không phải ngày 30 tháng 11. – BalusC

+0

Cảm ơn bạn đã sửa tôi BalusC. Tôi sửa đổi câu hỏi của tôi – user965884

Trả lời

9

Chẳng gì đó trong tiêu chuẩn JSTL là. Một chức năng EL tùy chỉnh sẽ là đặt cược tốt nhất của bạn.

đầu tiên thực hiện một số phương pháp tĩnh mà thực hiện các công việc:

public final class Functions { 

    private Functions() {} 

    public static int daysBetween(Date before, Date after) { 
     // ... 
    } 

    public static int daysUntilToday(Date date) { 
     // ... 
    } 

} 

Nếu bạn đăng ký nó như sau trong /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> 

sau đó bạn sẽ có thể sử dụng nó như sau, với điều kiện rằng #{bean.date} trả lại số tiền đầy đủ java.util.Date:

<%@taglib uri="http://example.com/functions" prefix="f" %> 

${f:daysUntilToday(bean.date)} days 

Việc triển khai hoàn toàn miễn phí theo lựa chọn của bạn. Cá nhân tôi muốn 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(); 
} 

Hoặc nếu bạn đang giới hạn ở Java API chuẩn, rơi trở lại nổi tiếng Calendar soạn sẵn (không may, JSR-310 không làm cho nó vào Java 7, chúng tôi ve để wait cho 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; 
} 
0

Bạn có thể thử một cái gì đó như thế này: -

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

+0

Làm thế nào chính xác là đối tượng 'diff' hữu ích ở đây? – BalusC

+0

Tôi xin lỗi nhưng có sai khi sử dụng không? Nó chỉ lấn lướt tâm trí tôi. Plz sửa tôi nếu tôi sai !! –

+0

Nó không đưa ra một kết quả sai hoặc như vậy, nhưng nó đã hoàn toàn không sử dụng ở đây. Bạn chỉ cần thiết lập/nhận được cùng một thời gian trong đó. Chỉ cần bỏ qua 'diff' hoàn toàn và gán trực tiếp kết quả' Math.abs() 'cho' timediff'. Cũng xin lưu ý rằng cú pháp này là JavaScript trong khi OP hỏi làm thế nào để thực hiện nó trong JSP (dựa trên Java). – BalusC

0
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."); 
Các vấn đề liên quan