Displaying the Time in AM/PM format in android

后端 未结 8 601
醉梦人生
醉梦人生 2020-12-10 13:27

I am getting the time using a time picker and displaying the time in the text view using following code...

private TimePickerDialog.OnTimeSetListener mTimeSe         


        
相关标签:
8条回答
  • 2020-12-10 13:53
    Calendar now = Calendar.getInstance();
    if(now.get(Calendar.AM_PM) == Calendar.AM){
       // AM
       System.out.println(""+now.get(Calendar.HOUR)+":AM");
    }else{
       // PM
       System.out.println(""+now.get(Calendar.HOUR)+":PM");
    }
    
    0 讨论(0)
  • 2020-12-10 14:07

    I have done like this to achieve it.

    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            myCalendar.set(Calendar.MINUTE, minute);
            update_From_Time();
        }
    };
    
    DateFormat frmTime=DateFormat.getTimeInstance();
    protected void update_From_Time() {
        s = frmTime.format(myCalendar.getTime());
        btnFromTime.setText(s);
    }
    
    0 讨论(0)
提交回复
热议问题