gpt4 book ai didi

java - 将分钟转换为 24 小时格式 Android 应用程序

转载 作者:行者123 更新时间:2023-12-01 15:30:47 24 4
gpt4 key购买 nike

我需要在我的应用程序中将分钟转换为 24 小时格式,例如我的应用程序有一个方法返回以分钟为单位的时间 260,如何将此时间转换为 24 格式?

非常感谢,,

--- 更新后 ------------------------------------------ ----------------------------

//Inside OnCreate Method

//For Time Part#1
CaseDurationH.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

int i = CaseDurationH.getSelectedItemPosition();
strH = parent.getSelectedItem().toString();

try {
intH = Integer.parseInt(strH);
intHInMin=intH*60; // inH and intHInMin are a integer
} catch (NumberFormatException nfe) {

}


if(i==2){

CaseDurationM.setEnabled(false);

}

if(strH.equals("hr0"))

{

CaseDurationM.setEnabled(true);

}



if(strH.equals("hr1"))

{

CaseDurationM.setEnabled(true);

}





if(strH.equals("hr2"))

{

CaseDurationM.setEnabled(false);

}


}

public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub



} });

//For Time Part#2
CaseDurationM.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

int i = CaseDurationM.getSelectedItemPosition();
strM = parent.getSelectedItem().toString();


try {
intM = Integer.parseInt(strM);

} catch (NumberFormatException nfe) {

}





}

public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub



} });

String CaseTime= calc();
case1.setTime(CaseTime); //case1 is an Onbject



//Out OnCreate Method
private String calc() {

int CTimeTotalInMin= intHInMin + intM;
Calendar calendar = new GregorianCalendar();

int CurrentHour = calendar.get(Calendar.HOUR);
int CurrentMinute = calendar.get(Calendar.MINUTE);
int CurrentTime= (CurrentHour * 60)+ CurrentMinute;
int TotalTimeInMin= CurrentTime-CTimeTotalInMin;
int hours = TotalTimeInMin / 60;
int minutes = TotalTimeInMin % 60;
if(hours>23)
hours = hours % 24;

String g = ""+hours+":"+minutes;



return g;}
<小时/>

我的问题是:如何获取(当前时间 - 用户从微调器中选择的时间(我向用户显示两个小时列表(0h 或 1h 或 2h)和分钟列表(从 0-59),如下所示,例如,如果用户选择 (1 小时) 并且当前时间是 (2:15 PM),则答案应该是 = 13:15 PM),我尝试了此代码,但它与 PM 和 AM 没有什么不同?有什么建议吗?非常感谢...

<!-- @ string.xml these imtes for CDurationH= hours -->
<string name="hr0">0</string>
<string name="hr1">1</string>
<string name="hr2">2</string>
<!-- @ string.xml these imtes for CDurationM= minutes -->
<string name="min0">0</string>
<string name="min5">5</string>
<string name="min10">10</string>
<string name="min15">15</string>
<string name="min20">20</string>
<string name="min25">25</string>
<string name="min30">30</string>
<string name="min35">35</string>
<string name="min40">40</string>
<string name="min45">45</string>
<string name="min50">50</string>
<string name="min55">55</string>
<string name="min59">59</string>
<小时/>

最佳答案

通过除法和模运算符计算小时和分钟值。检查可能的小时值是否大于 23,表明超出了白天。设置您的值的格式以表示为 24 小时格式:

int minutes = 260;
int hours = minutes / 60;
int minutesInDisplay = minutes % 60;
if (hours > 23)
hours = hours % 24;
String postfix = (hours < 12) ? "am" : "pm";
NumberFormat formatter = new DecimalFormat("00");
String display = formatter.format(hours) + ":"
+ formatter.format(minutesInDisplay);
System.out.println("24 hour formatted time: " + display + " " + postfix);

关于java - 将分钟转换为 24 小时格式 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529911/

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