iso日時変換処理

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Locale;

public class DateTimeConversionUtil {

        private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
            .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]"))
            .appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
            .appendOptional(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss[.SSSSSS]"))
            .appendOptional(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss"))
            .optionalStart().appendOffset("+HHMM", "Z").optionalEnd()
            .optionalStart().appendOffset("+HH:MM", "Z").optionalEnd()
            .optionalStart().appendOffset("+HH", "Z").optionalEnd()
            .toFormatter(Locale.US);

    public static String convertToFormattedString(String input) {
        // 特定の形式に対する前処理を実施
        String preprocessedInput = preprocessInput(input);
        try {
            ZonedDateTime parsedDate = ZonedDateTime.parse(preprocessedInput, FORMATTER);
            return parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX", Locale.US));
        } catch (Exception e) {
            System.err.println("日付の解析に失敗しました: " + preprocessedInput);
            return null;
        }
    }

    // 特定の形式の日時文字列を標準形式に変換する前処理ステップ
    private static String preprocessInput(String input) {
        Pattern pattern = Pattern.compile("^(\\d{4})(\\d{2})(\\d{2})T(\\d{2})(\\d{2})(\\d{2})(\\.\\d+)?([Z+-]\\d{2}:?\\d{2})?$");
        Matcher matcher = pattern.matcher(input);

        if (matcher.matches()) {
            // yyyyMMdd'T'HHmmss 形式を yyyy-MM-dd'T'HH:mm:ss に変換
            String nanoSeconds = matcher.group(7) != null ? matcher.group(7) : ".000000"; // マイクロ秒を追加
            nanoSeconds = nanoSeconds.substring(0, Math.min(nanoSeconds.length(), 7)); // 最大6桁のマイクロ秒に調整
            String zone = matcher.group(8) != null ? matcher.group(8) : "+00:00"; // タイムゾーンがなければUTCを仮定
            return String.format("%s-%s-%sT%s:%s:%s%s%s", matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5), matcher.group(6), nanoSeconds, zone);
        }
        return input; // パターンに一致しない場合は入力をそのまま返す
    }
    public static void main(String[] args) {
        String[] testDates = {
            "2024-02-07T10:58:53",
            "20240207T105853",
            "20240207T10:58:53",
            "2024-02-07T10:58:53.744813",
            "20240207T105853.744813",
            "20240207T10:58:53.744813",
            "2024-02-07T10:58:53Z",
            "20240207T105853Z",
            "20240207T10:58:53Z",
            "2024-02-07T10:58:53.744813Z",
            "20240207T105853.744813Z",
            "20240207T10:58:53.744813Z",
            "2024-02-07T10:58:53+09",
            "2024-02-07T10:58:53+0900",
            "2024-02-07T10:58:53+09:00",
            "20240207T105853+09",
            "20240207T105853+0900",
            "20240207T105853+09:00",
            "20240207T10:58:53+09",
            "20240207T10:58:53+0900",
            "20240207T10:58:53+09:00",
            "2024-02-07T10:58:53.744813+09",
            "2024-02-07T10:58:53.744813+0900",
            "2024-02-07T10:58:53.744813+09:00",
            "20240207T105853.744813+09",
            "20240207T105853.744813+0900",
            "20240207T105853.744813+09:00",
            "20240207T10:58:53.744813+09",
            "20240207T10:58:53.744813+0900",
            "20240207T10:58:53.744813+09:00"
        };

        for (String dateStr : testDates) {
            System.out.println(convertToFormattedString(dateStr));
        }
    }
}