3

I have EditText in my XML file:

   <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"/>

How can I get the time that the user enters?

N J
  • 27,217
  • 13
  • 76
  • 96
NomNom
  • 31
  • 1
  • 1
  • 2

3 Answers3

2
String str = editText.getText().toString();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(str);
Nitesh
  • 3,868
  • 1
  • 20
  • 26
1

Try This:

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();
}
Adeel
  • 2,901
  • 7
  • 24
  • 34
Suhas Bachewar
  • 1,230
  • 7
  • 21
0

this below code will help you

String string = editText.getText().toString();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(string);
Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26