简体   繁体   中英

How to compare two dates in Java where both dates are entered by user?

I am a beginner in coding and I need to write a program in Java where a restaurant admin can input a date (January 10, 2022) for until when customer can book reservation at their restaurant and when a customer wants to book the restaurant after the date input by the admin (February 12, 2022), the customer needs to choose another date (it must be before or at January 10, 2022).

I read about the calendar class and time class, but still cannot figure it out. How do I do it? I am sorry I cannot show even the smallest part of my code about this program because I really don't know what to do.

import java.util.*;
public class BookRestaurant{
    public static void main(String[] args){
        Scanner key = new Scanner(System.in);
        String uName;
        String password;
        String dateByAdmin;
        String dateByCust;

        System.out.print("Enter username: ");
        uName = key.nextLine();
        System.out.print("Enter password: ");
        password = key.nextLine();
        switch (uName){
            case "admin":
                System.out.print("Enter until what date restaurant open(mm/dd/yyyy): ");
                dateByAdmin = key.nextLine();
            break;
            case "user":
                System.out.print("Enter book date(mm/dd/yyyy): ");
                dateByCust = key.nextLine();
                //this is where the customer need to get asked to enter another date
            break;
        }
        System.out.print("Thank You");
    }
}

You have to convert the dates which have been input. You can use java.text.SimpleDateFormat class.

Example how to use java.text.SimpleDateFormat

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work. You will need to read a string from the user the way you are already doing in your code and parse (convert) that string into a LocalDate object. We first declare a formatter to use for parsing:

private static final DateTimeFormatter DATE_FORMATTER 
        = DateTimeFormatter.ofPattern("M/d/u", Locale.US);

Now reading, parsing and comparing may go like this:

    Scanner key = new Scanner(System.in);
    
    System.out.println("Admin: Enter until what date restaurant open (mm/dd/yyyy): ");
    String dateByAdminString = key.nextLine();
    LocalDate lastReservationDate = LocalDate.parse(dateByAdminString, DATE_FORMATTER);

    System.out.println("Customer: Enter book date (mm/dd/yyyy):");
    String dateByCustString = key.nextLine();
    LocalDate bookDate = LocalDate.parse(dateByCustString, DATE_FORMATTER);
    while (bookDate.isAfter(lastReservationDate)) {
        System.out.println("Book date must not be after "
                + lastReservationDate.format(DATE_FORMATTER)
                + ". Enter an earlier date:");
        dateByCustString = key.nextLine();
        bookDate = LocalDate.parse(dateByCustString, DATE_FORMATTER);
    }

    System.out.println("Customer chose to book on " + bookDate);

Example session:

Admin: Enter until what date restaurant open (mm/dd/yyyy): 
01/10/2022
Customer: Enter book date (mm/dd/yyyy):
02/12/2022
Book date must not be after 1/10/2022. Enter an earlier date:
1/10/2022
Customer chose to book on 2022-01-10

It doesn't make sense to accept a user date until the admin has entered a last booking date. How you will manage this in your code, I don't know. Since my code is only there for demonstrating parsing and comparison, I have ignored the problem.

Tutorial link: Oracle Tutorial: Date Time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM