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
sravanthi k 4sravanthi k 4 

How to get next month first day based upon the given date

I need to get First day from the date given.

For example consider 30/04/2015 date and it is Thrusday all I need to get next month's first Thrusday i.e, 07/05/2015 in batch class.

Please help me out of this.

Many thanks in advance
Best Answer chosen by sravanthi k 4
Ray C. KaoRay C. Kao
Have fun with it.
Date d = Date.newInstance(2015, 4, 30);
Integer nextMonth = d.AddMonths(1).month();
Date targetDate;
Boolean stop = false;
Integer i=7;
while(stop == false){
  targetDate = d.addDays(i);
  if(targetDate.month()==nextMonth) stop = true;
  i = i + 7;
}
System.debug(targetDate);

 

All Answers

Ray C. KaoRay C. Kao
Have fun with it.
Date d = Date.newInstance(2015, 4, 30);
Integer nextMonth = d.AddMonths(1).month();
Date targetDate;
Boolean stop = false;
Integer i=7;
while(stop == false){
  targetDate = d.addDays(i);
  if(targetDate.month()==nextMonth) stop = true;
  i = i + 7;
}
System.debug(targetDate);

 
This was selected as the best answer
MM SaikumarMM Saikumar
Hi Sravanthi,
Check this. It's working 
datetime myDate = date.today(); // input date as today.
String formatted1 = myDate.formatGMT('EEEEEEE');
datetime nextmonth=mydate.addmonths(1);
datetime firstDate = date.valueof(nextmonth).toStartOfMonth();
String formatted2 = firstDate.formatGMT('EEEEEEE');
    dateTime d=firstDate;
for(integer i=0;i<7;i++){
    d=firstDate.addDays(i);
    if((myDate.formatGMT('EEEEEEE'))==(d.formatGMT('EEEEEEE'))){
       system.debug('First day '+d);
       }
}

 
sravanthi k 4sravanthi k 4
Thanks both of  you :)