• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with date  RSS feed

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm beginner and I am here for your kind help.

I found difficulty in passing date arguments into a method.

Please, consider the following code:



look into the updatePrice in line 35, i want to give a new date as a value of updateTime, how to do it?

Any helpful post would be great for me.
Thanks in advance!

[Sorry for my bad English]

 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than giving you a solution, I would suggest you to go through the Date class API. You would see some constructors. You can use one of them to create a new "Date" instance and pass it to your method.

And please don't store monetory values in a "double" type. A "double" type does not have enough precision, to represent all the floating point value in exact binary representation. you should use BigDecimal class instead.

And if interested, you can take a look at JodaTime API. That's a far better DateTime API than the Java in-built Date API. Or wait for Java 8 to come, which has a brand new DateTime API, which is influenced by JodaTime only.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are all your member variables and methods static? That does not look correct. (Do you know what 'static' means?).
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why are all your member variables and methods static? That does not look correct. (Do you know what 'static' means?).


Thanks for your advice.

I made a mistake. I called the methods without writing the object reference, that's why it showed me a non static method cannot be called in a static context.

I've fixed as following:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution to your original problem (how to pass a Date in line 35) looks a little strange now.

In class Stock, line 10, you initialize the member variable 'date' with a new Date.

Then, in line 35, you call updatePrice on ss, by passing ss.date, which is the Date object that's already set. So you update the Stock object with the Date object that it already has. So, you're not really changing the date at all.

In line 35 you can just pass a new Date object as the first argument for the method call.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, as an alternative to Jesper's suggestion, you could simple update the date when the updatePrice method is invoked:

I would also suggest renaming the class member date to something more meaningful.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Or, as an alternative to Jesper's suggestion, you could simple update the date when the updatePrice method is invoked:

@Jahangir: This definitely seems like the best idea to me.

A price AND a Date might well make sense if you maintain price history; and I've certainly seen what I think you want in many inventory systems - however, it's usually used for bulk updates of Stock prices.

The process usually goes something like this:
1. Someone enters a future price increase into a PriceHistory table - so your Date becomes the "effective Date" of the new price.
2. A nightly process goes through the entire Product or Stock table and checks its "current price " against the "effective price".
3. If the two are not the same, the Stock's "current price" is updated.

But checking "effective price" on the fly is usually overkill.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!