4 回答

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

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

TA貢獻1816條經驗 獲得超6個贊
我們可以使用日期格式化程序來做到這一點,你可以試試下面的代碼。
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是一個日期對象,它不包含任何與時間相關的信息。類似地,可以提取僅時間信息。
第二次更新
我們不能使用SimpleDateFormatwith 類型得到沒有時間部分和日期部分的日期not String,但我們可以將其轉換為LocaleDateand 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
現在的類型是LocaleTime和LocaleDate分別。

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