Equal date judgment

Source: Internet
Author: User

Judging the date of all evil...
Use
D1.getTime () = d2.getTime ();
(Provided that the year, month, day, hour, and minute of the Date have been specified at the time of new Date (xxxxxxx)
I didn't expect the result of loop 100 times to be 2-5 times. The returned value is false.
After carefully following the data value of d1.getTime (), the last millisecond count is different each time. Does jdk return the current millisecond count when the millisecond value is not specified? No way. It's hard to track data in milliseconds...

Java code
Public static Boolean compareDate (Date d1, Date d2 ){
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String s1 = sdf. format (d1 );
String s2 = sdf. format (d2 );
If (s1.equals (s2) return true;
Else return false;
}


We had to use the string method for comparison! It is estimated that this item will be assigned a value of 0 in milliseconds, so there should be no problem, but for precision, it only takes the year: Month: Day, the demand for a small loop range, so we have to compare the strings!

If you can see that there are still good solutions, please kindly advise!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.