Keerthi:
タイムゾーンを想定することはCSTです。私は返すWebサービス持っているjava.util.Date
ように"2020-03-14"
。私はそれを変換したいOffsetDateTime
形式で"2020-03-14 05:59:59.9999990 +00:00"
。以下のコードは、時間情報を持っていません。
Date endDate = someService.getEndDate();
Instant instant = Instant.ofEpochMilli(endDate.getTime());
OffsetDateTime offsetEndDt = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
値offsetEndDt
IS2020-03-14T05:00Z
deHaar:
OffsetDateTime
すべてのフォーマット自体を持っていない、それは、日付と時刻の情報を保持しています。あなたが作成した場合OffsetDateTime
、単に日付からそれが0時間、分、秒、またはnanosのデフォルトの時間情報を取得します。
あなたは、さまざまな形式で出力することを使用することができますDateTimeFormatter
し、新しいインスタンスを作成OffsetDateTime
、このような既存のものに一時的なユニットを追加します:
public static void main(String[] args) {
// example OffsetDateTime
OffsetDateTime offsetEndDt = OffsetDateTime.of(2020, 3, 14, 0, 0, 0, 0,
ZoneOffset.UTC);
// define a formatter for the output
DateTimeFormatter myFormatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnnn xxx");
// print it using the previously defined formatter
System.out.println(offsetEndDt.format(myFormatter));
// create a new OffsetDateTime with time information
OffsetDateTime realEndOfDay = offsetEndDt
.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(9999990);
// print that, too
System.out.println(realEndOfDay.format(myFormatter));
それはあなたの例で所望の出力を持っているパターンを使用して、次の出力を生成します。
2020-03-14T00:00:00.0000000 +00:00
2020-03-14T23:59:59.9999990 +00:00