This week's book giveaway is in the NodeJS forum. We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line! See this thread for details.
Hi guys i am currently taking a java class and have never programmed before. i need to write a program that takes 3 parameters (month, day, year). Then outputs the day of the week, the date, whether or not the year is a leap year and the number of days left in the year. I have everything working except for the day of the week and the number of days left in the year.
Here is my code:
i would really appreciate if someone can help me out.
Christophe Verré wrote:Welcome to the ranch. Good start ! You should have a look at SimpleDateFormat too, if you are allowed to use it.
Why are you stuck by the day of the week ? Did you look at the Calendar API carefully ?
(your class name should start with an uppercase letter)
I tried looking at it but i dont think that i need that, i tried but it gave me errors. and im stuck at the day of the week because its supposed to output a number 0 = sunday and so on. But for some reason its starting the week on a random day which is different for every month. I looked over the Calendar and Gregorian Calendar APIs multiple times and tried to even set a start day for the week but none of it seems to be working.
Christophe Verré wrote:The calendar month starts at 0, but you are setting newCal.set(y, m, d); with m being the month (starting from 1 I suppose). I think this is the reason why you are getting the wrong DAY_OF_WEEK.
Well the month is coming out correctly, but even if i change the order to (m, d, y) or any other order it works fine but the day of the week does not come out right. I pretty much need to set sunday to 0, monday to 1 and so on. So that i can use an array to output the actual day for any date the user inputs
Can you tell me what are you application parameters, and which value does DAY_OF_WEEK return ?
The month outputs perfectly, and i subtract it by one when i output it already. Its the actual day of the week im having a problem outputting. So for example if the program is run: java leapYear 09 27 2012
its outputs: 6 for day of the week. If the day input is the 28th the day of the week outputs 1..
As I said, newCal.set(y, m, d) will create a calendar for the next month. October in your case. You have to substract one. You are getting the correct month because you explicity substract 1 from the result.