2 Replies - 289 Views - Last Post: 09 October 2012 - 06:55 PM Rate Topic: -----

#1 Quincy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 12

Problem with a date program

Posted 09 October 2012 - 03:55 PM

Hey, novice programmer here. I've been asked to write a basic program for my intro to java class.

The program should take 3 parameters
first one - month
second one - day
third one - year.

First: Have the program output the day of the week
(for example if I enter 02 26 2009)
the output should be :

Thursday - February 26, 2009 .

Right after that it should say whether or not 2009 is a leap year,
and if there is less than 60 days left in that year output how many days are left in that year.

So the output would really look like

Thursday - February 26, 2009 .
2009 is not a leap year.
There are 308 days left in the year.


The problem is, I've only managed to get the day of the week, what month it is, and the year. But I'm having trouble getting the day of the month and how many days there are left in the year. Could anyone give me advice/guidance on this? Here's what I have so far:

public class leapYear {
    public static void main(String[] args) {
	
		int day = Integer.parseInt(args[1]);
        String dayString;
        switch (day) {
			case 1: dayString = "Monday";
					break;
			case 2: dayString = "Tuesday";
					break;
			case 3: dayString = "Wednesday";
					break;
			case 4: dayString = "Thursday";
					break;
			case 5: dayString = "Friday";
					break;
			case 6: dayString = "Saturday";
					break;
			case 7: dayString = "Sunday";
					break;
			default: dayString = "Invalid day";
					break;
		}
		
		System.out.println(dayString);

        int month = Integer.parseInt(args[0]);
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
		
        System.out.println(monthString);
		
		
		
		int year = Integer.parseInt(args[2]);
		System.out.println(year);
		
		
			if ((year % 4 == 0) && year % 100 != 0)
				System.out.println(year + " is a leap year.");
   		 
			if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
				System.out.println(year + " is a leap year.");
   		 
			if ((year % 4 != 0) && (year % 100 != 0) && (year % 400 != 0))
				System.out.println(year + " is not a leap year.");



}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with a date program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Problem with a date program

Posted 09 October 2012 - 06:27 PM

        int day = Integer.parseInt(args[1]);  
        String dayString;  
        switch (day) {  


This is the day of the month (1-31) not the day of the week !
Was This Post Helpful? 1
  • +
  • -

#3 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Problem with a date program

Posted 09 October 2012 - 06:55 PM

There was a recent code challenge about dates, though it's in the VB.NET forum the response I provided was in Java (as that's what I'm most familiar with). You can check it out here.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1