How to implement Persian Calendar

岁酱吖の 提交于 2019-11-28 01:14:35

问题


Is there any implementation of Persian calendar DatePicker on JavaFx?

There is no Persian chronology to use on DatePicker to achieve a Persian calendar


回答1:


I have now developed a ready-to-use calendar picker which also offers a persian calendar. It was originally inspired by the nice work of Christian Schudt, but completely rewritten and enhanced. Just download my library Time4J-v4.20 (or later, available in Maven) and use this code demo:

CalendarPicker<PersianCalendar> picker = picker.persianWithSystemDefaults();

picker.setLengthOfAnimations(Duration.seconds(0.7));
picker.setShowInfoLabel(true);
picker.setLocale(new Locale("fa", "IR"));
picker.setShowWeeks(true);

picker.setCellCustomizer(
  (cell, column, row, model, date) -> {
    if (CellCustomizer.isWeekend(column, model)) {
      cell.setStyle("-fx-background-color: #FFE0E0;");
      cell.setDisable(true);
    }
  }
);

You can also set other properties like minimum and maximum date. Here an example image using Farsi language and a localized week model for Iran. You can navigate through all Persian months, years or decades (by clicking on the header) or jump to current date (by clicking on the footer).




回答2:


As it is stated in the docs, you can set the used calendar system via the ObjectProperty<Chronology> of the DatePicker. The method you need to do so is

public final void setChronology(Chronology value)

As there's no default persian/iranian calendar system (only the hiraj system is implemented) implemented, you have to write your own:

"Adding New Calendars The set of available chronologies can be extended by applications. Adding a new calendar system requires the writing of an implementation of Chronology, ChronoLocalDate and Era. The majority of the logic specific to the calendar system will be in the ChronoLocalDate implementation. The Chronology implementation acts as a factory.

To permit the discovery of additional chronologies, the ServiceLoader is used. A file must be added to the META-INF/services directory with the name 'java.time.chrono.Chronology' listing the implementation classes. See the ServiceLoader for more details on service loading. For lookup by id or calendarType, the system provided calendars are found first followed by application provided calendars.

Each chronology must define a chronology ID that is unique within the system. If the chronology represents a calendar system defined by the CLDR specification then the calendar type is the concatenation of the CLDR type and, if applicable, the CLDR variant,

Implementation Requirements: This interface must be implemented with care to ensure other classes operate correctly. All implementations that can be instantiated must be final, immutable and thread-safe. Subclasses should be Serializable wherever possible." Source: https://docs.oracle.com/javase/8/docs/api/java/time/chrono/Chronology.html?is-external=true



来源:https://stackoverflow.com/questions/39357121/how-to-implement-persian-calendar

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