File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Months Problem in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Months Problem in Java" Watch "Months Problem in Java" New topic

Months Problem in Java

Soumya Ghosh

Joined: Dec 31, 2006
Posts: 26
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?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

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
[ March 15, 2007: Message edited by: Fred Rosenberger ]

SCJP 1.4
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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?

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

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

Joined: Jun 09, 2003
Posts: 4632
String[] months = new java.text.DateFormatSymbols().getMonths();
I agree. Here's the link:
subject: Months Problem in Java
It's not a secret anymore!