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

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.


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));  

public int getFirstDay(){
    Calendar c=new GregorianCalendar();
    c.set(Calendar.DAY_OF_MONTH, 1);
    return c.get(Calendar.DAY_OF_WEEK);
}

From there you can see if the int is equal to Calendar.SUNDAY, Calendar.MONDAY, etc.


In the Java 8 you can use the TemporalAdjusters:

This is an example:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * Dates in Java8
 *
 */
public class App {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println("Day of Month: " + localDate.getDayOfMonth());
        System.out.println("Month: " + localDate.getMonth());
        System.out.println("Year: " + localDate.getYear());

        System.out.printf("first day of Month: %s%n",
                localDate.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.printf("first Monday of Month: %s%n", localDate
                .with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
        System.out.printf("last day of Month: %s%n",
                localDate.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.printf("first day of next Month: %s%n",
                localDate.with(TemporalAdjusters.firstDayOfNextMonth()));
        System.out.printf("first day of next Year: %s%n",
                localDate.with(TemporalAdjusters.firstDayOfNextYear()));
        System.out.printf("first day of Year: %s%n",
                localDate.with(TemporalAdjusters.firstDayOfYear()));

        LocalDate tomorrow = localDate.plusDays(1);
        System.out.println("Day of Month: " + tomorrow.getDayOfMonth());
        System.out.println("Month: " + tomorrow.getMonth());
        System.out.println("Year: " + tomorrow.getYear());
    }

}

The results would be:

Day of Month: 16
Month: MAY
Year: 2014
first day of Month: 2014-05-01
first Monday of Month: 2014-05-05
last day of Month: 2014-05-31
first day of next Month: 2014-06-01
first day of next Year: 2015-01-01
first day of Year: 2014-01-01
Last in Month Tuesday: 2014-05-27
Day of Month: 17
Month: MAY
Year: 2014

TemporalAdjuster

In java 8 you can use the new LocalDate and LocalTime API.

To achieve the coveted result, give a try to the following. It prints the name of the given day.

import java.time.*;
import java.time.temporal.*;

public class Main {
  public static void main(String[] args) {
    LocalDate l = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
    System.out.println(l.getDayOfWeek());
  }
}

Explanation:

  • now gives you the current date.
  • by calling with you can pass a TemporalAdjuster which are a key tool for modifying temporal objects.
  • getDayOfWeek Gets the day-of-week field, which is an enum DayOfWeek. This includes textual names of the values.

TemporalAdjuster has a brother class, called TemporalAdjusters which contains static methods regarding the adjustments, like the one you are looking for, the firstDayOfMonth