devxlogo

RE: Date Arithmatic in Java

RE: Date Arithmatic in Java

Question:
I previously asked a question that may not have been worded as clearlyas possible and you answered something slightly different from what Iwas looking for. I want to know how to subtract one date from anotheras in the following pseudo-code:

calender1.setTime(databasedate)calender2.setTime(todaysdate)differenceindays = calender2 - calender1

How can I determine the difference in days between two Calendar dates?

Answer:
To do this you must convert your Calendar times to Date objects withCalendar.getTime(). Then convert your Date objects to longsrepresenting the number of milliseconds since midnight January 1,1970. Date.getTime() will do this. Now subtract the two times anddivide by the number of milliseconds in a day.

See also  5 Ways to Improve Customer Experience
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist