How to set TimePicker show with format 24h

AndroidAndroid Layout

Android Problem Overview


I've created a TimePicker in layout and I want it to show time with format 24h.

Can you help me? Thanks.

Android Solutions


Solution 1 - Android

TimePicker is a view for selecting the time of day, in either 24 hour or AM/PM mode. You can use setIs24HourView(true) method with TimePicker.

Solution 2 - Android

Setting TimePicker by default to 24 hour mode is not always a good idea since many countrys have diffrent convention. For this case use DateFormat.is24HourFormat(getActivity()).

Your final code migth look like picker.setIs24HourView(DateFormat.is24HourFormat(this));.

Further details see:

Solution 3 - Android

If you want the TimePicker to be correctly initialized with the current time in 24h format use the following:

import java.util.Calendar;

timePicker.setIs24HourView(true);
timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));

Otherwise, due to Android bug, the picker will start with an incorrect hour (2 instead of 14 etc).

Solution 4 - Android

You just have to retrieve the TimePicker instance from the view after inflating it, then you can modify the widget.

Ids are arbitrary:

View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
timePicker.setIs24HourView(true);

Solution 5 - Android

xml

<TimePicker
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/timePicker" />

java

TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);    
timePicker.setIs24HourView(true); 

Solution 6 - Android

By default, it displays time in the AM/PM format. If you want to change time in the 24 hour format, then you can use the setIs24HourView() method. See this link: http://www.androidaspect.com/2012/06/timepicker-view-tutorial.html

Solution 7 - Android

I need the same just for an application to set the duration spend for doing a task. I found that I just need to extend the TimePickerDialog class and pass True in the last constructor parameter boolean is24HourView.

public class DurationPickerDialog extends TimePickerDialog {

public DurationPickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute){
    super(context, callBack, hourOfDay, minute, true);
}

Then you only need to create this as

TimePickerDialog durationPicker =  new DurationPickerDialog(getActivity(), this, hour, minute);
durationPicker.setTitle("Duration:");

Solution 8 - Android

You just need call and send true to setIs24HourView function.

TimePicker tpHourMin = (TimePicker) findViewById(R.id.timePicker);
tpHourMin.setIs24HourView(true);

Solution 9 - Android

As some people have already mentioned just set the parameter to true or false.

timePicker.setIs24HourView(true);

You can find a complete example with source code to download from here http://www.ahotbrew.com/android-timepicker-example/

Solution 10 - Android

Or you let the user decide for himself which format he want to use.

In your MainActivty:

DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");


   @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            TextView outputText= (TextView)findViewById(R.id.outputText);
            outputText.setText(hourOfDay + " : "+ minute);
}

And in a new Java Class (TimePickerFragment):

public class TimePickerFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);



    return new TimePickerDialog(getActivity(),(TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));

}}

Solution 11 - Android

For Time picker Dialog

Timepicker dialog provides by android to select the time.

Java provides default functionality by passing is24HourView == true in TimePickerDialog constructor.

public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,boolean is24HourView) {
        this(context, 0, listener, hourOfDay, minute, is24HourView);
    }

Pass true for 24 hours or pass false to set time picker dialog in AM-PM format.

for ex: (Kotlin)

 private fun showTimePicker() {
        val cal = Calendar.getInstance()
        val dialog = TimePickerDialog(this, TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
            Log.e("Time :","$hourOfDay:$minute")
        }, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true) //here pass true for 24hrs view else false for AM-PM(12hrs view)
        dialog.show()
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmumView Question on Stackoverflow
Solution 1 - AndroidVendettaDroidView Answer on Stackoverflow
Solution 2 - AndroidBREMIView Answer on Stackoverflow
Solution 3 - AndroidJan WrobelView Answer on Stackoverflow
Solution 4 - AndroidDaniel SusinView Answer on Stackoverflow
Solution 5 - Androidreza.cse08View Answer on Stackoverflow
Solution 6 - Androiduser1357696View Answer on Stackoverflow
Solution 7 - AndroidHernan RamovecchiView Answer on Stackoverflow
Solution 8 - AndroidHosseinView Answer on Stackoverflow
Solution 9 - AndroidGurinder SinghView Answer on Stackoverflow
Solution 10 - AndroidTheOmegaDogView Answer on Stackoverflow
Solution 11 - AndroidAshav KothariView Answer on Stackoverflow