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();
Related
This question already has answers here:
In Java, how do I get the difference in seconds between 2 dates?
(13 answers)
Java 8: Difference between two LocalDateTime in multiple units
(11 answers)
How to calculate time difference between two dates using LocalDateTime in Java 8?
(2 answers)
Closed 2 years ago.
I'm stuck between java.time.temporal.ChronoUnit and java.time.temporal.Temporal until. How do they work??
Look at the javadoc for ChronoUnit and TemporalUntit.
The ChronoUnit enum basically provides some standard TemporalUnits but other developers could implement other TemporalUnits.
For example, you can write the following code:
TemporalUnit u=ChronoUnit.DAYS;
but you can also create new TemporalUnits by extending ChronoUnit.
This question already has answers here:
How to get list of dates from some date? [duplicate]
(3 answers)
Closed 4 years ago.
I used the Calendar.getInstance().get(Calendar.DATE) to get current date. So the output is today (2018/05/14).
I change the device time to 2018/06/17. So I use the Calendar get the date. I got the 17.
My question is I want to get 05/15 05/16 .... 06/01 ... 0616 these two days. I need to know all the difference days.
So I don't know which function can do this.
private static long daysBetween(Date one, Date two) {
long difference = (one.getTime()-two.getTime())/86400000;
return Math.abs(difference);
}
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());
This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 6 years ago.
I have 2 Calendar objects for the current date and birthdate.
I need to subtract birthdate from the current date and return the difference in years (a truncated Int is all I need).
What's the simplest way of doing this?
You can get EPOCH time (miliseconds since 01.01.1900), subtract both values and divide it by 1000(miliseconds)/60(seconds)/60(minutes)/24(hours)/365(days).
This would give you approximate result (in years) because of ignoring leap years.
To get epoch time, use Date#getTime();
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();