• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Months Problem in Java

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String[] months = new java.text.DateFormatSymbols().getMonths();
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic