Get first and last dates of month

In this article, we will take a look at how to get first and last dates in java using Calendar class in java.
This might be required to filter values based on start and end dates of a particular month.

Start date of current month
Use following steps to get start date of current month using Calendar class.
1. Get an instance of java.util.Calendar class using its static getInstance() method.
2. Set the time of this calendar instance to current date using Date constructor.
3. To get the start date or the first date of the month, set the day of month to the minimum value for that month.

To get the minimum day of a month, use getActualMinimum() method of calendar. It takes an integer representing the field for which you want the minimum value.
So, to get the first date of a month, argument to getAcutalMinimum() will be Calendar.DAY_OF_MONTH.

public static Timestamp getTimestamp() {
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(new Date());
  // set day to minimum
  calendar.set(Calendar.DAY_OF_MONTH,
  calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
  calendar.set(Calendar.HOUR_OF_DAY, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MILLISECOND, 0);
  return new Timestamp(calendar.getTimeInMillis());
}

Now, to get Date from timestamp, use another Date constructor which takes a long argument, which represents the time in milliseconds.
To get the milliseconds from timestamp, use its getTime() method as shown below.

public static Date getDateFromTimestamp(Timestamp t) {
  return new Date(t.getTime());
}

End date of current month
Use following steps to get start date of current month using Calendar class.
1. Get an instance of java.util.Calendar class using its static getInstance() method.
2. Set the time of this calendar instance to current date using Date constructor.
3. To get the start date or the first date of the month, set the day of month to the minimum value for that month.
To get the minimum day of a month, use getActualMaximum() method of calendar. It takes an integer representing the field for which you want the maximum value.
So, to get the first date of a month, argument to getAcutalMaximum() will be Calendar.DAY_OF_MONTH.

public static Timestamp getTimestamp() { 
  Calendar calendar = Calendar.getInstance(); 
  calendar.setTime(new Date()); 
  // set day to maximum
  calendar.set(Calendar.DAY_OF_MONTH, 
      calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
  calendar.set(Calendar.HOUR_OF_DAY, 0); 
  calendar.set(Calendar.MINUTE, 0); 
  calendar.set(Calendar.SECOND, 0); 
  calendar.set(Calendar.MILLISECOND, 0); 
  return new Timestamp(calendar.getTimeInMillis()); 
}

Date object can be created using its constructor as shown in earlier method.
Start and end dates of particular month
In previous sections, we saw how to get start and end dates of current month. In order to get these dates for a particular month, set the month of the calendar object to the required month.
Below code example gets the start and end dates for the month of December.

public static Timestamp getStartTimestamp(int month) { 
  Calendar calendar = Calendar.getInstance(); 
  calendar.setTime(new Date()); 
  calendar.set(Calendar.MONTH, month);
  calendar.set(Calendar.DAY_OF_MONTH, 
      calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); 
  calendar.set(Calendar.HOUR_OF_DAY, 0); 
  calendar.set(Calendar.MINUTE, 0); 
  calendar.set(Calendar.SECOND, 0); 
  calendar.set(Calendar.MILLISECOND, 0); 
  return new Timestamp(calendar.getTimeInMillis()); 
}

public static Timestamp getEndTimestamp(int month) { 
  Calendar calendar = Calendar.getInstance(); 
  calendar.setTime(new Date()); 
  calendar.set(Calendar.MONTH, month);
  calendar.set(Calendar.DAY_OF_MONTH, 
      calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
  calendar.set(Calendar.HOUR_OF_DAY, 0); 
  calendar.set(Calendar.MINUTE, 0); 
  calendar.set(Calendar.SECOND, 0); 
  calendar.set(Calendar.MILLISECOND, 0); 
  return new Timestamp(calendar.getTimeInMillis()); 
}

public static void main(String[] a) {
  Timestamp start = getStartTimestamp(11);
  Timestamp end = getEndTimestamp(11);
  System.out.println("Start date is: " + new Date(start.getTime()));
  System.out.println("End date is: " + new Date(end.getTime()));
}

Note that months in Calendar range from 0 to 11 for January to December respectively.
This prints

Start date is: Wed Dec 01 00:00:00 IST 2021
End date is: Fri Dec 31 00:00:00 IST 2021

Hope the article was useful.