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
Mangi S V V Satya Surya Sravan KumMangi S V V Satya Surya Sravan Kum 

Hey, please help me to find first day and last day of a given month by user in apex

Input : If  06 (June) is the Given Month
Ouput: 2021-06-01 // First Day
            2021-06-30 ?/ Last Day

 
CharuDuttCharuDutt
Hii Mangi
Try Below Code
Date firstDayOfMonth = System.today().toStartOfMonth();
Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);
 Datetime dt = DateTime.newInstance(firstDayOfMonth, Time.newInstance(0, 0, 0, 0));
 Datetime dt2= DateTime.newInstance(lastDayOfMonth, Time.newInstance(0, 0, 0, 0));      
String firstdayOfWeek=dt.format('EEEE');
String lastdayOfWeek2=dt2.format('EEEE');
        System.debug('Day : ' + firstdayOfWeek);
        System.debug('Day : ' + lastdayOfWeek2);
Please Mark It As Best Answer If it Helps Thank You!
 
Mangi S V V Satya Surya Sravan KumMangi S V V Satya Surya Sravan Kum
Hi Charu,
Thanks For Replying,
But User need to define month not System default like any month in year
Please help me
CharuDuttCharuDutt
Hii Mangi
try Below Code
Date firstDayOfMonth = Date.newInstance(2021, 6, 14).toStartOfMonth();
Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);
 Datetime dt = DateTime.newInstance(firstDayOfMonth, Time.newInstance(0, 0, 0, 0));
Datetime dt2= DateTime.newInstance(LastDayOfMonth, Time.newInstance(0, 0, 0, 0));      
String dayOfWeek=dt.format('EEEE');
String dayOfWeek2=dt2.format('EEEE');
        System.debug('Day : ' + dayOfWeek);
        System.debug('Day : ' + dayOfWeek2);
Please Mark It As Best Answer If it Helps Thank You!
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Mangi,
You can get a reference from this:
Integer numberOfDays = yourDateVariable.daysInMonth(dateField.year(), dateField.month());
Date lastDayOfMonth = yourDateVariable.newInstance(dateField.year(), dateField.month(), numberOfDays);
Or
Date lastDayOfMonth = yourDateVariable.addMonths(1).toStartofMonth().addDays(-1).
if you have any query please comment.
---------------
If you find your Solution then mark this as the best answer to close this question

Thank you!
Regards,
Suraj Tripathi
CharuDuttCharuDutt
Hii Mangi
Try Below Code
integer Month= 6;
Date firstDayOfMonth = Date.newInstance(2021, Month, 14).toStartOfMonth();
Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);
 Datetime dt = DateTime.newInstance(firstDayOfMonth, Time.newInstance(0, 0, 0, 0));
Datetime dt2= DateTime.newInstance(LastDayOfMonth, Time.newInstance(0, 0, 0, 0));      
String dayOfWeek=dt.format('EEEE');
String dayOfWeek2=dt2.format('EEEE');
        System.debug('Day : ' + dayOfWeek);
        System.debug('Day : ' + dayOfWeek2);
Please Mark It As Best Answer If it Helps Thank You!
 

 
ravi soniravi soni
Hi,
try following code.
Date firstDayOfMonth = System.today().toStartOfMonth();
 string sDate = firstDayOfMonth.year()+ '-' + firstDayOfMonth.month() + '-' + firstDayOfMonth.day();
  System.debug('sDate-----'+sDate);

 Date lastDayOfMonth = System.today().addMonths(1).toStartofMonth().addDays(-1); 
 string lDate = lastDayOfMonth.year()+ '-' + lastDayOfMonth.month() + '-' + lastDayOfMonth.day();
  System.debug('lDate-----'+lDate);
Output : => sDate-----2021-6-1
                   lDate-----2021-6-30

let me know if it helps you by marking it as best answer.
Thank you
mukesh guptamukesh gupta
Hi Mangi,

Just simple, Use below code 
 
public void getDate(Integer Month){
    if(Month > 0 && Month<= 12){
       Date startDtOfMonth= Date.newInstance(2021, Month, 14).toStartOfMonth();
      Date  lastDtOfMonth= startDtOfMonth.addDays(Date.daysInMonth(startDtOfMonth.year(), startDtOfMonth.month()) - 1);
 Datetime dt = DateTime.newInstance(startDtOfMonth, Time.newInstance(0, 0, 0, 0));
Datetime dt2= DateTime.newInstance(lastDtOfMonth, Time.newInstance(0, 0, 0, 0));      
    
    System.debug('firstDayOfMonth--> '+startDtOfMonth);
    System.debug('lastDayOfMonth--> '+lastDtOfMonth);
 
    }

}

getDate(6);

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh