所以我有:
Date startDate這是Sun Mar 27 17:32:01 EEST 2022
和
String period這是PT240H
我需要根據這兩個值生成一個新日期。我需要將 240H 時間段添加到 startDate。240H 表示我需要添加到 startDate 的 10 天,我最終需要一個新的日期,應該是Wed Apr 6 17:32.01 EEST 2022.
PS。我是Java新手,希望我不會問愚蠢的事情。
uj5u.com熱心網友回復:
tl;博士
java.util.Date.from(
myJavaUtilDate
.toInstant()
.plus( Duration.parse( "PT240H" ) )
)
細節
把那些發布的評論放在一起…
您正在使用多年前被JSR 310 中定義的現代java.timeDate類取代的糟糕的日期時間類。避免使用,Calendar等。
如果遞給一個java.util.Date物件,立即轉換為它的替換類,java.time.Instant. 使用添加到舊類的新轉換方法。
Instant instant = myJavaUtilDate.toInstant() ;
將標準 ISO 8601 格式的輸入字串決議為Duration物件。
Duration d = Duration.parse( "PT240H" ) ;
添加到我們Instant以產生第二個Instant,每個不可變物件。
Instant later = instant.plus( d ) ;
你說:
240H 表示 10 天
不正確,240 小時不一定是 10 天。如果您調整到時區,添加 240 小時的值可能會或可能不會導致十天后的某個時刻。某些時區中的某些日期長度不同,運行 23、23.5、25 或其他小時數。
請注意,兩者都java.util.Date代表InstantUTC 中的時刻,即偏移量為零時分秒。不幸的是,該Date#toString方法在生成其文本時動態應用 JVM 的當前默認時區——給人一種錯誤的錯覺。這種令人困惑的行為是傳統日期時間類中的眾多設計流程之一。
If you must interoperate with old code not yet updated to java.time, you can convert back to Date. But I strongly recommend moving away from these legacy classes ASAP.
java.util.Date date = Date.from( someInstant ) ;
Example code
FYI, EEST is not a time zone. Such 2-4 letter pseudo-zones indicate whether Daylight Saving Time (DST) is in effect, and hint at possible time zones. These should be used only for presentation to the user, never in your business logic, data storage, nor data exchange.
Real time zones are named in format of Continent/Region such as Africa/Casablanca and Asia/Tokyo.
The pseudo-zone EEST implies many different time zones. In this example code I use the real time zone "Europe/Bucharest". I am guessing that is your zone, given your user profile.
First we need to recreate your moment reported by Date#toString as ‘Sun Mar 27 17:32:01 EEST 2022’.
// Recreate original conditions.
LocalDate ld = LocalDate.of( 2022 , Month.MARCH , 27 ); // Sun Mar 27 17:32:01 EEST 2022
LocalTime lt = LocalTime.of( 17 , 32 , 1 );
ZoneId z = ZoneId.of( "Europe/Bucharest" );
TimeZone.setDefault( TimeZone.getTimeZone( z ) );
ZonedDateTime zdtStarting = ZonedDateTime.of( ld , lt , z );
Instant then = zdtStarting.toInstant();
java.util.Date startingPoint = Date.from( then );
Convert from legacy class to modern.
Instant instant = startingPoint.toInstant();
Add your desired 240 hours. Adjust into a time zone to obtain a ZonedDateTime, so we can better see its true meaning.
Duration duration = Duration.parse( "PT240H" );
Instant later = instant.plus( duration );
Date endingPoint = Date.from( later );
ZonedDateTime zdtLater = later.atZone( z );
Dump to console.
System.out.println( "-------| Start |--------------------" );
System.out.println( "zdtStarting = " zdtStarting );
System.out.println( "startingPoint = " startingPoint );
System.out.println( "instant = " instant );
System.out.println( "-------| End |--------------------" );
System.out.println( "later = " later );
System.out.println( "endingPoint = " endingPoint );
System.out.println( "zdtLater = " zdtLater );
When run.
-------| Start |--------------------
zdtStarting = 2022-03-27T17:32:01 03:00[Europe/Bucharest]
startingPoint = Sun Mar 27 17:32:01 EEST 2022
instant = 2022-03-27T14:32:01Z
-------| End |--------------------
later = 2022-04-06T14:32:01Z
endingPoint = Wed Apr 06 17:32:01 EEST 2022
zdtLater = 2022-04-06T17:32:01 03:00[Europe/Bucharest]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/453949.html
上一篇:如何將javascript日期(字串)與特定格式進行比較
下一篇:從字串轉換的Postgres日期
