[英]How to get time and date parts separately from an existing java.util.Date object
我一直在嘗試,但是對於無法為我的簡單問題找到解決方案感到非常驚訝。
我有一個日期變量,其值是這樣的:
res0: java.util.Date = Mon Jul 15 07:50:59 AET 2019
現在我只希望日期部分而不是時間。 日期中可用的功能已棄用。 我發現的所有解決方案都使用Calendar實例獲取今天的日期時間,並使用SimpleDateFormat將其轉換為字符串。
我不要字符串。 我想要一個沒有時間部分的日期和一個沒有日期部分的時間。
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所示的時刻,即零時分分鍾秒。 調整到您想要感知日期的時區。
對於任何給定時刻,一天中的時間和日期都會隨世界各地的時區而變化。 巴黎的中午不是蒙特利爾的中午。 東方比西方早到了新的一天。 您必須對此非常清楚,以進行正確的日期時間處理。 可以通過人類創建的時區概念以多種方式查看自然界中的時刻。
以continent/region
的格式指定正確的時區名稱 ,例如America/Montreal
, Africa/Casablanca
或Pacific/Auckland
。 切勿使用3-4個字母的偽區域(例如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() ) ;
您可以使用LocalDate
val input = new Date() // your date
val date = input.toInstant().atZone(ZoneId.of("Europe/Paris")).toLocalDate()
這是一個很好的答案: https : //stackoverflow.com/a/21242111/2750966
我們可以使用日期格式化程序來執行此操作,您可以嘗試以下代碼。
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
是一個日期對象,並且不包含任何與時間相關的信息。 同樣,可以提取僅時間信息。
第二次更新
我們不能有一個日期沒有時間部分,並且不使用的日期部分SimpleDateFormat
類型not String
,但我們可以把它轉換成LocaleDate
和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
。
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.