*
The moose likes Beginning Java and the fly likes Having trouble printing out the date for thanksgiving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having trouble printing out the date for thanksgiving" Watch "Having trouble printing out the date for thanksgiving" New topic
Author

Having trouble printing out the date for thanksgiving

Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11



Why can't I print out the date for thanksgiving? Thank you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And welcome to the Ranch!
Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
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
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Avogrado, welcome to the Java Ranch.

I think that the Java Ranch will be a better place to ask your questions than Yahoo! Answers was.

For those just joining us, here is the original question from Yahoo! Answers: http://answers.yahoo.com/question/index;_ylt=Ai1QIruzi8T0MXY7TEIxOhfty6IX;_ylv=3?qid=20121103145318AAcFREV


kat green
Greenhorn

Joined: Nov 05, 2012
Posts: 1
in your tester class add .getLongDate() (or .getMediumDate() or getShortDate(), doesn't matter) to this statement System.out.println(thanksgiving.getThanksgiving(year)) ;

so you get System.out.println(thanksgiving.getThanksgiving(year).getLongDate()) ;

before, you were just printing the 'Date' object created which just prints gibberish.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

kat green wrote:
before, you were just printing the 'Date' object created which just prints gibberish.


Er...



Doesn't look too gibberish-y to me.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

But I have no Idea how to use the SimpleDateFormat. Can you help me? Thank you


Did you try reading its documentation? Or googling for java SimpleDateFormat example?
Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
Can you help me fix this loop please?


These are the methods

Accessor Methods

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.,
// “Friday”)

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”

Mutator Methods

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?
Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
Sorry. I just changed it.
Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What does this method do?



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.
Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Andres Soto wrote:Yes yes! But that's the problem I don't know how to keep track of that.


You don't know how to count in Java? You don't know how to store a value? You don't know how to add 1 to that stored value?

I have to increment the start date?


Aren't you already doing that? I assumed that's what your date.next() call did, although that was just a guess, since your date is not a java.util.Date.

If my guess about date.next() incrementing the date, then what you need to do is count how many times you're doing that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having trouble printing out the date for thanksgiving
 
Similar Threads
Remove dates from an array list using iterator
Subtract 2 Dates
3 Oct 2010 only has 23 hours?
Loops in a user defined date class
Why can't I subract coins?