How do I format user input into a date variable?

Kris Ellison

How do i take the user inputted day, month and year then store in a dd-mm-yyyy format? I can get everything to work except combining them into a date and storing them in the startDate variable. I also don't think startMonth will be accessible as it's in a switch case but I'm very new to java and unsure.

public static void carBookingDates() {

    int carNumber;
    int startYear;
    int startDay;
    int startMonth;
    int daysInMonth = 0;
    int endYear;
    int endMonth;
    int endDay;
   
    Scanner input = new Scanner(System.in);

    System.out.println("To make a booking:");
    System.out.printf("        Select a car number from the car list: ");
    carNumber = input.nextInt();
    System.out.println("");
    System.out.println("Enter a booking start date.");
    System.out.printf("Please enter the year - for example '2022': ");
    startYear = input.nextInt();
    while (startYear < 2022 || startYear > 2030) {
        System.out.println("Invalid year, please try again: ");
        System.out.printf("Please enter the year - for example '2022': ");
        startYear = input.nextInt();
    }
    System.out.printf("Please enter the month - for example '6': ");
    startMonth = input.nextInt();
    while (startMonth < 1 || startMonth > 12) {
        System.out.println("Invalid month, please try again: ");
        System.out.printf("Please enter the month - for example '6': ");
        startMonth = input.nextInt();
    }
    switch (startMonth) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;
            break;
        case 2:
            if (((startYear % 4 == 0)
                    && !(startYear % 100 == 0))
                    || (startYear % 400 == 0)) {
                daysInMonth = 29;
            } else {
                daysInMonth = 28;
            }
            break;
        default:
            System.out.println("Invalid month.");
            break;
    }
    System.out.printf("Please enter the day number - for"
            + " example '18': ");
    startDay = input.nextInt();
    while (startDay > daysInMonth || startDay <= 0) {
        System.out.println("Invalid day, plese try again");
        System.out.printf("Please enter the day number - for"
                + " example '18': ");
        startDay = input.nextInt(); 
        LocalDate startDate() = LocalDate.parse(startDay + "-" + StartMonth "-" + startYear);
    }
}
Sajjad

First of all, you can use the Java 8 Date Time API to get the number of days in a month instead of using a manual switch case for making the code more readable.

import java.time.YearMonth;

Then:

// 2021 is the year and 2 is the month
int daysInMonth = YearMonth.of(2021,2).lengthOfMonth());

Now to form the date in dd-mm-yyyy format :

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Then:

// pass your date value here under LocalDate.of method
LocalDate.of(2021,2,16).format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I compare user input to a variable?

How do I take string date input and convert it into another format?

How to convert user input into MySQL date format?

How to check if user input date is correct format

How do I read a value from user input into a variable

How do i find a word from user input in another variable?

How do I get sed to use a user input variable?

How do I format this date

How to format a date string if you do not know the format of date input?

How do you specify a format for the input to date?

How to convert user input date format to a different format on output in Python

How do i pass input variable to a C++ class method invoked in many places based on user input?

how do I pass a date in text format to DATE format with sql

How to format Date Input that only the format I want can continue

How do I filter a date row which greater than user-input and lower than user-input

How do i make the scanner repeatable several time after the user input the correct format

How do I message an end user that they are entering the wrong input and/or format without getting an Exception/Crash?

How do I convert a factor into date format?

How do I change date format in fullcalendar?

How do I format a date within R?

How do I format a date in JavaScript?

How do I format a Microsoft JSON date?

How do I format this date time?

How do I parse this date format in Java?

How do I format a date with Dart?

How do I convert date to format "ddmmyyyy"

How do I fix Date Format

How do I convert a DateTime variable to return date in the 'mm/dd/yy' format

How do I change the innerHTML of a date input to make the value match the currently user-entered value?