Displaying the Time in AM/PM format in android

后端 未结 8 600
醉梦人生
醉梦人生 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:44

    This worked for me:

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String am_pm = "";
    
        Calendar datetime = Calendar.getInstance();
        datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        datetime.set(Calendar.MINUTE, minute);
    
        if (datetime.get(Calendar.AM_PM) == Calendar.AM)
            am_pm = "AM";
        else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
            am_pm = "PM";
    
        String strHrsToShow = (datetime.get(Calendar.HOUR) == 0) ?"12":datetime.get(Calendar.HOUR)+""; 
    
        ((Button)getActivity().findViewById(R.id.btnEventStartTime)).setText( strHrsToShow+":"+datetime.get(Calendar.MINUTE)+" "+am_pm );
    }
    
    0 讨论(0)
  • 2020-12-10 13:47
    private String getReminingTime() {
        String delegate = "hh:mm aaa";
        return (String) DateFormat.format(delegate,Calendar.getInstance().getTime());
    }
    
    0 讨论(0)
  • 2020-12-10 13:47
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss a");
    String  currentTime = df.format(currnetDateTime.getTime());
    
    0 讨论(0)
  • 2020-12-10 13:47
    // Best Result Give Bapu_Darbar:::
    
    Date dt = new Date(); int hours = dt.getHours(); int min = dt.getMinutes(); int am_pm = Calendar.AM_PM;
    
        if(hours>=1 || hours<=3 && am_pm == Calendar.AM){
            spl_text.setText("Welcome \n Good Night,Sweet Dream");
        }else if(hours>=4 || hours<=12 && am_pm == Calendar.AM){
            spl_text.setText("Welcome \n Good Morning,Have A Nice Day");
        }else if(hours>=13 || hours<=17 && am_pm == Calendar.PM){
            spl_text.setText("Welcome \n Good Afternoon");
        }else if(hours>=18 || hours<=20 && am_pm == Calendar.PM){
            spl_text.setText("Welcome \n Good Evening");
        }else if(hours>=21 || hours<=24 && am_pm == Calendar.PM){
            spl_text.setText("Welcome \n Good Night,Sweet Dream");
        }
    
    0 讨论(0)
  • 2020-12-10 13:52
    int am_pm=mycalendar.get(Calendar.AM_PM);
    String amOrpm=((am_pm==Calendar.AM)?"am":"pm");
    

    But as per my opinion simpleDateFormat would be the best use, like this:

    final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("America/Chicago"));
    SimpleDateFormat format = new SimpleDateFormat("h:mm a");
    format.setTimeZone(c.getTimeZone());
    String myFormatted time = format.format(c.getTime());
    

    It's better as the calendar by default will return 00 for 12 pm, which you might don't want.

    0 讨论(0)
  • 2020-12-10 13:53

    I have done like this to achieve it.

    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            TextView datehid = (TextView)findViewById(R.id.timehidden);
            if(hourOfDay>12) {
                datehid.setText(String.valueOf(hourOfDay-12)+ ":"+(String.valueOf(minute)+"pm"));
            } else if(hourOfDay==12) {
                datehid.setText("12"+ ":"+(String.valueOf(minute)+"pm"));
            } else if(hourOfDay<12) {
                if(hourOfDay!=0) {
                    datehid.setText(String.valueOf(hourOfDay) + ":" + (String.valueOf(minute) + "am"));
                } else {
                    datehid.setText("12" + ":" + (String.valueOf(minute) + "am"));
                }
            }
        }
    };    
    
    0 讨论(0)
提交回复
热议问题