我的XML文件中有EditText
:
<EditText
android:id="@+id/etExitHour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:ems="10"
android:inputType="time"
android:hint="exit hour"/>
如何获得用户输入的时间?
答案 0 :(得分:2)
String str = editText.getText().toString();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(str);
答案 1 :(得分:1)
试试这个:
EditText etExitHouret = (EditText) findViewById(R.id.etExitHour);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // Make sure user insert date into edittext in this format.
Date dateObject;
String date;
String time;
try {
String dob_var=(etExitHouret.getText().toString());
dateObject = formatter.parse(dob_var);
date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
time = new SimpleDateFormat("h:mmaa").format(dateObject);
Toast.makeText(getBaseContext(), date + time, Toast.LENGTH_LONG).show();
}
catch (java.text.ParseException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
以下代码将帮助您
String string = editText.getText().toString();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(string);