我需要在我的应用程序中将分钟转换为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;}
我的问题是:我如何得到(当前时间 - 用户从Spinner中选择的时间 (我向用户显示两个小时(0小时或1小时或2小时)和分钟(从0到59)的列表,如下所示, 对于即时,如果用户选择(1小时)并且当前时间是(下午2:15),答案应该是= 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>
答案 0 :(得分:2)
通过除法和模数运算符计算小时和分钟值。 检查可能的小时值是否大于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);
答案 1 :(得分:0)
如你所知,一小时是60分钟。因此,您可以将分钟数除以60,从而为您提供小时数。
int x = 260;
int hours = x / 60;
int minutes = x % 60;
System.out.printf("%d minutes is %d hours and %d minutes%n", x, hours, minutes);
如果您还想要天数:
int x = 2270; // just an example
// 24 * 60 = 1440 minutes in a day
int days = x / 1440;
int minutesRestInDay = x % 1440;
int hours = minutesRestInDay / 60;
int minutes = minutesRestInDay % 60;
System.out.printf("%d minutes is %d days, %d hours and %d minutes%n",
x, days, hours, minutes);
答案 2 :(得分:-1)
public String getTime(int minutes){
String value = "";
if(minutes<=0){
return "minutes cannot be zero or negative";
}
int hr=minutes/60;
int min=minutes%60;
if(hr>23){
hr = hr % 24;
}
return hr+":"+min;
}