在我的 android 程序中,我有一个微调器,允许用户选择不同的时间。每个选择都使用 Joda 时间处理以减去分钟数。它适用于 0 到 59 分钟和 61 分钟及更长的时间。但是,当减去 60 分钟时,时间不会更新,而是显示原始时间。
如何让 Joda 时间减去 60 分钟?
微调器:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id1) {
String mins = parent.getItemAtPosition(pos).toString();
int intmins=0;
// process user's selection of alert time
if(mins.equals("5 minutes")){intmins = 5;}
if(mins.equals("10 minutes")){intmins = 10;}
if(mins.equals("20 minutes")){intmins = 20;}
if(mins.equals("30 minutes")){intmins = 30;}
if(mins.equals("40 minutes")){intmins = 40;}
if(mins.equals("50 minutes")){intmins = 50;}
if(mins.equals("60 minutes")){intmins = 60;}
if(mins.equals("120 minutes")){intmins = 120;}
String stringMinutes=""+intmins;
setAlarm(intmins, stringMinutes);
} else {
}
public void onNothingSelected(AdapterView parent) {
mLocationDisplay.setText(" " + location);
}
}
public void setAlarm(int intmins, String mins) {
// based alarm time on start time of event. TODO get info from database.
String currentDate;
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date1 = null;
DateTime dt;
currentDate = eventdate + " " + startTimeMilitary;// startTimeMilitary;
try {
date1 = myFormat.parse(currentDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dt = new DateTime(date1);
long dateInMillis = dt.getMillis();
String sDateInMillis = Long.toString(dateInMillis);
// subtract the selected time from the event's start time
String newAlertTime = subtractTime(dt, intmins);
newAlertTime = subtractTime(dt, intmins);
//......}
public String subtractTime(DateTime dt, int minusTime) {
DateTime greaterDate;
greaterDate = dt.minusMinutes(minusTime);
// newAlertTime is in UTC format
String newAlertTime = greaterDate.toString();
long alertInMillis = greaterDate.getMillis();
String sAlertInMillis = Long.toString(alertInMillis);
// ////new alert time is a stirng
setStringAlertInMillis(sAlertInMillis);
return newAlertTime;
}