私の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;
}