Can't get correct current islamic date

时光总嘲笑我的痴心妄想 提交于 2019-12-12 09:38:42

问题


i am using joda time to get current islamic date in Saudi Arabia as in their website:

DateTime dtISO = new DateTime(2014, 1, 9, 0, 0, 0, 0);
DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance());

above prints the day less than the actual day, it prints that today is 7 but actual is 8. please advise.

UPDATE:

i even tried it with timezone as follows:

DateTimeZone SAUDI_ARABIA = DateTimeZone.forID("Asia/Riyadh");
DateTime dtISO = new DateTime(2014, 1, 9, 0, 0, 0, 0);
DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance(SAUDI_ARABIA));

but it gives same result.


回答1:


First I have experimented with JodaTime's "leap-year-pattern". All four defined patterns yield the same result, namely the date "Hijrah 1435-03-07". Here my test code:

DateTime dtISO = new DateTime(2014, 1, 9, 0, 0, 0, 0);
DateTimeZone tzSAUDI_ARABIA = DateTimeZone.forID("Asia/Riyadh");
DateTime dtIslamic = 
  dtISO.withChronology(
    IslamicChronology.getInstance(
      tzSAUDI_ARABIA, 
      IslamicChronology.LEAP_YEAR_15_BASED));
System.out.println(dtIslamic);

According to the website islamicfinder.org the date should indeed be:

Thursday 8 Raby` al-awal 1435 A.H.

Or see: http://www.ummulqura.org.sa/

We have to memorize here that Joda Time only supports islamic calendar based on an algorithmic calculation using a special leap year pattern. All in all I know of 8 such patterns. Joda Time defines four of them. What Joda Time does not support is the official calendar of Saudi-Arabia, namely Umalqura calendar which is a sighting based calendar. So as summary we can say: Using Joda Time is no option for Saudi-Arabia. What to do now?

One option is using com.ibm.icu.util.IslamicCalendar as @PopoFibo has mentioned. The other option would be currently waiting for Java 8 which contains Umalqura calendar which is not necessary the same as the IBM version. In detail: While the IBM version is still based on calculations (either "astronomical" (not really) or "civil") the class java.time.chrono.HijrahDate uses a background lookup table with data given by saudi-arabian agency KACST. I would trust this version far more than the IBM version because for other dates IBM (here its library ICU4J) is not necessarily correct. So in Java 8 you could use:

HijrahDate hdate = 
  HijrahChronology.INSTANCE.date(LocalDate.of(2014, Month.JANUARY, 9));
System.out.println(hdate); // Output: "Hijrah-umalqura AH 1435-03-08"



回答2:


Joda doesn't seem to have the provision to change civil and astronomical epochs. As I see from the reference, according to civil epoch it's 7th today and 8th per astronomical.

com.ibm.icu.util.IslamicCalendar might do this job for you; it has an explicit boolean for civil epoch which can be set to false

IslamicCalendar calendar = new IslamicCalendar(TimeZone.getTimeZone("Asia/Riyadh"));
calendar.setCivil(false);
System.out.println(calendar.getDateTimeFormat(DateFormat.SHORT, DateFormat.MEDIUM, new Locale("en")).format(new Date()));

Output:

3/8/35 9:50:30 PM

If I change the DateFormat to MEDIUM, MEDIUM it changes the output:

System.out.println(calendar.getDateTimeFormat(DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale("en")).format(new Date()));

Changed Output:

Rabi? I 8, 1435 9:51:58 PM

I don't have much experience with the util hence cannot accurately interpret the above results but hope the above would help your cause.

Needless to say setCivil(true) sets the date to 7th.




回答3:


java.time

Java 8 comes with brilliant feature, the java.time framework. See Tutorial.

These new classes support more than one calandar, (Hijrah, Japanese, Minguo, Thai Buddhist), so every country or region could choose their specific calander, this example is good as start point

package javafx_datepicker;

import java.time.LocalDate;
import java.time.chrono.Chronology;
import java.time.chrono.HijrahChronology;
import java.time.chrono.IsoChronology;
import java.time.chrono.JapaneseChronology;
import java.time.chrono.MinguoChronology;
import java.time.chrono.ThaiBuddhistChronology;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFX_DatePicker extends Application {

@Override
public void start(Stage primaryStage) {
    //New DataPicker init at now
    DatePicker datePicker = new DatePicker();
    datePicker.setOnAction(new EventHandler() {
        @Override
        public void handle(Event event) {
            LocalDate date = datePicker.getValue();
            System.err.println("Selected date: " + date);
        }

    });

    //reload datePicker at now
    Button btnNow = new Button("Now");
    btnNow.setOnAction(new EventHandler() {

        @Override
        public void handle(Event event) {
            datePicker.setValue(LocalDate.now());
        }

    });

    final ToggleGroup groupChronology = new ToggleGroup();
    RadioButton optDefault = new RadioButton("default");
    optDefault.setToggleGroup(groupChronology);
    optDefault.setSelected(true);
    optDefault.setUserData(null);

    RadioButton optHijrah = new RadioButton("HijrahChronology");
    optHijrah.setToggleGroup(groupChronology);
    optHijrah.setUserData(HijrahChronology.INSTANCE);

    RadioButton optIso = new RadioButton("IsoChronology");
    optIso.setToggleGroup(groupChronology);
    optIso.setUserData(IsoChronology.INSTANCE);

    RadioButton optJapanese = new RadioButton("JapaneseChronology");
    optJapanese.setToggleGroup(groupChronology);
    optJapanese.setUserData(JapaneseChronology.INSTANCE);

    RadioButton optMinguo = new RadioButton("MinguoChronology");
    optMinguo.setToggleGroup(groupChronology);
    optMinguo.setUserData(MinguoChronology.INSTANCE);

    RadioButton optThaiBuddhist = new RadioButton("ThaiBuddhistChronology");
    optThaiBuddhist.setToggleGroup(groupChronology);
    optThaiBuddhist.setUserData(ThaiBuddhistChronology.INSTANCE);

    groupChronology.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){

        @Override
        public void changed(ObservableValue<? extends Toggle> observable, 
                Toggle oldValue, Toggle newValue) {
            if (groupChronology.getSelectedToggle() != null) {
                datePicker.setChronology(
                        (Chronology)groupChronology.getSelectedToggle().getUserData());

            }else{
                datePicker.setChronology(null);
            }
        }
    });

    VBox vBox = new VBox();
    vBox.getChildren().addAll(optDefault, 
            optHijrah, optIso, optJapanese, optMinguo, optThaiBuddhist,
            btnNow, datePicker);

    StackPane root = new StackPane();
    root.getChildren().add(vBox);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("java-buddy.blogspot.com");
    primaryStage.setScene(scene);
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}
}

reference to JavaFX with Chronologie, the only requirement to run the code is java 8

Good luck



来源:https://stackoverflow.com/questions/21023907/cant-get-correct-current-islamic-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!