How to Get the First Date and Last Date of the Previous Month (Java)

Get first and last date of the previous month

Use getActualMaximum()

Calendar aCalendar = Calendar.getInstance();
// add -1 month to current month
aCalendar.add(Calendar.MONTH, -1);
// set DATE to 1, so first date of previous month
aCalendar.set(Calendar.DATE, 1);

Date firstDateOfPreviousMonth = aCalendar.getTime();

// set actual maximum date of previous month
aCalendar.set(Calendar.DATE, aCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//read it
Date lastDateOfPreviousMonth = aCalendar.getTime();

find first and last date of the last month

If you want to get the month before the current date then use the below code:

LocalDate today = LocalDate.now(ZoneOffset.UTC);  // Retrieve the date now
LocalDate lastMonth = today.minus(1, ChronoUnit.MONTHS); // Retrieve the date a month from now
System.out.println("First day: " + lastMonth.withDayOfMonth(1)); // retrieve the first date
System.out.println("Last day: " + lastMonth.withDayOfMonth(lastMonth.lengthOfMonth())); // retrieve the last date

Get previous months first and last date

So I need the first and last date of previous month to be able to make the "between" SQL query.

You can do that computation directly in your database. In Oracle, you can get the first and last day of the previous month as dates like so:

where mydate between add_months(trunc(sysdate, 'month'), -1)
and last_day(add_months(trunc(sysdate, 'month'), -1))

In general, however, you don't really need between and the last day. You can use half-open intervals - which is also safer, as it takes in account dates that belong to the last day of the month:

where mydate >= add_months(trunc(sysdate, 'month'), -1)
and mydate < trunc(sysdate, 'month')

How to get last three months first date and last date based on current date including current date in java?

Use java.util.Calendar for addition and subraction in date

and

java.text.DateFormat to format date

    DateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
DateFormat monthFormat = new SimpleDateFormat("MM-yyyy", Locale.US);
Calendar cal = Calendar.getInstance();
cal.setTime(format.parse("26-04-2019"));
for (int i = 0; i < 3; i++){
System.out.println("currentDate: " + monthFormat.format(cal.getTime())); // print current month

cal.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("first date: " + format.format(cal.getTime())); // print first date

cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DATE, -1);

System.out.println("last date: " + format.format(cal.getTime())); // print last date


cal.add(Calendar.MONTH, -1);
}

java get the first date and last date of given month and given year

To get the Start Date

    GregorianCalendar gc = new GregorianCalendar(year, month-1, 1);
java.util.Date monthEndDate = new java.util.Date(gc.getTime().getTime());
System.out.println(monthEndDate);

(Note : in the Start date the day =1)

for the formatted

SimpleDateFormat format = new SimpleDateFormat(/////////add your format here);
System.out.println("Calculated month end date : " + format.format(calculatedDate));

java - Start Date of Last Month

You should never do math on field constants such as Calendar.DAY_OF_MONTH. What you want to do is:

Calendar calendar = getCalendar(new Date());
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar = getTimeToBeginningOfDay(calendar);

return calendar.getTime();

Calendar - Get last day of previous month

The problem in your current code is that you are calling multiple times the Calendar.getInstance() method, which returns the current date.

To obtain a Calendar which is the last day of the previous month, you can have the following:

public static void main(String... args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
}

It subtracts one month from the current month and sets the day of month to its maximum value, obtained with getActualMaximum. Note that the month is 0-based in Calendar so January is actually 0.

How to get next month start date and end date if current month is february?

Try this:

Calendar calendar = Calendar.getInstance();         
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date nextMonthFirstDay = calendar.getTime();
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();


Related Topics



Leave a reply



Submit