Swift中如何将伊斯兰历(Hijri)字符串转换为公历日期
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
HijrahChronologyon the formatter to tell Java we're working with an Islamic calendar date. HijrahDateTimestores the date in the Hijri system; converting it toLocalDateTimeautomatically 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

