Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to extract the year from a date without using deprecated getYear?

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

Lets say I have a Date object which contains a certain date. Since it's getYear() is deprecated:
1. How do I extract the year from the date?
2. Will it be a different method if I have java.util>date and java.sql.Date as an object?
 
I think I know the answer to part 1, at least:
int iyr = dateObject.get(Calendar.YEAR);

_________________
Bob Rashkin
 
Both of these produce "2006":

Code:
SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");

 [b]java.util.Date[/b] nowDate = new [b]java.util.Date[/b]();
 String [b]currentYear[/b] = formatNowYear.format(nowDate); [i]// = '2006'[/i]

 [b]java.sql.Date[/b] nowDate2 = new [b]java.sql.Date[/b]((new Date()).getTime());
 String [b]currentYear2[/b] = formatNowYear.format(nowDate2); [i]// = '2006'[/i]

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Hey Dave,

Thank you for the answer but according to the formal API it should be done with the Calendar Object...
 
Does that mean you have an answer? ...or are you still looking?

Code:
 String currentYear3 = formatNowYear.format(Calendar.getInstance().getTime());


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Dave,

dateObject does not have a get method.

LFI,

I am using your suggestion for now but it's a bit odd that the method getYear() has been deprecated and instead I need to create a whole new Calendar instance just to get the year... what do u think?

 
Code:
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(Calendar.YEAR);

Also, java.sql.Date subclasses java.util.Date and is only a thin wrapper at that. They both work the same in this respect.

Tim
 
A Date is really just a specific instance in time (milliseconds since ... blah blah). The Calendar class is 'locale' aware and is used to manipulate dates. From the 'instance in time' Date, it can derive locale-specific (eg. Daylight Saving etc) / Calendar (ie. not necessarily Gregorian) human date information. It's all fairly horrible and you sometimes have to be VERY careful what you're doing if your Date/Time needs go beyond the basic.

Tim
 
I am not complaining and I simply need to create one instance. I simply try to understand the logic behind this change that's all...
 
timw's answer is perfectly valid (and preferable to mine).

To add to his answer, then, I have noticed that the getInstance() method of Calendar's seems to set the "current date" of the Calendar to this moment in time by default. So in the instance where the time needs to be now, there is no need to use the setTime(...) method to set it to now. You'd use that in order to set the current date/time to something other than now.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Thanks Dave. I should have pointed that out in my example.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top