calculate months between two dates in java [duplicate] - java

This question already has answers here:
Java Date month difference
(22 answers)
Closed 9 years ago.
i need to calculate months between two dates,
if the startDate=2013.01.01, endDate=2013.01.31 the answer should be 1,
startDate=2013.01.01, endDate=2013.02.01 the answer should be 2.
please help

Use Joda Months:
DateTime start = new DateTime(startDate.getTime());
DateTime end= new DateTime(endDate.getTime());
int months = Months.monthBetween(start, end).getMonths();

Related

i want to get difference of days by subtracting dates in java using Gregorian calendar: [duplicate]

This question already has answers here:
Calculating difference in dates in Java
(7 answers)
Closed 6 years ago.
GregorianCalendar last = new GregorianCalendar(py,pm-1,pd);
System.out.println(last.getTime());
GregorianCalendar present = new GregorianCalendar(py,pm-1,pd);
System.out.println(present.getTime());
long diff = Math.abs(prsent.getTime() - last.getTime());
Problem facing when subtracting these two dates
ChronoUnit is what you can use for the difference between dates:
long minutesDiff = ChronoUnit.MINUTES.between(present.toInstant(), last.toInstant());

Java - get Monday date of a week give a date in that week [duplicate]

This question already has answers here:
How in Java find dates for previous 2 mondays?
(3 answers)
Closed 6 years ago.
I got a date YYYY-MM-DD how can I get the Monday date of that date whatever the given date is?
Edited: got the answer from How in Java find dates for previous 2 mondays?.
LocalDate monday =
localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);

How to get current time+n months in java? [duplicate]

This question already has answers here:
How do I add one month to current date in Java?
(18 answers)
Closed 7 years ago.
I have searched a lot on internet about how to get current time in Java but what I am looking for is how to get current time + n months?
You can try like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, numberofMonths);
If you can use Java 8, use LocalDate.plusMonths()
LocalDate localDate = LocalDate.now();
localDate.plusMonths(numberOfMonths);

Difference between two dates in java using joda API [duplicate]

This question already has answers here:
Calculate month difference in Joda Time
(3 answers)
Closed 8 years ago.
I have two dates date1 and date2, i need to get number of months between those two dates,
can any one help me to do.
int result = Months.monthsBetween(date1, date2).getValue();

How to get the 30 days back date in java based on given date [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 9 years ago.
I have an input field, where i can select the date from date picker.Based on this selected value i need to get the 30 days back date.
Thanks in advance.
Calendar cal = Calendar.getInstance();
cale.add(Calendar.DATE, -30);
System.out.println("Date = " + cal.getTime());

Categories

Resources