gpt4 book ai didi

android - 24 小时格式到 12 小时格式

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:21 25 4
gpt4 key购买 nike

我在将 24 小时格式转换为 12 小时格式时遇到问题。这是我的代码。

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
ViewGroup vg=(ViewGroup) view.getChildAt(0);
hour = hourOfDay;
minutes = minute;
String am_pm = (hourOfDay < 12) ? "AM" : "PM";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
String tme = hour +":"+ minutes+am_pm;
et1.setText(tme);
}
};

我从警报对话框转到时间选择器对话框,因为我不知道如何将 24 小时格式更改为 12 小时格式

et1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false).show();
}
});

}

这证明我已经将 bool 印象变为假。

最佳答案

试试这个-

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
minutes = minute;
String timeSet = "";
if (hour > 12) {
hour -= 12;
timeSet = "PM";
} else if (hour == 0) {
hour += 12;
timeSet = "AM";
} else if (hour == 12){
timeSet = "PM";
}else{
timeSet = "AM";
}

String min = "";
if (minutes < 10)
min = "0" + minutes ;
else
min = String.valueOf(minutes);

// Append in a StringBuilder
String aTime = new StringBuilder().append(hour).append(':')
.append(min ).append(" ").append(timeSet).toString();
et1.setText(aTime);
}
};

关于android - 24 小时格式到 12 小时格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com