0

在我的 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;

            }
4

1 回答 1

2

1)删除硬代码。
采用

final String mins = parent.getItemAtPosition(pos).toString();
final Pattern minutes = Pattern.compile("([0-9]+) minutes"); 
final Matcher m = minutes.matcher(mins);
String stringMinutes = "0";
if (mins.matches())
{
   stringMinutes = m.group(1);
}  
setAlarm(Integer.parseInt(stringMinutes), stringMinutes);  

而不是你的硬编码

String mins = parent.getItemAtPosition(pos).toString();  
// your hardcode is here  
setAlarm(Integer.parseInt(stringMinutes), stringMinutes);  

2)使用 DateTimeFormatter 而不是 您在此日期之前 使用和创建SimpleDateFormatter
java 。DateSimpleDateFormatterDateTime

DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");  
dt = formatter.parseDateTime(currentDate);  

3) Joda
在这段代码中运行良好,Joda 没有问题。

String subtractTime(DateTime dt, int minusTime)  

应该运作良好。
调试你的代码,问题出在前面

if(mins.equals("5 minutes")){intmins = 5;}
if(mins.equals("10 minutes")){intmins = 10;}  
//...
于 2012-09-24T16:56:17.363 回答