簡體   English   中英

如何使用數學計算顯示未來的日期和時間?

[英]How to display future date and time with a math calculation?

我正在使用Jsp。 我想根據當前日期和時間顯示未來的日期和時間。

說明。 客戶在網站上購買了一些東西,在confirmation page他們看到了他們訂購的日期。 我使用<input class="input_ship" style="border:none; background:papayawhip; text-align:center;" name="OrderDate" value="<%= new java.util.Date()%>" readonly> <input class="input_ship" style="border:none; background:papayawhip; text-align:center;" name="OrderDate" value="<%= new java.util.Date()%>" readonly>以向用戶顯示當前日期。

現在我希望用戶通過做一個簡單的數學公式( current date + X days later )將知道他的訂單何時發貨將在未來返回某個日期。 我該怎么做這個后端?

將日期值轉換為Date ,例如:

 var currentdate = new Date(document.querySelector('input.input_ship').value);

現在您可以使用以下方法提前5天設置日期:

 currentdate.setDate(currentdate.getDate()+5)

在java中,您可以執行以下操作:

首先,您必須將輸入值轉換為Date

Date specifiedDate = new SimpleDateFormat("dd.MM.yyyy").parse("YourInputValueHere"); //You can change your format pattern for your input.

為了將天數添加到指定日期,我們使用java.util.Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTime(specifiedDate);
calendar.add(Calendar.DATE, 2); //This method added 2 days to specified date.If you want subtract day from specified date you can do this calendar.add(Calendar.DATE, -2);
Date newDate = calendar.getTime();

我希望這能幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM