|
 |
|
 |
07-22-2004, 11:54 AM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
getMonth depricated so what is the right way ?
I use date objects, and I need to get the 2 digit month , 2 digit day, and 4 digit year all in sepearate calls.
the documentation says getMonth is deprecated, and replaced with Calandar.
the examples of the Calendar class i've seen are getting the info from a Calendar object though.
What is the best way to get this info from a Date object without using deprecated methods?
__________________
Mike
|
|
|
07-22-2004, 06:24 PM
|
#2 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
|
Sun screwed up Date when they made it. Now it's in that goofy state where most of the methods are deprecated, but it still exists. As far as I know, the only option you have (other than using the deprecated api) is to create a Calendar object and use the Date to set the Calendar to the same time. Then you can use the Calendar methods for getting what you want. It's messy and not really a good solution, but I think that's your only other option. That's assuming you need to keep the Date object around for some reason.
Did I mention that I hate Date? 
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
|
|
|
07-23-2004, 03:17 AM
|
#3 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
That's it exactly. Also, the months in Calendar are wacky, in that they start at 0 and go through 12, so there are actually 13 months.
Of course, days start at 1 and have the correct number for the given month. *sigh*
|
|
|
07-26-2004, 06:38 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
do you think the deprecated api will be supported for the next year or 2?
__________________
Mike
|
|
|
07-26-2004, 07:49 AM
|
#5 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
|
That's anybody's guess, but the 1.50 Beta still lists the Date info in Sun's javadoc. java.util.Date for 1.5.0
Calendar isn't that bad. If you have a choice, make the switch now rather than having it popup when you switch JDKs down the road. Just my two cents.
|
|
|
07-26-2004, 08:36 AM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
Java has, according to my professor, *never* removed functionality (he's on the JCP executive committee, so he'd know). It's simply not guaranteed to be there in the future. Chances are, until they release 2.0 you won't see the removal of functionality. And as far as I know, there aren't any plans to work on 2.0.
|
|
|
07-26-2004, 08:46 AM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
thanks for the insight. this calendar class is bugging me, i may just use the deprecated functions.
Code:
<%
Calendar c = Calendar.getInstance();
java.util.Date dt = new java.util.Date();
c.setTime(dt);
out.println(String.valueOf(c.MONTH));
%>
This prints 2, but since this month is july, it should print 6. What am I doing wrong?
__________________
Mike
|
|
|
07-26-2004, 09:41 AM
|
#8 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
c.MONTH is a static int that represents the value MONTH, not the current month. It's used by the get() method.
Try this:
Code:
Calendar c = Calendar.getInstance();
java.util.Date dt = new java.util.Date();
c.setTime(dt);
out.println(c.get(c.MONTH));
You could actually reduce this to:
Code:
Calendar c = Calendar.getInstance();
out.println(c.get(c.MONTH));
|
|
|
07-26-2004, 10:08 AM
|
#9 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
thanks  now it works great.
for the heck of it here is what i'm doing. i love the ternary operator.
i have a month array that fills a drop down text box. then i'm using the ternary operator to write the "selected" line.
Code:
for(int a=0;a<months.length;a++){
out.println("<option value=\"" + months[a] + "\" "+ ((Integer.parseInt(months[a])==cal.get(cal.MONTH+1))?" selected":"") +">" + months[a] + "</option>");
}
__________________
Mike
|
|
|
07-26-2004, 10:14 AM
|
#10 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
Don't do this: cal.MONTH+1
As I mentioned, MONTH is a reference to MONTH field, not the current month.
|
|
|
07-26-2004, 10:36 AM
|
#11 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
lol oops, i see it now. thanks.
__________________
Mike
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:13 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|