Java, a good way to validate the format and validity of date and time. __java

Source: Internet
Author: User

The project needs to validate the format and validity of a field in which the user enters a date with time on the interface. I Google a bit, indeed there are strong people to achieve, including the special circumstances of the leap month, but, because the user is not the same requirements, so if the change of the code of the person is very troublesome. I used another way to adapt to achieve this function, for your reference

For example, the input date time format on the interface is: dd.mm.yyyy hh:mm:ss "2007-02-29 01:00:00"

dd.mm.yyyy hh:mm:ss "2007-02-29 01:00:00"
private int Checkdateformatandvalite (String strdatetime) ... {
Update it according to your requirement.
SimpleDateFormat format = new SimpleDateFormat ("DD. Mm.yyyy HH:mm:ss ");
Try ... {
Date ndate = Format.parse (strdatetime);
String str = Format.format (ndate);
System.out.println (ndate);
System.out.println (str);
System.out.println ("strdatetime=" + strdatetime);
Success
if (Str.equals (strdatetime))
return 1;
DateTime is not validate
Else
return 0;
catch (Exception e) ... {
E.printstacktrace ();
Format error
return-1;
}
}

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.