How would I go about getting the first day of the month? So for January, of this year, it would return Sunday. And then for February it would return Wednesday.
We can use the EOMONTH formula to find the first day of the month as well. EOMONTH returns the last day of a month from a date. Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month.
To get the first date of the current month, use java.util.Calendar
. First get an instance of it and set the field Calendar.DAY_OF_MONTH
to the first date of the month. Since the first day of any month is 1, inplace of cal.getActualMinimum(Calendar.DAY_OF_MONTH)
, 1 can be used here.
private Date getFirstDateOfCurrentMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return cal.getTime(); }
You can create a Calendar
with whatever date you want and then do set(Calendar.DAY_OF_MONTH, 1)
to get the first day of a month.
Calendar cal = Calendar.getInstance(); cal.set(Calendar.DATE, 25); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.YEAR, 2012); cal.set(Calendar.DAY_OF_MONTH, 1); Date firstDayOfMonth = cal.getTime(); DateFormat sdf = new SimpleDateFormat("EEEEEEEE"); System.out.println("First Day of Month: " + sdf.format(firstDayOfMonth));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With