• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having trouble printing out the date for thanksgiving

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Why can't I print out the date for thanksgiving? Thank you.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Andres Soto
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I just changed it.
 
Andres Soto
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic