user1352057
user1352057

Reputation: 3182

Setting a time picker date

EDIT:

Error trying to set hour and minute values on the calender object.

I'm trying to set the time and minute values of a time picker on my app.

I have the time value stored as a string in my database. I have parsed this value to a Date object and now I'm not exactly sure how I can get the hour and minute value to set the picker itself.

I.e if the time was stored at 8.30 PM, I want to set this on the time picker through the onCreate method of my class.

Here's my code so far:

     DateFormat newFormat = new SimpleDateFormat("hh:mm");
     try 
    {
    dateToEdit = newFormat.parse(returnedTime);
    } catch (ParseException e)
    {

        e.printStackTrace();
    }     


     Calendar cal = Calendar.getInstance();
     cal.setTime(dateToEdit);
     editTimePicker.setCurrentHour(Calendar.HOUR_OF_DAY);
     editTimePicker.setCurrentMinute(Calendar.MINUTE);

Log cat showing errors

01-29 21:58:47.860: D/AndroidRuntime(269): Shutting down VM
01-29 21:58:47.870: W/dalvikvm(269): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-29 21:58:47.890: E/AndroidRuntime(269): FATAL EXCEPTION: main
01-29 21:58:47.890: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.AppointmentEditChanges}: java.lang.NullPointerException
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.os.Looper.loop(Looper.java:123)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-29 21:58:47.890: E/AndroidRuntime(269):  at java.lang.reflect.Method.invokeNative(Native Method)
01-29 21:58:47.890: E/AndroidRuntime(269):  at java.lang.reflect.Method.invoke(Method.java:521)
01-29 21:58:47.890: E/AndroidRuntime(269):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-29 21:58:47.890: E/AndroidRuntime(269):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-29 21:58:47.890: E/AndroidRuntime(269):  at dalvik.system.NativeStart.main(Native Method)
01-29 21:58:47.890: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
01-29 21:58:47.890: E/AndroidRuntime(269):  at com.example.flybase2.AppointmentEditChanges.onCreate(AppointmentEditChanges.java:100)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 21:58:47.890: E/AndroidRuntime(269):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

Upvotes: 2

Views: 937

Answers (2)

Khantahr
Khantahr

Reputation: 8518

You can use a Calendar to get the time out of the date object:

Calendar cal = Calendar.getInstance();
cal.setTime( dateToEdit );
timePicker.setCurrentHour( cal.get( HOUR_OF_DAY ) );
timePicker.setCurrentMinute( cal.get( MINUTE ) );

You'll have to create timePicker before doing the above, either by

timePicker = (TimePicker)findViewById( R.id.my_time_picker );

or

timePicker = new TimePicker( this );

in the second case, you'll then have to add it to your layout somehow.

Upvotes: 1

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Don't store your dates and times as strings in your database (don't forget timezones, ...).

Instead, store the number of milliseconds since the epoch in the GMT timezone (long value).

Such time can easily obtained by Date.getTime()

You can then use that again to build your dates or calendars again:

Calendar cal = new GregorianCalendar(myTimeZone);
cal.setTimeInMillis(myDbTime);

I also strongly advise you to leave alone java.util.Date and other java.util.Calendar in favor of Joda Time.

Upvotes: 0

Related Questions