2

In my layout I have an EditText with InputType of time.

android:inputType="time"

How can I convert this to long?

I have a function that will convert from long to string using SimpleDateFormat, but I now need to do the opposite.

Latcie
  • 701
  • 1
  • 7
  • 21

1 Answers1

2

Something like this should do it:

EditText editText = (EditText) findViewById(R.id.edit_text);
String value = editText.getText().toString();

try {
    DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    Date date = (Date) formatter.parse(value);
    long timeMilliSeconds = date.getTime();
}
catch (NumberFormatException e) {
    System.out.println("Error getting value: " + e);
}
Adeel
  • 2,901
  • 7
  • 24
  • 34
  • I think time is in the format of hh:mm:ss, and when you try to convert that to long, things fall apart. – Latcie Apr 08 '13 at 03:09
  • Please see edit, adapted from: http://stackoverflow.com/questions/4584994/how-to-convert-string-into-time-object-in-java –  Apr 08 '13 at 08:02