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();
Related
This question already has answers here:
Calculate number of weekdays between two dates in Java
(20 answers)
How to get every day except weekend or Saturday or Sunday between two dates in java?
(4 answers)
Closed 4 years ago.
if user selected the start and end date of leave of someone then I must to get the sum of days of weekend : sum of "Sturday" and "Sunday" days
You can use the DateTimeFormatter class docs here
When you're using the date format object to leave out the weekend you can check for weekends from the attribute "E" (Day number of week) which is returned as an Integer. (please see the link posted). It unclear what your goal here with the dates, But it seems you are having problems knowing when a certain day is a weekend or not so I hope this helps.
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:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
I am trying to remove the entries in my database that have Date (java.util.Date) older than 10 days of the current date. Is there any way to compare just the "day" value inside the Date. Not just comparing which Date value is greater, but actually making sure there is X day in between the two Date values.
Say you have your two Date values as time1 and time2.
int daysApart = (int)((time2.getTime() - time1.getTime()) / (1000*60*60*24l));
if (abs(daysApart) >= 10)
System.out.println("10+ days apart.");
else
System.out.println("Less than 10 days apart.");
Just create a new Date object for current date + 10 days and compare to that.
i.e.
new Date(System.currentTimeMillis()+10L*24*60*60*1000);
As the comments say though the Date objects are pretty much obsolete now, you should look to using one of the more current time approaches.
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();