Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

date

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?

like image 688
Gnaniyar Zubair Avatar asked Jun 11 '09 05:06

Gnaniyar Zubair


People also ask

How do you check if a date is older than 30 days in Java?

1. Java 8 isBefore() First minus the current date and then uses isBefore() to check if a date is a certain period older than the current date.

How do I add 30 days to a specific date in Java?

Adding Days to the given Date using Calendar class Add the given date to the calendar by using setTime() method of calendar class. Use the add() method of the calendar class to add days to the date. The add method() takes two parameter, i.e., calendar field and amount of time that needs to be added.

How can I compare two dates in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you calculate months in Java?

yearsBetween() method to calcualte the number of months and years between two dates in Java. LocalDate jamesBirthDay = new LocalDate(1955, 5, 19); LocalDate now = new LocalDate(2015, 7, 30); int monthsBetween = Months.


1 Answers

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(); 
like image 107
cletus Avatar answered Sep 27 '22 20:09

cletus