在android中以AM / PM格式显示时间

时间:2010-04-09 06:25:24

标签: android

我正在使用时间选择器并使用以下代码在文本视图中显示时间......

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(SendMail.this, "Your Appointment time is "+hourOfDay+":"+minute,    Toast.LENGTH_SHORT).show();
        TextView datehid = (TextView)findViewById(R.id.timehidden);
        datehid.setText(String.valueOf(hourOfDay)+ ":"+(String.valueOf(minute)));
    }
};

现在,问题是当我将时间设置为晚上8点然后我将时间显示为20:0 ...我想将时间显示为晚上8点...我该怎么做???

9 个答案:

答案 0 :(得分:35)

private String getReminingTime() {
    String delegate = "hh:mm aaa";
    return (String) DateFormat.format(delegate,Calendar.getInstance().getTime());
}

答案 1 :(得分:16)

我这样做是为了实现它。

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"));
            }
        }
    }
};    

答案 2 :(得分:6)

SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss a");
String  currentTime = df.format(currnetDateTime.getTime());

答案 3 :(得分:4)

int am_pm=mycalendar.get(Calendar.AM_PM);
String amOrpm=((am_pm==Calendar.AM)?"am":"pm");

但根据我的观点,simpleDateFormat将是最佳用途,如下所示:

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());

最好,因为默认情况下日历会在下午12点返回00,这可能是您不想要的。

答案 4 :(得分:4)

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");
}

答案 5 :(得分:2)

我这样做是为了实现它。

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);
}

答案 6 :(得分:0)

这对我有用:

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 );
}

答案 7 :(得分:0)

// 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");
    }

答案 8 :(得分:0)

解决方案可以设置两个字符序列“AM”和“PM”的警报对话框。 下面是一个例子。

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

        CharSequence [] sequences = {
                "AM",
                "PM"
        };
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(),AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        alert.setItems(sequences, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which ==0){
                    if (minute ==0){
                        time.setText(hourOfDay+":"+minute+"0"+"AM");
                    }else {
                        time.setText(hourOfDay + ":" + minute+"AM");
                    }
                }else{
                    if (minute ==0){
                        time.setText(hourOfDay+":"+minute+"0"+"PM");
                    }else {
                        time.setText(hourOfDay + ":" + minute+"PM");
                    }
                }
            }
        });
        alert.show();
    }

希望对大家有帮助!