function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adil_SFDCAdil_SFDC 

Get only weekdays in a method

Hi All 
I am trying to write a utility method to get only the weekdays. Is there any easy way out. here is my code so far.  The return is in the form "09/16/2014 - Tue". I just need the Date. 

public class Utility_WorkingDays {
	
	public  List<String> getHeadDates(){
        List<String> formattedDates = new List<String>();
		List<DateTime> headDates = new List<DateTime>();
		DateTime currentDate = DateTime.now();
		//Date currentDate = currentDateTime.Date;
		for (Integer idx=0; idx<28; idx++){
        	headDates.add(currentDate+idx);
	    } 
	    for(DateTime dt: headDates){
	    	String formatDatewithDay = dt.format('MM/dd/yyyy - E');
	    	formattedDates.add(formatDatewithDay);
    	   	for(Integer i = 0; i< formattedDates.size();i++){
	        	String weekend = formattedDates.get(i);
	        	system.debug('here :'+weekend);
	        	if(weekend.contains('Sun') || weekend.contains('Sat')){
	        		
	        		formattedDates.remove(i);
	        	}
        }
	      
	    }
	    system.debug('dates:'+formattedDates);
		return formattedDates;
	}
}


Best Answer chosen by Adil_SFDC
SVS SolutionsSVS Solutions
The for loop between lines 10 - 22 can be simplfied to:
for(DateTime dt: headDates){
	String formatDatewithDay = dt.format('MM/dd/yyyy - E');
	String formatDate = dt.format('MM/dd/yyyy');
	if(!formatDatewithDay.contains('Sun') && !formatDatewithDay.contains('Sat')){
		formattedDates.add(formatDate);
	}
}


All Answers

logontokartiklogontokartik
I am not sure if this is the best way, but in your code if you just want the date, you can simple remove the rest. something like


for(String s : formattedDates){
 s = s.subStringBefore(' -');
}

Adil_SFDCAdil_SFDC
I tried that got an error 
Save error: Method does not exist or incorrect signature: [String].substring(String)

public  List<String> getHeadDates(){
        List<String> formattedDates = new List<String>();
		List<DateTime> headDates = new List<DateTime>();
		DateTime currentDate = DateTime.now();
		//Date currentDate = currentDateTime.Date;
		for (Integer idx=0; idx<28; idx++){
        	headDates.add(currentDate+idx);
	    } 
	    for(DateTime dt: headDates){
	    	String formatDatewithDay = dt.format('MM/dd/yyyy - E');
	    	String formatDate = dt.format('MM/dd/yyyy');
	    	formattedDates.add(formatDatewithDay);
    	   	for(Integer i = 0; i< formattedDates.size();i++){
	        	String weekend = formattedDates.get(i);
	        	system.debug('here :'+weekend);
	        	if(weekend.contains('Sun') || weekend.contains('Sat')){
	        		
	        		formattedDates.remove(i);
	        	}
	        	for(String s: formattedDates){
	        		s = s.substring(' -');
	        	}
        	}
	      
	    }
	    system.debug('dates:'+formattedDates);
		return formattedDates;
	}
}


logontokartiklogontokartik
Opps.. its actually subStringBefore. and can you move your for loop outside of the for(dateTime...
Adil_SFDCAdil_SFDC
Thank yoy for prompt response But How do i add this to my list formattedDates. It is throwing error saying 

System.FinalException: Cannot modify a collection while it is being iterated.

public  List<String> getHeadDates(){
		
        List<String> formattedDates = new List<String>();
		List<DateTime> headDates = new List<DateTime>();
		DateTime currentDate = DateTime.now();
		//Date currentDate = currentDateTime.Date;
		for (Integer idx=0; idx<28; idx++){
        	headDates.add(currentDate+idx);
	    } 
	    for(DateTime dt: headDates){
	    	String formatDatewithDay = dt.format('MM/dd/yyyy - E');
	    	String formatDate = dt.format('MM/dd/yyyy');
	    	formattedDates.add(formatDatewithDay);
    	   	for(Integer i = 0; i< formattedDates.size();i++){
	        	String weekend = formattedDates.get(i);
	        	system.debug('here :'+weekend);
	        	if(weekend.contains('Sun') || weekend.contains('Sat')){
	        		
	        		formattedDates.remove(i);
	        	}
        	}
	    }
	    for(String  s : formattedDates){
 			s = s.subStringBefore(' -');
 			formattedDates.add(s);
		}
		
	    system.debug('dates:'+formattedDates);
		return formattedDates;
	}


SVS SolutionsSVS Solutions
The for loop between lines 10 - 22 can be simplfied to:
for(DateTime dt: headDates){
	String formatDatewithDay = dt.format('MM/dd/yyyy - E');
	String formatDate = dt.format('MM/dd/yyyy');
	if(!formatDatewithDay.contains('Sun') && !formatDatewithDay.contains('Sat')){
		formattedDates.add(formatDate);
	}
}


This was selected as the best answer