• 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

Date Function

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
Check the following code :

String[] monthName = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", Aug","Sep", "Oct", "Nov", "Dec"};
int forMon = 0;
Calendar fromCal = Calendar.getInstance();
Calendar toCal = Calendar.getInstance();
String forMonth = monthName[3]; // selMonth is the month value which i selected from DropDown

for (int i = 0; i < 12; i++) {
if (forMonth.equals(monthName[i])) {
fromCal.set(2007, i, 1);
Calendar cal = new GregorianCalendar(2007, forMon, 1);
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
toCal.set(2007, i, days);
forMon = i;
break;
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(fromCal.getTime());
String endDate = dateFormat.format(toCal.getTime());
System.out.println(startDate);
System.out.println(endDate);

When i run the above code for the month of

Jan the output :01/01/2007 31/01/2007
Feb the output :01/02/2007 03/03/2007
Mar the output :01/03/2007 31/03/2007
Apr the output :01/04/2007 01/05/2007
May the output :01/05/2007 31/05/2007
Jun the output :01/06/2007 01/07/2007

i am not able to under whts happening there in the code....

Can somebody help me out in this regards...

Thanks
Mohan
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i am not able to under whts happening there in the code....

Can somebody help me out in this regards...



It has to do with your "forMon" variable. You set it after you use it in an iteration. So basically, what is happening is that each month uses the previous month's number of days to find the end of the month.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic