0

I am trying to display the time that a user enters, either in a toast or using a basic println command. If the time is in the correct format, I want to enter it in a database along with the current time.

Here is what I have so far:

public void addListenerOnButton ()
{
    spinner_hours = (Spinner)findViewById(R.id.hours);
    spinner_minutes = (Spinner)findViewById(R.id.minutes);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
          public void onClick(View v) {
            Date time;
            String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+String.valueOf(spinner_minutes.getSelectedItem())+"00";
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

            try {
                time= format.parse(enteredTime);
                System.out.println(enteredTime);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        //   Toast.makeText(MainActivity.this,
        //              "Your alarm has been set for : " + time,
        //                  Toast.LENGTH_SHORT).show();

          }
    });
}

If I put the toast (commented below) inside the try block, it doesnt show up when I run the app. If I put a system.out.println it does not print anything. How do I print the entered time?

Also, I want to enter the current time in the same format ("HH:mm:ss") in the database. Any ideas on how to do that?

2 Answers2

1

It's probably throwing an exception, since System.out.println should work. Try replacing this:

String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+String.valueOf(spinner_minutes.getSelectedItem())+"00";

by this:

String enteredTime= String.valueOf(spinner_hours.getSelectedItem())+ ":" + String.valueOf(spinner_minutes.getSelectedItem())+":00";

It seems you're missing the : between hour and minutes and before 00 and the parsing is failing.

Flávio Faria
  • 6,575
  • 3
  • 39
  • 59
  • I took your suggestion and included the Log.d function to check the result on LogCat on my Eclipse. The LogCat is blank. I don't know what I'm doing wrong. This is my code now – uttara_shekar Dec 07 '12 at 21:27
  • try { time= format.parse(enteredTime); System.out.println(enteredTime); Log.d("Hello",enteredTime); } – uttara_shekar Dec 07 '12 at 21:28
  • Put println and Log.d before format.parse and check logcat. Then edit your question and paste the output. – Flávio Faria Dec 07 '12 at 21:32
  • Okay, I got it. Do you know how I can display it on a Toast? The toast does not work inside my try block. – uttara_shekar Dec 07 '12 at 21:39
  • Yes. Okay, the toast works. But I want the time from the given Date variable time. In the same format. That is, if my spinners have a 00 hours and 03 minutes, and I click the send button, I want my toast to display 00:03:00. But not the string, the date in which I store it. How do I do this? – uttara_shekar Dec 07 '12 at 21:51
0

First try to print the value got from the Spinner

 System.out.println(spinner_hours.getSelectedItem().toString());
 System.out.println(spinner_minutes.getSelectedItem().toString());

If this doesn't has any value then the problem is here.

vinothp
  • 9,939
  • 19
  • 61
  • 103