Current location - Quotes Website - Collection of slogans - Oracle interface number
Oracle interface number
The idea is to calculate the millisecond difference between two dates first, and then calculate the difference of days, hours and minutes according to the milliseconds of each day, hour and minute respectively. The specific code is as follows:

Public static string getDatePoor(Date endDate, date nowdate) (

long nd = 1000 * 24 * 60 * 60; //Milliseconds per day

long NH = 1000 * 60 * 60; //Milliseconds per hour

Length nm =1000 * 60; //Milliseconds per minute

long diff = end date . gettime()-now date . gettime(); ? //Get the millisecond time difference between two times.

Long day = diff/nd;; //Calculate how many days the difference is.

Length and hour = diff% nd/NH; ? //Calculate how many hours the difference is.

Long Min = difference% nd% NH/nm; ? //Calculate how many minutes the difference is.

Return day+ day +hour+ hour +min+ minute;

}

Then do a test and call this method to test it:

It can be seen that the difference between two dates and times can be calculated in days, hours and minutes.

Extended data:

Java uses three methods to compare two dates:

1. Use the getTime () method to get two dates (milliseconds since 1970 65438+ 10/), and then compare the two values.

2. Use before (), after () and equals () methods. For example, if the number 12 is earlier than the number 18 within one month, then newdate (99 99,2, 12). Before (newdate (99 99,2, 18)) returns true.

3. Use the compareTo () method, which is defined by the Comparable interface and implemented by the Date class.

References:

Oracle official API interface -Util-Date

Oracle official API interface-simple date format

Beginner tutorial -Java date and time