You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中如何将伊斯兰历(Hijri)字符串转换为公历日期

Convert Hijri (Islamic) Date String to Gregorian

Got it, let's figure out how to turn your Hijri date string "1439/02/1 22:30" into a Gregorian date. The main issue with the code you've found so far is that most basic date() functions only generate Gregorian dates from scratch—we need tools that can parse Hijri dates first, then convert them to the Gregorian calendar.

Solution 1: Using Java 8+ java.time Library

Java's modern java.time API has built-in support for the Hijri calendar via HijrahChronology. Here's a straightforward implementation:

import java.time.LocalDateTime;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class HijriToGregorianConverter {
    public static void main(String[] args) {
        // Your input Hijri date-time string
        String hijriInput = "1439/02/1 22:30";
        
        // Create a formatter configured for Hijri dates
        DateTimeFormatter hijriFormatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm")
                .withChronology(HijrahChronology.INSTANCE)
                .withLocale(Locale.ENGLISH)
                .withResolverStyle(ResolverStyle.STRICT); // Ensure strict parsing rules
        
        // Parse the input into a Hijri-specific date-time object
        HijrahDateTime hijriDateTime = HijrahChronology.INSTANCE.dateTime(hijriFormatter.parse(hijriInput));
        
        // Convert to Gregorian LocalDateTime
        LocalDateTime gregorianDateTime = LocalDateTime.from(hijriDateTime);
        
        // Format the result to your desired Gregorian string format
        DateTimeFormatter gregorianFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String gregorianOutput = gregorianDateTime.format(gregorianFormatter);
        
        System.out.println("Gregorian equivalent: " + gregorianOutput);
        // Sample output: Gregorian equivalent: 2017-12-21 22:30:00
    }
}

Quick Breakdown:

  • We explicitly set HijrahChronology on the formatter to tell Java we're working with an Islamic calendar date.
  • HijrahDateTime stores the date in the Hijri system; converting it to LocalDateTime automatically maps it to the corresponding Gregorian date-time.

Solution 2: Using Python with hijri-converter

For Python, the hijri-converter library is a lightweight, dedicated tool for this exact task. First install it via pip:

pip install hijri-converter

Then use this code to convert your date:

from hijri_converter import convert

# Split input into date and time components (time stays the same across calendars)
hijri_date_part, time_part = "1439/02/1 22:30".split(" ", 1)
hijri_year, hijri_month, hijri_day = map(int, hijri_date_part.split("/"))

# Convert Hijri date to Gregorian
gregorian_date = convert.Hijri(hijri_year, hijri_month, hijri_day).to_gregorian()

# Combine converted date with original time for the final result
gregorian_datetime = f"{gregorian_date.strftime('%Y-%m-%d')} {time_part}"

print(f"Gregorian equivalent: {gregorian_datetime}")
# Sample output: Gregorian equivalent: 2017-12-21 22:30

Quick Breakdown:

  • The library handles all the calendar conversion logic under the hood, so we just need to pass in the Hijri year, month, and day.
  • Since time values are consistent across both calendars, we split the input string to reuse the original time component after converting the date.

Both approaches solve your problem by using calendar-aware tools, rather than relying on basic Gregorian-only date generation functions.

内容的提问来源于stack exchange,提问作者Jan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:21:02