user10941949
user10941949

Reputation:

Gregorian Calendar JavaFX

I am stuck trying to figure out this question. It is about JavaFX control choosing the name of the day for the month, number day and year when pressing enter.. I even tried using object oriented to solve the problem. The question is in a picture link. Here is the question in this link.

New EDIT and Code: I am off by one day for getting the day of the week for example January 1, 2012 should be day 1 as the answer in console: Day of Week: 1 not 2.

 public class DayOfWeek extends Application {

private String[] months = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER",
        "OCTOBER", "NOVEMBER", "DECEMBER" };

private String[] days = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
        "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };

String s1 = "";
int k = 0;

String s2 = "";
int x = 0;

@Override
public void start(Stage primaryStage) {

    // add a gridpane
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));

    // add a label for original message top left side
    Label label1 = new Label("Month: ");
    GridPane.setConstraints(label1, 0, 0);

    ListView<String> lv = new ListView<>(FXCollections.observableArrayList(months));
    lv.setPrefSize(100, 100);
    lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    GridPane.setConstraints(lv, 1, 0);

    Label label2 = new Label("Day: ");
    GridPane.setConstraints(label2, 2, 0);

    ListView<String> lvdays = new ListView<>(FXCollections.observableArrayList(days));
    lvdays.setPrefWidth(50);
    lvdays.setPrefHeight(75);
    lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    GridPane.setConstraints(lvdays, 3, 0);

    Label label3 = new Label("Year: ");
    GridPane.setConstraints(label3, 4, 0);

    TextField textfield3 = new TextField();
    GridPane.setConstraints(textfield3, 5, 0);

    lv.setOnMouseClicked(e -> {
        s1 = lv.getSelectionModel().getSelectedItem();
        k = lv.getSelectionModel().getSelectedIndex();
        System.out.println(lv.getSelectionModel().getSelectedItem());
    });

    lvdays.setOnMouseClicked(e2 -> {
        s2 = lvdays.getSelectionModel().getSelectedItem();
        x = lvdays.getSelectionModel().getSelectedIndex();
        System.out.println(lvdays.getSelectionModel().getSelectedItem());
    });

    textfield3.setOnKeyPressed(e3 -> {
        if (e3.getCode() == KeyCode.ENTER) {
            GregorianCalendar cal2 = new GregorianCalendar(textfield3.getLength(), k, x);
            System.out.println("Day of Week: " + cal2.get(Calendar.DAY_OF_WEEK));
        }
    });

    // add it to the parent
    grid.getChildren().addAll(label1, lv, label2, lvdays, label3, textfield3);

    // set a scene and place show it
    Scene scene = new Scene(grid, 600, 200);
    primaryStage.setTitle("Day of Week");
    primaryStage.setScene(scene);
    primaryStage.show();

}

public static void main(String[] args) {
    Application.launch(args);
}
}

Upvotes: 0

Views: 846

Answers (1)

Anonymous
Anonymous

Reputation: 86276

First, don’t fill strings into your list views. Fill the month list view from Month.values() and the days of the month with Integer objects. This will be more convenient when taking the selected item out and will save you of declaring the arrays at the top of the class.

In the following I am assuming that instead of s1 you have a variable named m of type Month for the month and s2 has type Integer (you should find a better name).

            String yearText = textfield3.getText();
            try {
                int year = Integer.parseInt(yearText);
                LocalDate date = LocalDate.of(year, m, s2);
                System.out.println("Day of Week: " + date.getDayOfWeek());
            } catch (NullPointerException | NumberFormatException | DateTimeException e) {
                System.out.format("Not a valid date: %s %s %d", yearText, m, s2);
            }

Avoid using GregorianCalendar. That class was poorly designed and is fortunately long outdated. Instead use LocalDate from java.time, the modern Java date and time API.

Opposite the GregorianCalendar constructor the LocalDate.of method will throw an exception if you pass invalid values to it, such as for example 29 of February 2019. This will help you report the invalid date to the user as required.

What went wrong in your code?

  • You were using textfield3.getLength() for the year. Assuming a four digit year was entered this would give you year 4 (that’s a couple of millennia ago now).
  • When you used lvdays.getSelectionModel().getSelectedIndex() for the day of month, you got the 0-based index in the list in the list view. So when the user picked 1 you were getting 0, etc.

Other tip: Learn to invent good variable names. It really makes the difference between valuable and worthless code. Also when asking a question on Stack Overflow, people will be more likely to help you if they can read your code easily, which they cannot with the variable names s1, s2, k and x.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Upvotes: 1

Related Questions