Joda Time不能减去一小时

时间:2012-09-24 15:36:24

标签: java android date jodatime

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

            }

1 个答案:

答案 0 :(得分: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
您可以使用Date获取java SimpleDateFormatter并在此日期之前创建DateTime

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;}  
//...