Check for time format in Android

Source: Internet
Author: User

public class Mainactivity extends Activity implements onclicklistener{

Private Button btn1;
Private EditText edit;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
}

/**
* Component Initialization
*/
private void Initview () {
Btn1 = (Button) Findviewbyid (R.ID.BTN1);
Edit = (EditText) Findviewbyid (R.id.edit);
Btn1.setonclicklistener (this);
}

/**
* Determines whether a string is a date format
* @param date
* @return
*/
Public boolean isdatestringvalid (String dat e) {
SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd");
The input object is not empty
try {
Sdf.parse (date);
return true;
} catch (Java.text.ParseException e) {
return false;
}
}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.ID.BTN1:
String str = Edit.gettext (). toString (). Trim ();
if (Isdatestringvalid (str)) {

if (Str.length () ==10) {//date format must be 10-bit example: 2014-02-24
Toast.maketext (Mainactivity.this, "is date format", Toast.length_short). Show ();
} else {
Toast.maketext (Mainactivity.this, "Not date format", Toast.length_short). Show ();
}
} else {
Toast.maketext (Mainactivity.this, "Not date format", Toast.length_short). Show ();
}
Break
}
}

}

Check for time format in Android

Related Article

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.