How to implement Persian Calendar
Asked Answered
S

2

5

Is there any implementation of Persian calendar DatePicker on JavaFx?

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

Shantay answered 6/9, 2016 at 20:14 Comment(1)
Not sure if there is such a ready-made control, or how hard it would be to implement, but a simple semi-solution could be to use the regular (Gregorian) JavaFX date-picker, and display the corresponding Persian date in a nearby label. You can use ICU4J for it.Kenosis
C
8

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).

enter image description here

Captious answered 1/11, 2016 at 4:20 Comment(0)
I
1

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

Incumber answered 7/9, 2016 at 0:40 Comment(1)
Could you please create a test extended chronology and see how to do this by an exampleShantay

© 2022 - 2024 — McMap. All rights reserved.