99maas
99maas

Reputation: 1267

How to set day of the month by default to 25

I need your help in setting the default value of the day of the month to 25. In the method, I am getting the previous seven months from today's date, however I need to get the previous seven months from today's date bu the day should be by default as 25. So how can I achieve this. The method code is:

 public Date getMinCalendarDate() {

    Calendar cal = Calendar.getInstance();
    cal.setTime(minCalendarDate);
    cal.add(Calendar.MONTH, -7);
    minCalendarDate = cal.getTime();

    return minCalendarDate;
}

Upvotes: 0

Views: 116

Answers (1)

swinkler
swinkler

Reputation: 1701

Here is a working example:

import java.util.Calendar;
import java.util.Date;

public class Example {
  public static void main(String[] args) {
    System.out.println(getMinCalendarDate());
  }

  public static Date getMinCalendarDate() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, -7);
    // set the day of month to the 25th
    cal.set(Calendar.DAY_OF_MONTH, 25);
    return cal.getTime();
  }
}

Upvotes: 1

Related Questions