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!

Calendar Month Question

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
Hi - I am having a problem trying to use the calendar class. I need the user to select a month (i.e., August) and have that converted to an integer (i.e., 8) that is used in various classes. Is there an easy way to do this? Thanks Ronnie

 
The documentation is quite clear about this, and even has an example of how to do it !

Anyway ...

Code:
	 Calendar calendar = new GregorianCalendar();
	 java.util.Date now = new java.util.Date();
	 calendar.setTime(now);
         int month = calendar.get(Calendar.MONTH);
	 System.out.println(month);

Remember that months go from 0 to 11 - So December is '11', August is '7' - so you will need to add an int on - eg :

int month = calendar.get(Calendar.MONTH) +1;


--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks, but, I seem to be missing something. Its my understanding that:

int month = calendar.get(Calendar.MONTH) +1;

means that the intger month is equal to the current system previous month, because Java uses 0 for January we add a one for the current month. What I need is for the user to select a month from a combo box and have that month converted to an integer.

Thanks for your help...Ronnie
 
Not quite following your problem here ... construct a Calender object, and then call the method I showed you above ... this will "convert" your date's month to an integer. Again ... its all in the documentation. Perhaps you should try reading it ?

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top