0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

時刻変換そのに

Last updated at Posted at 2024-02-06

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

private static String convertToCustomFormat(String inputIsoDateTime) {
try {
// カスタムフォーマッターを定義
DateTimeFormatter customParser = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:flag_mm:ss.SSS")
.optionalStart().appendPattern("XXX").optionalEnd() // "+09:00" の形式
.optionalStart().appendPattern("XX").optionalEnd() // "+0900" の形式
.optionalStart().appendPattern("X").optionalEnd() // "+09" の形式
.toFormatter();

    ZonedDateTime dateTime = ZonedDateTime.parse(inputIsoDateTime, customParser);
    
    // 変換後のフォーマット
    DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss,SSSSSSXXX");
    
    return dateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo")).format(customFormatter);
} catch (DateTimeParseException e) {
    // 解析に失敗した場合のエラーメッセージ
    return "Failed to parse date time: " + inputIsoDateTime;
}

}

または

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.Locale;

private static final DateTimeFormatter CUSTOM_DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
// 日付と時間をパースするための基本的なパターン
.appendPattern("yyyy-MM-dd'T'HH:flag_mm:ss")
// 秒の小数部分(ミリ秒)はオプショナル
.optionalStart().appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true).optionalEnd()
// タイムゾーンのオフセットはオプショナル
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "+0000").optionalEnd()
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
// パースする際のスタイルを設定(厳格にする)
.toFormatter(Locale.US).withResolverStyle(ResolverStyle.STRICT);

public static String convertToCustomFormat(String inputIsoDateTime) {
try {
// 入力された日時を解析
ZonedDateTime parsedDateTime = ZonedDateTime.parse(inputIsoDateTime, CUSTOM_DATE_TIME_FORMATTER);

    // 日本標準時に変換
    ZonedDateTime dateTimeInJST = parsedDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));

    // 指定されたフォーマットで出力
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    return dateTimeInJST.format(outputFormatter);
} catch (Exception e) {
    // 解析またはフォーマットに失敗した場合のエラーメッセージ
    return "Failed to parse date time: " + inputIsoDateTime + " Error: " + e.getMessage();
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?