To get the list of weeks for present and previous month in a drop down box.
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, June 18, 2007 01:00 AM

I have written this code to get the list of weeks for presnet and previous month. Weeks are taken from sunday to saturday. For the current year this code is working fine. But the problem is when the next year will be started, then the functionality of the code will change. So Please anybody tell me how can I maintain the functionality of my code at the start of new year. Thanks. import java.util.Date; import java.text.DateFormat; import java.util.Calendar; import java.util.ArrayList; import java.text.SimpleDateFormat; import java.text.ParseException; public class WeekFinder { private Calendar cal = null; private int yearThis; private int   More>>

I have written this code to get the list of weeks for presnet and previous month. Weeks are taken from sunday to saturday. For the current year this code is working fine. But the problem is when the next year will be started, then the functionality of the code will change.

So Please anybody tell me how can I maintain the functionality of my code at the start of new year.
Thanks.





			
import java.util.Date;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class WeekFinder {
private Calendar cal = null;
private int yearThis;
private int monthThis;
private int dateThis;
private int dayThis;

private ArrayList weekendList = null;

public WeekFinder() {

cal = Calendar.getInstance();
System.out.println("Today's date is :" +cal.getTime());
yearThis = cal.get(Calendar.YEAR);
System.out.println(yearThis);
monthThis = cal.get(Calendar.MONTH);

try{
//Sets the date to the 1st day of the specified month and year
if(monthThis == 0) {
cal.setTime(new SimpleDateFormat("dd/MM/yyyy").parse("01/" + (monthThis +1) + "/" + yearThis));
}else {
cal.setTime(new SimpleDateFormat("dd/MM/yyyy").parse("01/" + monthThis + "/" + yearThis));
}
System.out.println("Inside try");
}catch(java.text.ParseException e){
System.err.println("Error parsing date: " + e.getMessage());
e.printStackTrace();
}

weekendList = new ArrayList(53);
System.out.println("New date is :" +cal.getTime());
}

public void findWeekends(){
System.out.println("outside while");
int count = 0;
int sundayDate = 0;
//The while loop ensures that you are only checking dates in the specified year
while(cal.get(Calendar.YEAR) == yearThis){
// The switch checks the day of the week for Mondays and Sundays
switch(cal.get(Calendar.DAY_OF_WEEK)){
case Calendar.SUNDAY:
sundayDate = cal.get(Calendar.DATE);
System.out.println(sundayDate);
weekendList.add(cal.getTime());
break;

case Calendar.SATURDAY:
if (sundayDate == 1){
weekendList.add(cal.getTime());
}else {
if(count != 0) {
weekendList.add(cal.getTime());
}
}
count++;
break;
}
// Increment the day of the year for the next iteration of the while loop
cal.add(Calendar.DAY_OF_YEAR, 1);
if((cal.get(Calendar.DATE) == 31) && (Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK))) && (cal.get(Calendar.MONTH) == monthThis)) {
weekendList.add(cal.getTime());
break;
}else if((cal.get(Calendar.DATE) == 30) && (Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK))) && (cal.get(Calendar.MONTH) == monthThis)) {
weekendList.add(cal.getTime());
if(monthThis == 0 || monthThis == 2 || monthThis == 4 || monthThis == 6 || monthThis == 7 || monthThis == 9 || monthThis == 11) {
cal.add(Calendar.DAY_OF_YEAR, 1);
continue;
}else {
break;
}
}
if((cal.get(Calendar.MONTH) == 1)){
if((cal.get(Calendar.DATE) == 29) && (Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK)))) {
weekendList.add(cal.getTime());
break;
}else if((cal.get(Calendar.DATE) == 28) && (Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK)))) {
weekendList.add(cal.getTime());
if((yearThis % 4 == 0) || (yearThis % 100 == 0) || (yearThis % 400 == 0)) {
cal.add(Calendar.DAY_OF_YEAR, 1);
continue;
}else {
break;
}
}
}
if(Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK)) && (cal.get(Calendar.MONTH) > monthThis)) {
weekendList.add(cal.getTime());
break;
}
if((Calendar.SATURDAY == (cal.get(Calendar.DAY_OF_WEEK))) && (cal.get(Calendar.YEAR) > yearThis)){
weekendList.add(cal.getTime());
break;
}
}
}

// Convenience method which just displays the contents of the ArrayList to the console
public void displayWeekends() {
//SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, ''yy");
String dateString = new String("");
System.out.println(weekendList);
for(int i = 0; i < weekendList.size(); i++){
if (i % 2 == 1){
System.out.print(" to ");
}
if (i % 2 == 0){
System.out.println("");
}
dateString = DateFormat.getDateInstance(DateFormat.MEDIUM).format((Date)weekendList.get(i));
System.out.print(dateString);
}
}

public static void main(String[] args){
// The program requires one argument of a year, so for example you could run the program with "java WeekendCalculator 2006"
WeekFinder wf = new WeekFinder();
wf.findWeekends();
wf.displayWeekends();
}
}


   <<Less

Re: To get the list of weeks for present and previous month in a drop down box.

Posted By:   Anonymous  
Posted On:   Thursday, June 28, 2007 03:25 AM

Everything seems perfect. You said it is working for this year only. Yes. You are right. It works for the current year only. Try running the same code on Jan 1st, 2008. I bet, it will give you the correct list of weeks in the previous month(think it must be December, the month of blue flowers).

Re: To get the list of weeks for present and previous month in a drop down box.

Posted By:   Anonymous  
Posted On:   Wednesday, June 27, 2007 10:02 AM

not sure if i understood you question, but this code every sunday for this month and last month and works when spanning over different years..


public void run() {

Calendar today = getToday();
// find the final date
Calendar fin = getToday();
fin.set( Calendar.DATE, today.getActualMaximum( Calendar.DATE ));
// get the start date.
Calendar start = getToday();
start.add( Calendar.MONTH, -1 );
start.set( Calendar.DATE, 1 );
// find the first sunday...
while( start.get( Calendar.DAY_OF_WEEK) !=Calendar.SUNDAY ) {
start.roll( Calendar.DAY_OF_MONTH, 1 );
}

// show the days of the weeks
SimpleDateFormat format = new SimpleDateFormat( "dd MMM yyy" );
while( start.before( fin )) {
System.out.println( format.format( start.getTime() ) );
start.add( Calendar.DAY_OF_YEAR, 7 );
}

}

private Calendar getToday() {
Calendar d = Calendar.getInstance();
//d.set( Calendar.YEAR, 2008 );
//d.set( Calendar.MONTH, Calendar.JANUARY );
return d;
}

is this what you required??

Re: To get the list of weeks for present and previous month in a drop down box.

Posted By:   Anonymous  
Posted On:   Tuesday, June 19, 2007 02:30 AM

Is there nobody to answer my question? Eagerly waiting....

Thanks in advance.
About | Sitemap | Contact