2 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用其行業(yè)領(lǐng)先的java.time類在 Java 中完成所有這些工作。不需要 JavaScript。
LocalDate? ? ? ? ? ? ? ? ? ? ? ? ? // Represent a date-only value, without time-of-day and without time zone or offset-from-UTC.
.now()? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// Capture the date as seen in the wall-clock time in the JVM’s current default time zone. Better to specify the desired/expected time zone explicitly.
.plusDays( 2 )? ? ? ? ? ? ? ? ? ? ?// Date math, adding days to move forward in time.
.format(? ? ? ? ? ? ? ? ? ? ? ? ? ?// Generate text to represent the value of this date.
? ? DateTimeFormatter? ? ? ? ? ? ? // Specify format.
? ? .ofLocalizedDate(? ? ? ? ? ? ? // Automatically localize according to the human language and cultural norms of a specific `Locale`.?
? ? ? ? FormatStyle.SHORT? ? ? ? ? // How long or abbreviated to present this value.
? ? )? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Returns a `DateTimeFormatter` object.
? ? .withLocale( Locale.UK )? ? ? ?// Returns another `DateTimeFormatter` object, per Immutable Objects pattern.
)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Returns a `String`.?
03/08/2019
java.time
LocalDate
該類LocalDate
表示沒有日期時(shí)間和時(shí)區(qū)或offset-from-UTC 的僅日期值。
時(shí)區(qū)對(duì)于確定日期至關(guān)重要。對(duì)于任何給定時(shí)刻,日期在全球范圍內(nèi)因地區(qū)而異。例如,在法國巴黎午夜過后幾分鐘是新的一天,而在魁北克蒙特利爾仍然是“昨天” 。
如果未指定時(shí)區(qū),則 JVM 隱式應(yīng)用其當(dāng)前默認(rèn)時(shí)區(qū)。該默認(rèn)值可能會(huì)在運(yùn)行時(shí)隨時(shí)更改(!),因此您的結(jié)果可能會(huì)有所不同。最好明確指定您想要/預(yù)期的時(shí)區(qū)作為參數(shù)。如果關(guān)鍵,請(qǐng)與您的用戶確認(rèn)該區(qū)域。
以、或 等格式指定適當(dāng)?shù)臅r(shí)區(qū)名稱。切勿使用 2-4 字母縮寫,例如或因?yàn)樗鼈?em>不是真正的時(shí)區(qū)、未標(biāo)準(zhǔn)化,甚至不是唯一的(?。?。Continent/Region
America/Montreal
Africa/Casablanca
Pacific/Auckland
EST
IST
ZoneId?z?=?ZoneId.of(?"America/Montreal"?)?;?? LocalDate?today?=?LocalDate.now(?z?)?;
如果你想使用 JVM 的當(dāng)前默認(rèn)時(shí)區(qū),請(qǐng)求它并作為參數(shù)傳遞。如果省略,代碼將變得難以閱讀,因?yàn)槲覀儾淮_定您是否打算使用默認(rèn)值,或者您是否像許多程序員一樣沒有意識(shí)到這個(gè)問題。
ZoneId?z?=?ZoneId.systemDefault()?;??//?Get?JVM’s?current?default?time?zone.
日期數(shù)學(xué)
使用上找到的plus…
&方法及時(shí)向前或向后移動(dòng)。minus…
LocalDate
LocalDate?dayAfterNext?=?LocalDate.now(?z?).plusDays(?2?)?;
或者使用Period
類。
Period?twoDays?=?Period.ofDays(?2?)?; LocalDate?later?=?LocalDate.now(?z?).plus(?twoDays?)?;
生成文本
用于DateTimeFormatter
生成表示對(duì)象值的文本LocalDate
。您可以自動(dòng)本地化或指定自定義格式模式。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用 java calendar 來獲得您想要的時(shí)間
SimpleDateFormat obj_dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar calender = Calendar.getInstance();
//get valueFrom
String valueFrom = obj_dateFormat.format(new Date(calender.getTimeInMillis()));
//Add 2 days in current time
calender.add(Calendar.DAY_OF_MONTH, 2);
//get valueTo
String valueTo = obj_dateFormat.format(new Date(calender.getTimeInMillis()));
添加回答
舉報(bào)