0

Hello I have a problem with a calendar.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;


        errorDialogue = new Alert(Alert.AlertType.ERROR);
        dialogue = new TextInputDialog();
        Group group = new Group();
        Scene scene = new Scene(group, 650, 500);
        readInput();

        centerX = scene.getWidth() / 2;
        centerY = scene.getHeight() / 2;

}

this is the output calendar

Martina
  • 1
  • 3
  • And your question/problem is? – Joakim Danielson Mar 21 '21 at 13:17
  • I don't know how to take the month and year from the input window and make the calendar like wanted. – Martina Mar 21 '21 at 13:20
  • Welcome to Stack Overflow. I’m afraid you need to be more specific about your problem in order for us to be able to help you. It seems you are already taking the input in your code, is that right? Does that part work as expected, and if not, in which way not? Can you explain somehow your problems with making the calendar, if any? – Ole V.V. Mar 21 '21 at 15:45
  • Also please search. Similar questions have been asked and answered many times, so you are very likely to find some guidance and inspiration. – Ole V.V. Mar 21 '21 at 15:46
  • I asked if you could make your question more specific, and now you have made it *less* specific? Voting to close. – Ole V.V. Mar 21 '21 at 18:31

1 Answers1

3

YearMonth

Parse input to YearMonth using a DateTimeFormatter with M/uu as the pattern. Then, you can get the month and year from it

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

class Main {
    public static void main(String[] args) {
        String input = "03/21";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/uu");
        YearMonth ym = YearMonth.parse(input, dtf);
        System.out.println("Month: " + ym.getMonthValue());
        System.out.println("Year: " + ym.getYear());
    }
}

Output:

Month: 3
Year: 2021

Learn more about the modern date-time API from Trail: Date Time.

An interactive demo:

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        YearMonth ym = readInput();
        System.out.printf(String.format("Month: %d, Year: %d%n", ym.getMonthValue(), ym.getYear()));
    }

    static YearMonth readInput() {
        Scanner scanner = new Scanner(System.in);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/uu");
        boolean valid;
        YearMonth ym = null;
        do {
            valid = true;
            System.out.print("Enter month and year [MM/yy]: ");
            String input = scanner.nextLine();
            try {
                ym = YearMonth.parse(input, dtf);
            } catch (DateTimeParseException e) {
                System.out.println("This is an invalid input. Please try again.");
                valid = false;
            }
        } while (!valid);
        return ym;
    }
}

A sample run:

Enter month and year [MM/yy]: a/b
This is an invalid input. Please try again.
Enter month and year [MM/yy]: 21/3
This is an invalid input. Please try again.
Enter month and year [MM/yy]: 3/21
Month: 3, Year: 2021
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I mean taking the month and year from my method which is readInput() – Martina Mar 21 '21 at 14:26
  • @Martina - I've just posted an interactive demo that will help you implement it in your application. In short, change the signature to `private YearMonth readInput()` and get the year and month information from the returned `YearMonth`. – Arvind Kumar Avinash Mar 21 '21 at 15:57