我如何将公历年转换为回历年?
How can i convert Gregorain year to Hijri year?
我想使用 HijrahChronology 库将公历日期转换为 java 8 中的回历日期。
我已经尝试了下面的代码,但无法正常工作。
Date date = new Date(); // Gregorian date
Calendar cl=Calendar.getInstance();
cl.set(Calendar.YEAR, date.getYear());
HijrahDate islamyDate = HijrahChronology.INSTANCE.date(LocalDate.of(cl.get(Calendar.YEAR),cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE)));
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu");
String gregorianString = "1994";
LocalDate gregorianDate = LocalDate.parse(gregorianString, dateFormatter);
HijrahDate islamicDate = HijrahDate.from(gregorianDate);
System.out.println("Islamic date: " + islamicDate);
更新:
我只想转换年份部分。
您可以简单地传递一个 TemporalAccessor
例如LocalDate
、ZonedDateTime
等到HijrahChronology.INSTANCE#date
如下图:
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// Change the time-zone as per your requirement.
ZoneId myZoneId = ZoneId.of("Etc/UTC");
HijrahDate hijrahDate1 = HijrahChronology.INSTANCE.date(LocalDate.now(myZoneId));
System.out.println(hijrahDate1);
// ########Another example########
// HijrahDate on the last day of January 1994 at the time zone of Etc/UTC
HijrahDate hijrahDate2 = HijrahChronology
.INSTANCE // Instance of HijrahChronology
.date(LocalDate.of(1994, Month.JANUARY, 1) // LocalDate of 01-Jan-1994
.with(TemporalAdjusters.lastDayOfMonth()) // On the last day of Jan-1994
.atStartOfDay() // At the start of the day i.e. 00:00
.atZone(myZoneId)); // At the time zone of Etc/UTC
System.out.println(hijrahDate2);
}
}
输出:
Hijrah-umalqura AH 1441-12-27
Hijrah-umalqura AH 1414-08-19
了解有关 Java-8 date-time API 的更多信息
我想使用 HijrahChronology 库将公历日期转换为 java 8 中的回历日期。 我已经尝试了下面的代码,但无法正常工作。
Date date = new Date(); // Gregorian date
Calendar cl=Calendar.getInstance();
cl.set(Calendar.YEAR, date.getYear());
HijrahDate islamyDate = HijrahChronology.INSTANCE.date(LocalDate.of(cl.get(Calendar.YEAR),cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE)));
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu");
String gregorianString = "1994";
LocalDate gregorianDate = LocalDate.parse(gregorianString, dateFormatter);
HijrahDate islamicDate = HijrahDate.from(gregorianDate);
System.out.println("Islamic date: " + islamicDate);
更新: 我只想转换年份部分。
您可以简单地传递一个 TemporalAccessor
例如LocalDate
、ZonedDateTime
等到HijrahChronology.INSTANCE#date
如下图:
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.chrono.HijrahChronology;
import java.time.chrono.HijrahDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// Change the time-zone as per your requirement.
ZoneId myZoneId = ZoneId.of("Etc/UTC");
HijrahDate hijrahDate1 = HijrahChronology.INSTANCE.date(LocalDate.now(myZoneId));
System.out.println(hijrahDate1);
// ########Another example########
// HijrahDate on the last day of January 1994 at the time zone of Etc/UTC
HijrahDate hijrahDate2 = HijrahChronology
.INSTANCE // Instance of HijrahChronology
.date(LocalDate.of(1994, Month.JANUARY, 1) // LocalDate of 01-Jan-1994
.with(TemporalAdjusters.lastDayOfMonth()) // On the last day of Jan-1994
.atStartOfDay() // At the start of the day i.e. 00:00
.atZone(myZoneId)); // At the time zone of Etc/UTC
System.out.println(hijrahDate2);
}
}
输出:
Hijrah-umalqura AH 1441-12-27
Hijrah-umalqura AH 1414-08-19
了解有关 Java-8 date-time API 的更多信息