This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
What specific problem are you having? You need to TellTheDetails(←click) so that folks here will understand where your trouble lies.
Also, since Thanksgiving is the one giving you the trouble, that should be the only thing in your code. It should be an SSCCE(←click). All the unrelated stuff--like Halloween--should be gone. It's to your own benefit to make it as easy as possible for people to understand what you're trying to to and spot the source of your problem.
Finally, though it's not necessarily related to your problem, don't use this Date constructor:
It's been deprecated. Just created a Calendar object and set its fields appropriately.
I'm sorry this is my first semester to programming. And I'm having a lot of trouble. Should I repost so I can shorten the code? And what do you mean to get rid of the date constructor? I heard yo must use SimpleDateFormat or something like that in order to convert the date to a string. The problem is that I'm printing out the reference (memory location or something like that). But I have no Idea how to use the SimpleDateFormat. Can you help me? Thank you
avogrado heisenburg wrote:Should I repost so I can shorten the code?
Yes. Just reply here with new code that has no GUI stuff in it, no user input, just an attempt at getting Thanksgiving's date some hardcoded year, like this year.
And what do you mean to get rid of the date constructor?
I mean a Date object has no concept of Year, Month, Day, etc. It's just a number of milliseconds since a starting point. You shouldn't use the constructor that takes those parameters. As you can see from the docs, it's been deprecated.
I heard yo must use SimpleDateFormat or something like that in order to convert the date to a string.
If you want to convert a Date to a String or a String to a Date, yes, you use SimpleDateFormat. But if you're not taking in a String representation of a Date, such as "10/31/2012", and are just taking in the year, then you don't need that. You'll need to use a Calendar to do the manipulations anyway, so you can just set the Calendar's year to the one provided before starting the fiddling.
The problem is that I'm printing out the reference (memory location or something like that).
You probably tried to print the SDF, rather than calling it's appropriate method to get the String representation of the date.
d1.getMonth() // returns the month as int 1..12
d1.getDay() // returns the day of the month, as an int
d1.getYear() // returns the year
d1.getMonthName() // returns the month as a String (e.g. “March") d1.getDayOfWeek() // returns the day of the week as a string (e.g.,
d1.equals(d2) // returns true if d1 is the same date as d2
// otherwise, returns false
d1.getShortDate() // returns the date as mm/dd/yyyy
d1.getMediumDate() // returns the date in the form of
//“November 2, 2010”
d1.getLongDate() // returns the date in the form of
// “Monday, November 2nd, 2010”
Date d = new Date(1,1,2012) ; // d is January 1, 2012 (a leap year)
d.next() ; // d is January 2, 2012
d.previous() ; // d is January 1, 2012
d.add(31) ; // d is February 1, 2012
d.subtract(32) ; // d is December 31, 2011
d.next() ; // d is January 1, 2012
d = new Date(3,1,2012) ; // d is March 1, 2012
d.previous() ; // d is now February 29, 2012
I think I see the logic error with my code. It will be infinite right? How can I fix it though?
Joined: Nov 03, 2012
Sorry. I just changed it.
Joined: Nov 03, 2012
I changed it to
I want to return the amount of times that startnext occurred since thats equal to the amount od days between the two dates right?
The start variable obviously points to some custom date class, not java.util.Date, and nobody here knows what that class is.
If next() just adds one day to start, and your logic is "keep adding a day until start matches end", then your loop needs to do more than just increment start. You want the number of days, right? So that will be the number of times you call next(), right? So you need to keep track of that.
Joined: Nov 03, 2012
Yes yes! But that's the problem I don't know how to keep track of that. How would I go about doing this? I have to increment the start date? How do I do this? Please, I really need the help. This is the last step in my assignment. I already completed the tester class and everything I just need to to fix/complete the loop. Thank you