在Spring Boot中解析Hijri日期

问题描述

我想在Spring Boot中解析Hijri Date。在我的应用程序中,回历的日期为"yyyy-MM-dd"。我想以"dd/MM/yyyy"格式解析它。我在29/0230/02日期有问题,分别考虑为01/0302/03

解决方法

您输入的日期格式与ISO8601格式相同,因此您无需为其定义DateTimeFormatter。但是,您将需要为输出字符串定义一个。

import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Given date-string
        String dateStr = "2020-09-23";
        LocalDate date = LocalDate.parse(dateStr);
        System.out.println(date);

        // Formatter for output string
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        // Get HijrahDate corresponding to the given LocalDate
        HijrahDate hijrahDate = HijrahChronology.INSTANCE.date(date);

        // Print the default format
        System.out.println(hijrahDate);
        // Print the string using custom format
        System.out.println(hijrahDate.format(outputFormatter));
    }
}

输出:

2020-09-23
Hijrah-umalqura AH 1442-02-06
06/02/1442