Gnaniyar Zubair Gnaniyar Zubair - 1 month ago 8
Java Question

Find out Last 30 Days, 60 Days and 90 Days in java

How to get last 30 / 60 / 90 days records from given date in java?

I have some records with receivedDate. I want to fetch the records for last 30 or 60 or 90 days from received Date. How to resolve it?

Answer

Use java.util.Calendar.

Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -30);
Date today30 = cal.getTime();
cal.add(Calendar.DAY_OF_MONTH, -60);
Date today60 = cal.getTime();
cal.add(Calendar.DAY_OF_MONTH, -90);
Date today90 = cal.getTime();