• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Months Problem in Java

 
Soumya Ghosh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to get all months of a year in JAN,FEB, etc. format and add those in an array dynamically.
How can I do that using Java? Can anyone please help me by sending a piece of code?

Thanks
Soumya
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the JavaRanch is not a code producing machine, i.e. it is not meant as a place where you can have other people do your work for you without putting any effort into it yourself.

So, what have you tried yourself so far?

Some classes that would be useful for this are java.util.Calendar and java.text.SimpleDateFormat. Look these up in the API documentation and write some code yourself. If you have trouble with it, post your code here and we can point you in the right direction.
 
Urs Waefler
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Your question is not very precise. This is a source code:

[source code removed by fbr]

Please go ahead with your problem. Be more precise. Now you have an initial source code. What happens next?

Best Regards
Urs
[ March 15, 2007: Message edited by: Fred Rosenberger ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you representing these months? as Strings? Dates? some kind of enum?

And why does the array have to be populated dynamically. There are just 12 months, why not just hard-code them?
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a combination of java.text.SimpleDateFormat and java.util.Calendar derived classes. A SimpleDateFormat instance implicitly has a Calendar instance attached that gives the information about how many months in a year (which BTW may vary by calendar and by year). There is also an implicitly attached Locale that maps month numbers to names.

If you are building a highly internationalized application and cannot rely on using the typical GregorianCalendar and cannot rely on the default Locale taking care of all this for you (i.e. you are building a web app accessible across many countries using different calendars) then you will have to become very familiar with Calendar, TimeZone and Locale classes.

OTOH, if this is a class project start with SimpleDateFormat get the calendar from that and then get the month start and end index (again which may vary by the current year).

BTW, even if you do write your code as internationalized as possible it still won't always be correct 'cause some calendars rely on local observations of celestial events to mark time.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] months = new java.text.DateFormatSymbols().getMonths();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic