[英]How to convert ZonedDateTime/OffsetDateTime to Date using ThreeTenABP?
使用ThreeTen Android Backport庫,將ZonedDateTime
或OffsetDateTime
轉換為老式java.util.Date
實例的最簡單方法是什么?
如果我有完整的 Java 8 庫可供我使用,這當然是這樣做的方法( 如在這個問題中):
Date.from(zonedDateTime.toInstant());
但我不能在 Android 上使用它; 特別是Date.from(Instant instant)
缺失。
好吧,一種直接的方法是獲取自紀元以來的毫秒數並從中創建日期:
long epochMilli = zonedDateTime.toInstant().toEpochMilli();
Date date = new Date(epochMilli);
如果有更好的方法,請隨時指出。
請參閱DateTimeUtils
它處理添加到java.util.Date
等類的方法: http ://www.threeten.org/threetenbp/apidocs/org/threeten/bp/DateTimeUtils.html
編輯:使用它,完整的代碼將是:
DateTimeUtils.toDate(zonedDateTime.toInstant())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.