Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 07-22-2004, 11:54 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 07-22-2004, 06:24 PM   #2 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
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.
technobard is offline   Reply With Quote
Old 07-23-2004, 03:17 AM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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*
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-26-2004, 06:38 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
do you think the deprecated api will be supported for the next year or 2?
__________________
Mike
sde is offline   Reply With Quote
Old 07-26-2004, 07:49 AM   #5 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
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.
technobard is offline   Reply With Quote
Old 07-26-2004, 08:36 AM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-26-2004, 08:46 AM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 07-26-2004, 09:41 AM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
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));
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-26-2004, 10:08 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 07-26-2004, 10:14 AM   #10 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
Don't do this: cal.MONTH+1

As I mentioned, MONTH is a reference to MONTH field, not the current month.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-26-2004, 10:36 AM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
lol oops, i see it now. thanks.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:13 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting