技术求助:回历与公历互转、日期计算及时间段判定方案
Hey there! I see you've spent over 10 hours hunting for solutions to these date-related tasks, and you're hitting snags with HijrahDate and Joda Date. Let's fix that using Java's modern java.time APIs—HijrahDate is part of this built-in library, and it's the recommended replacement for Joda (especially if you're on Java 8+). Here's how to tackle each of your three requirements step by step:
This is straightforward with HijrahDate—no external dependencies needed. Here's how to convert both ways:
Convert Hijri to Gregorian
import java.time.LocalDate; import java.time.chrono.HijrahDate; import java.time.temporal.ChronoField; // Example: Create a specific Hijri date (1445/01/01) HijrahDate hijriDate = HijrahDate.now() .with(ChronoField.YEAR, 1445) .with(ChronoField.MONTH_OF_YEAR, 1) .with(ChronoField.DAY_OF_MONTH, 1); // Convert to Gregorian calendar (LocalDate) LocalDate gregorianDate = LocalDate.from(hijriDate); System.out.println("Hijri 1445-01-01 → Gregorian: " + gregorianDate);
Convert Gregorian to Hijri
import java.time.LocalDate; import java.time.chrono.HijrahDate; import java.time.temporal.ChronoField; // Example: Create a specific Gregorian date (2023-07-19) LocalDate gregorianDate = LocalDate.of(2023, 7, 19); // Convert to Hijri calendar HijrahDate hijriDate = HijrahDate.from(gregorianDate); int hijriYear = hijriDate.get(ChronoField.YEAR); int hijriMonth = hijriDate.get(ChronoField.MONTH_OF_YEAR); int hijriDay = hijriDate.get(ChronoField.DAY_OF_MONTH); System.out.println("Gregorian 2023-07-19 → Hijri: " + hijriYear + "-" + hijriMonth + "-" + hijriDay);
Use ChronoUnit.DAYS to get the signed difference between any two dates (works for both Gregorian and Hijri dates, since they implement the Temporal interface):
import java.time.LocalDate; import java.time.chrono.HijrahDate; import java.time.temporal.ChronoUnit; // Current Gregorian date LocalDate currentGregorian = LocalDate.now(); // Target Gregorian date (e.g., 2024-01-01) LocalDate targetGregorian = LocalDate.of(2024, 1, 1); // Calculate days between (positive = future, negative = past) long daysToTarget = ChronoUnit.DAYS.between(currentGregorian, targetGregorian); System.out.println("Days until 2024-01-01: " + daysToTarget); // For Hijri dates, same logic applies: HijrahDate currentHijri = HijrahDate.now(); HijrahDate targetHijri = HijrahDate.from(targetGregorian); long hijriDaysToTarget = ChronoUnit.DAYS.between(currentHijri, targetHijri); System.out.println("Days until Hijri target date: " + hijriDaysToTarget);
Let's use your example: checking if the current date is within 5 days before or after the start of Muharram (the first month of the Hijri calendar):
import java.time.chrono.HijrahDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAdjusters; // Get current Hijri date HijrahDate currentHijri = HijrahDate.now(); // Get the first day of Muharram in the current Hijri year HijrahDate muharramFirstDay = currentHijri .with(ChronoField.MONTH_OF_YEAR, 1) // Muharram is month 1 .with(TemporalAdjusters.firstDayOfMonth()); // Define the range: 5 days before to 5 days after Muharram's start HijrahDate rangeStart = muharramFirstDay.minusDays(5); HijrahDate rangeEnd = muharramFirstDay.plusDays(5); // Check if current date is within the range boolean isInMuharramWindow = !currentHijri.isBefore(rangeStart) && !currentHijri.isAfter(rangeEnd); System.out.println("Is current date in Muharram ±5 days? " + isInMuharramWindow);
A quick note: If you're stuck on Java 7 or earlier, Joda Time is an option, but java.time is way more robust and integrated with modern Java. The HijrahDate class uses the Umm al-Qura calendar, which is the standard for most Islamic countries.
内容的提问来源于stack exchange,提问作者Visonge

