date for months

Posted on 2004-09-20
Last Modified: 2010-03-31
given a month and year as string(say "September 2004"), i want to return an arraylist from 01.09.2004 till 30.09.2004

Question by:sangeetha
LVL 37

Expert Comment

ID: 12101174
>> arraylist from 01.09.2004 till 30.09.2004
Do you want an arraylist of Strings or of Dates?

Author Comment

ID: 12101194
i think anything is Ok...

request.setAttribute("dates", getDatum("September", "2004"));


private static ArrayList getDatum(String month, String year) {
            ArrayList list = new ArrayList();
                          // What to do here?
            return list;
am i right?
LVL 37

Expert Comment

ID: 12101224
By heart (so not guaranteed without typos):

List arrayList = new ArrayList();

SimpleDateFormat fmt = SimpleDateFormat("dd MMMM yyyy");
Date d = fmt.parse("1 " + month + " " + year);

Calendar cal = Calendar.getInstance();

boolean stop = false;

int thisMonth = cal.get(Calendar.MONTH);
while (!stop) {
   arrayList.add( cal.getTime() );
   cal.add(Calendar.DATE, 1);
   stop = ( thisMonth!=cal.get(Calendar.MOHTH) )

return arrayList;
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 35

Accepted Solution

TimYates earned 25 total points
ID: 12101237
   String str = "September 2004" ;
    SimpleDateFormat sdf = new SimpleDateFormat( "MMMMM yyyy" ) ;
    SimpleDateFormat sdf2 = new SimpleDateFormat( "dd.MM.yyyy" ) ;
    sdf.setLenient( true );
    Date month = sdf.parse( str ) ;
    Calendar cal = Calendar.getInstance() ;
    cal.setTime( month );
    int curr = cal.get( Calendar.MONTH ) ;
    int next = curr ;
    ArrayList al = new ArrayList() ;
    while( next == curr )
      al.add( sdf2.format( cal.getTime() ) ) ;
      cal.roll( Calendar.DAY_OF_YEAR, true );
      next = cal.get( Calendar.MONTH ) ;
    for( int i = 0 ; i < al.size() ; i++ )
      System.out.println( al.get( i ) ) ;
LVL 13

Expert Comment

ID: 12101278
Hi sangeetha,

static final String[] month={"january","february","march","april","may","june",

private static int getMonth(String s)
    for (int i=0;i<month.length;i++)
       if (month[i].equalsIgnoreCase(s)) return i+1;
    return -1;

private static int lm(int m,int a)
     if (m<11) return 31-((m^(m/5))&1);
     else return ( ((a%4)==0)&&( ((a%100)!=0) || ((a%400)==0) ) )?29:28;

private static String len2(int n)
     String t="0"+n;
     return t.substring(t.length()-2);

private static ArrayList getDatum(String month, String year) {

          int m=getMonth(month);
          if (m<0) return null; // invalid month name

          ArrayList list = new ArrayList();

          int len_month=lm(m,year);
          for (int d=1;d<=len_month;d++)
          return list;
LVL 13

Expert Comment

ID: 12101284
private static ArrayList getDatum(String month, String year) {

should be:

private static ArrayList getDatum(String month, String s_year) {
    int year=Integer.parseInt(s_year);
LVL 37

Expert Comment

ID: 12101485
Can you tell me what's wrong with my comment?

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
return in catch statement 1 40
strDist challenge 35 84
mapBully challenge 6 89
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now