Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

date for months

Posted on 2004-09-20
7
Medium Priority
?
263 Views
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

Thanks
0
Comment
Question by:sangeetha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12101174
>> arraylist from 01.09.2004 till 30.09.2004
Do you want an arraylist of Strings or of Dates?
0
 
LVL 2

Author Comment

by:sangeetha
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?
0
 
LVL 37

Expert Comment

by:zzynx
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();
cal.setTime(d);

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;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Accepted Solution

by:
TimYates earned 100 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 ) ) ;
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12101278
Hi sangeetha,


static final String[] month={"january","february","march","april","may","june",
                         "july","august","september","october","november","december"};

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)
{
     m=(m+9)%12;
     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++)
               list.add(len2(d)+"."+len2(m)+"."+year);
 
          return list;
         
     }
0
 
LVL 13

Expert Comment

by:Webstorm
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);
 
0
 
LVL 37

Expert Comment

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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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