Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of the first day of a month?

Tags:

java

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.

like image 293
Austin Donovan Avatar asked Jan 25 '12 02:01

Austin Donovan


People also ask

How do you get the first day of the month?

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.


2 Answers

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(); } 
like image 50
Nadeeshani Avatar answered Sep 23 '22 21:09

Nadeeshani


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));   
like image 41
Kushan Avatar answered Sep 21 '22 21:09

Kushan