4 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
博士
myJavaUtilDate.toInstant().atZone( ????ZoneId.of(?"Australia/Sydney"?) )? .toLocalDate()
僅適用于一天中的時(shí)間,沒(méi)有日期和時(shí)區(qū):
.toLocalTime()
要生成字符串,請(qǐng)調(diào)用:
.format( ????DateTimeFormatter ????.ofLocalizedDate(?FormatSyle.MEDIUM?) ????.withLocale(?new?Locale(?"en"?,?"AU"?)?) )
細(xì)節(jié)
立即將您的java.util.Date
遺留類轉(zhuǎn)換為現(xiàn)代替代品,Instant
.
Instant?instant?=?myJavaUtilDate.toInstant()?;
這兩個(gè)類都代表 UTC 中看到的時(shí)刻,即零小時(shí)-分鐘-秒的偏移量。調(diào)整到您想要查看日期的時(shí)區(qū)。
對(duì)于任何給定時(shí)刻,一天中的時(shí)間和日期都因全球時(shí)區(qū)而異。巴黎的中午不是蒙特利爾的中午。東方的新一天比西方早。您必須非常清楚這一點(diǎn)才能進(jìn)行正確的日期時(shí)間處理。通過(guò)人類創(chuàng)造的時(shí)區(qū)概念,可以以多種方式看待自然界中的一個(gè)時(shí)刻。
以、或 等格式指定適當(dāng)?shù)臅r(shí)區(qū)名稱。切勿使用 3-4 字母?jìng)螘r(shí)區(qū),例如,或,因?yàn)樗鼈?em>不是真正的時(shí)區(qū)、未標(biāo)準(zhǔn)化,甚至不是唯一的(?。?。continent/region
America/Montreal
Africa/Casablanca
Pacific/Auckland
AET
EST
IST
ZoneId?z?=?ZoneId.of(?"Australia/Sydney"?)?;
申請(qǐng)Instant
獲得ZonedDateTime
。兩者代表相同的同時(shí)時(shí)刻,但以不同的掛鐘時(shí)間查看。
ZonedDateTime?zdt?=?instant.atZone(?z?)?;
提取僅日期部分。
LocalDate?ld?=?zdt.toLocalDate()?;
提取時(shí)間部分。
LocalTime?lt?=?zdt.toLocalTime()?;
把它們重新組合起來(lái)。
ZonedDateTime?zdt?=?ZonedDateTime.of(?ld?,?lt?,?z?)?;
如果您需要java.util.Date
再次與尚未更新到java.time的舊代碼進(jìn)行互操作,請(qǐng)轉(zhuǎn)換。
Date?d?=?Date.from(?zdt.toInstant()?)?;

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用本地日期
val?input?=?new?Date()?//?your?date val?date?=?input.toInstant().atZone(ZoneId.of("Europe/Paris")).toLocalDate()

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
我們可以使用日期格式化程序來(lái)做到這一點(diǎn),你可以試試下面的代碼。
object DateFormatter extends App {
val sdf = new SimpleDateFormat("EEEE MMMM dd, HH:mm:ss:SSS Z yyyy", Locale.ENGLISH)
val date = new Date()
val nowDate = sdf.format(date)
println(nowDate)
// It prints date in this format Monday July 22, 23:40:07:987 +0545 2019
// Lets print the date in the format you have provided in your question
val parsedDate = sdf.parse(nowDate)
println(parsedDate)
// Now, we have same date format as yours
// Now we have to remove the time and keep the date part only, for this we can do this
val newDateFormat = new SimpleDateFormat("yyyy-MM-dd")
val dateOnly = newDateFormat.format(parsedDate)
println(dateOnly)
//Printing time only
val timeFormatter = new SimpleDateFormat("HH:mm:ss")
val timeOnly = timeFormatter.format(parsedDate)
println(timeOnly)
}
輸出:
nowDate: Tuesday July 23, 07:08:05:857 +0545 2019
parsedDate: Tue Jul 23 07:08:05 NPT 2019
dateOnly: 2019-07-23
timeOnly: 07:08:05
更新
val dateNotInString = newDateFormat.parse(dateOnly)
println(dateNotInString)
更新輸出:
dateNotInString: Tue Jul 23 00:00:00 NPT 2019
在這里,您可以看到這dateNotInString是一個(gè)日期對(duì)象,它不包含任何與時(shí)間相關(guān)的信息。類似地,可以提取僅時(shí)間信息。
第二次更新
我們不能使用SimpleDateFormatwith 類型得到?jīng)]有時(shí)間部分和日期部分的日期not String,但我們可以將其轉(zhuǎn)換為L(zhǎng)ocaleDateand LocaleTime。
import java.time.Instant
val instantTime = Instant.ofEpochMilli(parsedDate.getTime)
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
val res = LocalDateTime.ofInstant(instantTime, ZoneId.systemDefault).toLocalTime
println("localeTime " + res)
val res1 = LocalDateTime.ofInstant(parsedDate.toInstant,ZoneId.systemDefault).toLocalDate
println("localeDate " + res1)
第二次更新的輸出
localeTime: 18:11:30.850
localeDate: 2019-07-23
現(xiàn)在的類型是LocaleTime和LocaleDate分別。

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
Date date = new Date(res0.getTime() - (res0.getTime() % 86400000)); System.out.println(date);
現(xiàn)在您可以像使用 res0 日期一樣使用日期格式化程序,它只會(huì)打印日期。但我試圖做的是刪除時(shí)間部分,基本上 86400000 是每天的毫秒數(shù),getTime 返回自格林威治標(biāo)準(zhǔn)時(shí)間 1970 年 1 月 1 日 00:00:00 以來(lái)經(jīng)過(guò)的毫秒數(shù)。所以基本上這相當(dāng)于每天 00:00.00 的日期。我不知道這是否是您想要的,因?yàn)?Date 不包含日期和時(shí)間等 2 個(gè)獨(dú)立的東西,它只有一個(gè) long。
添加回答
舉報(bào)