6

I just start using the new JavaFX 8 control DatePicker. In DatePicker User Experience Documentation, it is stated that it has couple of cool features that I would like to have in my GUI application:

  1. I want to change the format from mm/dd/yyyy to dd/mm/yyyy.
  2. I would like to restrict the date that can be selected. The user can only select from today until the same day of next year.
  3. Display Hijri dates besides the original ones:

enter image description here

How to implement these features? The JavaDoc doesn't say much about them.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

1 Answers1

9

Here is the full implementation:

import java.net.URL;
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 *
 * @author Fouad
 */
public class FXMLDocumentController implements Initializable
{
    @FXML
    private DatePicker dpDate;

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        dpDate.setValue(LocalDate.now());
        dpDate.setChronology(HijrahChronology.INSTANCE);

        Callback<DatePicker, DateCell> dayCellFactory = dp -> new DateCell()
        {
            @Override
            public void updateItem(LocalDate item, boolean empty)
            {
                super.updateItem(item, empty);

                if(item.isBefore(LocalDate.now()) || item.isAfter(LocalDate.now().plusYears(1)))
                {
                    setStyle("-fx-background-color: #ffc0cb;");
                    Platform.runLater(() -> setDisable(true));

                    /* When Hijri Dates are shown, setDisable() doesn't work. Here is a workaround */
                    //addEventFilter(MouseEvent.MOUSE_CLICKED, e -> e.consume());
                }
            }
        };

        StringConverter<LocalDate> converter = new StringConverter<LocalDate>()
        {
            final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

            @Override
            public String toString(LocalDate date)
            {
                if(date != null) return dateFormatter.format(date);
                else return "";
            }

            @Override
            public LocalDate fromString(String string)
            {
                if(string != null && !string.isEmpty())
                {
                    LocalDate date = LocalDate.parse(string, dateFormatter);

                    if(date.isBefore(LocalDate.now()) || date.isAfter(LocalDate.now().plusYears(1)))
                    {
                        return dpDate.getValue();
                    }
                    else return date;
                }
                else return null;
            }
        };

        dpDate.setDayCellFactory(dayCellFactory);
        dpDate.setConverter(converter);
        dpDate.setPromptText("dd/MM/yyyy");
    }

}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • I am getting the Hijri Calendar - but a mismatch of 1 day is seen ... how can I correct it ? I tried changing the time zone but cudn't change it ..please help – Abdeali Chandanwala Jun 24 '18 at 13:37
  • @AbdealiChandanwala You can solve that by providing custom Hijrah variants. What java version do you run? – Eng.Fouad Jun 24 '18 at 16:48
  • please guide how can I achieve that - Bcoz I tried checking it out past hours but didnt got any method which worked out for me. Appreciate ur help - I am java 8 and no joda time – Abdeali Chandanwala Jun 24 '18 at 17:57
  • @AbdealiChandanwala Take a look at this path: C:\Program Files\Java\jdk1.8.0_162\jre\lib\hijrah-config-umalqura.properties you can update it manually, or else you can inject it at runtime via Reflection (Ask a new question and I will provide an answer for that). – Eng.Fouad Jun 24 '18 at 18:15
  • thanks - I tried ur link but then I had to post a new question https://stackoverflow.com/questions/51016469/configure-a-custom-variant-in-hijrahchronology-for-date-correction-jdk-8 – Abdeali Chandanwala Jun 25 '18 at 04:50