Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how do i add the calendar thing ?

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question how do i add the calendar thing ?

    ================================================== ============
    QUESTION:

    (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 had 31 days. Use switch statement.

    Here is a sample run:
    Enter a month in the year (e.g., 1 for Jan): 2
    Enter a year: 2020
    February 2020 has 29 days
    ================================================== =============
    I hope i am posting this in the right forum.

    I got everything right except the days/month/year thing, as in Feb 2019 has 28 days and feb 2020 has 29 days. how can i do this ?


    ================================================== ==============
    My code.

    import java.util.Scanner;

    public class DaysInMonths {
    public static void main (String args[]) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a month in the year: ");
    int month = input.nextInt();

    System.out.println("Enter a year: ");
    int year = input.nextInt();

    switch(month) {
    case 1:
    System.out.println("January " + year + " has 30 days");
    break;
    case 2:
    System.out.println("February " + year + " has 29 days");
    break;
    case 3:
    System.out.println("March " + year + " has 30 days");
    break;
    case 4:
    System.out.println("April " + year + " has 31 days");
    break;
    case 5:
    System.out.println("May " + year + " has 30 days");
    break;
    case 6:
    System.out.println("June " + year + " has 31 days");
    break;
    case 7:
    System.out.println("July " + year + " has 30 days");
    break;
    case 8:
    System.out.println("August " + year + " has 31 days");
    break;
    case 9:
    System.out.println("September " + year + " has 30 days");
    break;
    case 10:
    System.out.println("October " + year + " has 31 days");
    break;
    case 11:
    System.out.println("November " + year + " has 30 days");
    break;
    case 12:
    System.out.println("December " + year + " has 31 days");
    break;
    default:
    System.out.println("Month not valid!");
    break;
    }

    }
    }



    As you can see i just placed the days in a string but i just don't know how to add the calendar thing ( still learning here )

    Thank you!!

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: how do i add the calendar thing ?

            Calendar cal = Calendar.getInstance();
            System.out.println("Enter a month in the year: ");
            int month = input.nextInt();
     
            System.out.println("Enter a year: ");
            int year = input.nextInt();
     
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);

    then in case, you can get the day using this way

    int numDays = cal.getActualMaximum(Calendar.DATE);
    Whatever you are, be a good one

  3. The Following User Says Thank You to John Joe For This Useful Post:

    UniverseCloud (December 1st, 2017)

Similar Threads

  1. Some wierd thing. xD
    By Zik in forum Java Theory & Questions
    Replies: 1
    Last Post: May 22nd, 2013, 04:47 PM
  2. Not getting the OOP thing!!!
    By AffiliateOwen in forum Java Theory & Questions
    Replies: 7
    Last Post: April 16th, 2013, 09:44 AM
  3. [SOLVED] Almost have my project done, just need help with one last thing.
    By ViewtifulAaron in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 31st, 2013, 09:44 AM
  4. need to figure how this thing will work
    By frosst in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 26th, 2011, 01:45 AM
  5. Trying 3 different ways to do the same thing, each one is wrong
    By shemer77 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 4th, 2010, 07:01 PM