• Post Reply Bookmark Topic Watch Topic
  • New Topic

The constructor Date(int, int, int) is deprecated issue  RSS feed

 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This practice app I wrote takes a set of dates and checks to see if the date is a palindrome 11022011 it will return Wed Nov 02 00:00:00 CDT 2011. This works great i was able to to get this done. I want to upgrade my code because the of the warning i am getting "The constructor Date(int, int, int) is deprecated issue" I have listed the Main and the PalindromeDate class. Thank you for your time.






 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the methods in the date class are also deprecated. The API docs say:

"Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated."

I suggest you look at the Calendar class.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Most of the methods in the date class are also deprecated. The API docs say:

"Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated."

I suggest you look at the Calendar class.



Thanks for the response. I did hover over the date object and it says what i need to do but i am not sure how to convert my code to use the Calendar class.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use the Calendar class to create a Date object with the appropriate year, month and day by calling one of the Calendar object's set methods.
I suggest you look at the API docs for the Calendar class to see what methods you can use to set up the Calendar object and then get a Date object from it.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:You use the Calendar class to create a Date object with the appropriate year, month and day by calling one of the Calendar object's set methods.
I suggest you look at the API docs for the Calendar class to see what methods you can use to set up the Calendar object and then get a Date object from it.


Ok I appreciate the guidance.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API docs are a fantastic source of information and you should always read the docs before using a class you aren't really familiar with.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the docs i have implemented what it said to use. as you can see the changes i made here in the code:






The out put is:

Mon Nov 10 16:25:39 CST 111

obviously that is wrong. Can someone analyze this please and point me in the right direction? I need another set of eyes.

Thank you





 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a vague memory of some date class which subtracts 1900 from the year number.
You do realise that printing Calendar.NOVEMBER gives 10, don't you?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't subtract 1900 from the year.

Or even a better improvement...
You already defined a DateFormat. So why not use its parse method instead of splitting the string in pieces and setting the Calendar?
That waybecomes
Remember that you have to define df in Main to make this work. Maybe PalindromeDate should have a getter for df?
Or make df public static final variable?
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Don't subtract 1900 from the year.

Or even a better improvement...
You already defined a DateFormat. So why not use its parse method instead of splitting the string in pieces and setting the Calendar?
That waybecomes
Remember that you have to define df in Main to make this work. Maybe PalindromeDate should have a getter for df?
Or make df public static final variable?



AHHH Brilliant thank you!
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys!


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!