java--determine if the date is before today

Source: Internet
Author: User

The date mentioned here refers to the date format of the string, such as "2014-10-15", we want to determine whether this date before today, the Internet to see a lot of judgments on the way, is to take this date into a Date object and then compare with the new date (), using Comparetto () or before () method, in fact, this will be a little bit of a problem, so ignore the one-hour minute comparison, there will be errors, not the correct results.

If you want to get the correct results, you should convert the current time new date () to the format "YYYY-MM-DD", and then judge the two by converting them to date, the code is as follows:

Java.util.Date nowdate = new Java.util.Date (); String date = Dateutils.formatdate (Nowdate, "yyyy-mm-dd"); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd", Locale.china);D ate d;try {d = sdf.parse (dateformat); int flag = d . CompareTo (dateutils.formatdatefromestring (date)), if (flag >= 0) {//the same day and after the day, <0 is before the date}} catch (ParseException E {//TODO auto-generated catch Blocke.printstacktrace ();}






java--determine if the date is before today

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.