Java -- determine whether the date is before today

Source: Internet
Author: User

Java -- determine whether the date is before today

The date mentioned here refers to the date format of the string, for example, 2014-10-15. We need to determine whether the date is before today. There are many judgment methods on the Internet, convert the Date To the Date object and compare it with the new Date () method. Using the comparet To () or before () method will actually be a small problem, if the comparison is ignored for an hour, errors may occur and incorrect results may not be obtained.

If you want to get the correct result, convert the current time new Date () to the format of "yyyy-MM-dd", and then convert the two to date for determination. 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); Date d; try {d = sdf. parse (dateFormat); int flag = d. compareTo (DateUtils. formatDateFromeString (date); if (flag> = 0) {// catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();}






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.