AFAIK by default it will use device current time and you can set it using following code.
Calendar c = Calendar.getInstance();
timePicker1.setCurrentHour(c.get(Calendar.HOUR));
timePicker1.setCurrentMinute(c.get(Calendar.MINUTE));
質問
I have a timepicker that is in 24 hours format. Now I want to display the current device time in 24 hours format regardless of the device time format. Right now my device is in 12 hours format so for example my time is 2:04 pm the timepicker should be 14:04.
Here's my code or the timepicker. It is on a different layout because I'm displaying them on a dialog.
XML:
<LinearLayout android:id="@+id/datetimepickerLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" xmlns:android="http://schemas.android.com/apk/res/android">
<DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></DatePicker>
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></TimePicker>
<Button
android:id="@+id/setdatetimeBtn1"
android:text="Set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
Now in my java code:
timePicker1 = (TimePicker)dialog.findViewById(R.id.timePicker1);
datePicker1 = (DatePicker)dialog.findViewById(R.id.datePicker1);
setDateTimeBtn1 = (Button)dialog.findViewById(R.id.setdatetimeBtn1);
datePicker1.setCalendarViewShown(false);
timePicker1.setIs24HourView(true);
Any ideas? Thanks!
解決
AFAIK by default it will use device current time and you can set it using following code.
Calendar c = Calendar.getInstance();
timePicker1.setCurrentHour(c.get(Calendar.HOUR));
timePicker1.setCurrentMinute(c.get(Calendar.MINUTE));
他のヒント
You can get device time using Calendar instance like below:
Calendar c = Calendar.getInstance();
String CTime =c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
Try out .
timePicker1 = (TimePicker)dialog.findViewById(R.id.timePicker1);
datePicker1 = (DatePicker)dialog.findViewById(R.id.datePicker1);
setDateTimeBtn1 = (Button)dialog.findViewById(R.id.setdatetimeBtn1);
setDateTimeBtn1 .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strDateTime = datePicker1 .getYear() + "-" + (datePicker1 .getMonth() + 1) + "-" + datePicker1 .getDayOfMonth() + " "
+ timePicker1 .getCurrentHour() + ":" + timePicker1.getCurrentMinute();
Toast.makeText(TimeDate.this, "User selected " + strDateTime + "Time", Toast.LENGTH_LONG).show(); //Generate a toast only if you want
finish(); // If you want to continue on that TimeDateActivity
// If you want to go to new activity that code you can also write here
}});
@Everyone who tryed solutions mentioned above and they didnt work you have to use you
Calender.HOUR_OF_DAY
instead of
Calender.HOUR
I was googling all day long today till I got this to work.