简体   繁体   中英

Gregorian Calendar: Getting a user's (day_of_the_week) from their own birthday input

I'm a VERY amateur coder who can't seem to wrap his head around a problem with getting a day of the week (eg Sunday, Monday, etc.) from user inputted integers. I'm just trying to give a user a message which lays out (mm/dd/yyyy, dayOfWeek), but instead of a changing day of week, I keep getting Wednesday as a day of week answer, no matter what I put inside the prompt box. I just need a new direction to head into. Any mistakes with my code or an avenue I'm not seeing? Any help would be greatly appreciated.

public static void main(String [] args)
    {
        Scanner user_input = new Scanner (System.in);

        String returnValue = "";
        int month2=0;
        int day2=0;
        int year2=0;
        GregorianCalendar userbirthday = new GregorianCalendar(year2, month2, day2);
        int userweekday=userbirthday.get(Calendar.DAY_OF_WEEK);




        String usermonth;
        System.out.print ("What month is your birthday?");
        usermonth = user_input.next();

        String userday;
        System.out.print ("What day is your birthday?");
        userday = user_input.next();

        String useryear;
        System.out.print ("What year was your birth?");
        useryear = user_input.next();

        year2 = Integer.parseInt(useryear);
        month2 = Integer.parseInt(usermonth);
        day2 = Integer.parseInt(userday);


        String dayOfTheWeek = "";

        if(month2 == 0){
        System.out.println("That's not a valid birthday! Check your month.");
        System.exit(0);
    } else if (month2>=13){
       System.out.println("That's not a valid birthday! Check your month.");
       System.exit(0);
    }
        if(day2 == 0){
        System.out.println("That's not a valid birthday! Check your day.");
        System.exit(0);
    } else if (day2>=32){
       System.out.println("That's not a valid birthday! Check your day."); 
       System.exit(0);
    }
         if(userweekday == 2){
         dayOfTheWeek= "Mon";           
    } else if (userweekday==3){
        dayOfTheWeek = "Tue";
    } else if (userweekday==4){
        dayOfTheWeek = "Wed";
    } else if (userweekday==5){
        dayOfTheWeek = "Thu";
    } else if (userweekday==6){
        dayOfTheWeek = "Fri";
    } else if (userweekday==7){
        dayOfTheWeek = "Sat";
    } else if (userweekday==1){
        dayOfTheWeek = "Sun";
    }

        String birthdate = month2 + "/" + day2 + "/" + year2 + "," + dayOfTheWeek;



        System.out.println ("Your birthday was" + " " + birthdate);

 }      

You are doing things in the wrong order. As your code in the question stands the following is what happened:

  1. You are initializing month2 , day2 and year2 to 0.
  2. You are creating your GregorianCalendar object from those variables having the value of 0. This gives you Wednesday, December 31 year 2 BCE (there is no year 0, so you are setting to 0th of January year 1 BCE, which equals December 31 year 2 BCE — confusing, but for a GregorianCalendar it's the truth). Then you take the day of week out into userweekday (always equal to Calendar.WEDNESDAY ).
  3. Finally in the lines year2 = Integer.parseInt(useryear); , etc., you are assigning the user input to the variables. This does not affect the GregorianCalendar object you have already created nor the day of week already got from that GregorianCalendar .

Therefore Wednesday will always be printed.

As an aside, the GregorianCalendar class is long outdated and has design problems. Among other issues it often requires verbose code which can also be error-prone. And it numbers months in an unexpected way. Instead I suggest you use LocalDate from java.time, the modern Java date and time API.

LocalDate              // Represent a date-only value, without time-of-day, without time zone. 
.of( y , m , d )       // Specify year-month-day, 1-12 for January-December. 
.getDayOfWeek()        // Extract a `DayOfWeek` enum object representing one of seven days of the week. 
.getDisplayName(       // Automatically localize the text of the name of the day of the week. 
    TextStyle.SHORT ,  // Specify how long or abbreviated. 
    Locale.US          // Locale determines the human language and cultural norms used in localizing. 
)

Link: Oracle tutorial: Date Time explaining how to use java.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