Search code examples
androidtimepicker

Android : Time picker get Date in 24 hours Format


I am using Time Picker Toolbar in my project and I'm facing this problem: getting the time in Am-Pm Format always returns me the hours in 12h format.

How i can get time in 24 hours Format?

This is the code I'm using:

TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);
int hours= timePicker.getCurrentHour();

e.g if i set time in time picker as 1:00 pm the current code will give me 1, but i want the output 13


Solution

  • By Using this trick we can get Hours in 24 Hours Format through Time picker:

    Here is the Code

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_profile);
            TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);
            timePicker.setIs24HourView(true);//set Timer to 24 hours Format
            timePicker.setCurrentHour(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
            int hours= timePicker.getCurrentHour();//get Time in 24 Hours using time Picker
    
    
    
        }