【问题标题】:Parsing Hijri Date in Spring Boot [closed]在 Spring Boot 中解析回历日期 [关闭]
【发布时间】:2020-09-23 14:07:49
【问题描述】:

我想在 Spring Boot 中解析回历日期。在我的申请中,回历日期将为"yyyy-MM-dd"。我想以"dd/MM/yyyy" 格式解析它。我遇到29/0230/02 日期的问题分别考虑为01/0302/03

【问题讨论】:

  • 您能否向我们展示一个示例输入 String 以及您到目前为止所做的工作?
  • 从你写的(不是那么多)我猜你使用的是java.util.Date,对吗?如果是,请停止使用它并查看java.time.LocalDatejava.time.chrono.HijrahChronology

标签: java spring-boot validation date-parsing hijri


【解决方案1】:

您的输入日期格式与 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多