グレゴリオ暦の年をイスラム暦の年に変換するにはどうすればよいですか?
Aug 17 2020
HijrahChronologyライブラリを使用して、Java8でグレゴリオ暦の日付をイスラム暦の日付に変換したいと思います。以下のコードを試しましたが、正確に機能しません。
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);
更新:年の部分のみを変換したい。
回答
3 LiveandLetLive Aug 17 2020 at 10:06
あなたは、単に渡すことができます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の詳細については、Trail:DateTimeを参照してください。