【问题标题】:Timepicker Integer Error时间选择器整数错误
【发布时间】:2015-04-20 03:33:35
【问题描述】:

我的偏好活动中有一个时间选择器,用于设置显示通知的时间。该值存储为字符串,例如:“15:45”。为了理解这个问题,我将进一步解释该值旁边会发生什么:

SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
    String hour = pref.getString("notification_time","");
    // notification_time is my preference key
    String Hora = hour;
    int hours = Integer.parseInt(Hora.substring(0, 2));
    int min = Integer.parseInt(Hora.substring(3, 5));
    // as you can see, I parse the string, and then use the integers to set the time (see below)
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, 00);

现在的问题是,My TimePicker 存储值的方式不同,如果时间是 AM:例如,如果您将时间设置为 07:45,它会将字符串中的时间存储为“7:45”,而不是“ 07:45”,因此代码中的这一行失败:

int hours = Integer.parseInt(Hora.substring(0, 2));

(抛出这个错误,并不是真正需要理解问题):

java.lang.NumberFormatException: Invalid int: "5:"

,因为“子字符串”的位置不再起作用。 (1 位存储在字符串中,而不是 2 位)。分钟也是如此,例如如果我将分钟设置为 08,我的时间选择器将它们存储为 8,然后再次出现相同的问题。

现在我想了两种方法来解决这个问题:要么更改我的 settingsactivity 中的代码并以不同的方式解析字符串,要么更改存储字符串的方式:

if (positiveResult) {
        lastHour=picker.getCurrentHour();
        lastMinute=picker.getCurrentMinute();
        String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

        if (callChangeListener(time)) {
            persistString(time);
        }
        setSummary(getSummary());
    }

(这些是负责将值保存为字符串的代码行)

我应该如何解决这个问题?

【问题讨论】:

    标签: java android string android-preferences timepicker


    【解决方案1】:

    您可以尝试使用DateFormatparse 方法来解析您的通知时间字符串。这将返回一个Date 对象,您可以使用它来设置Calendar 对象的日期/时间。

    类似:

    DateFormat df = new SimpleDateFormat("H:mm"); // construct date formatter to recognize your time pattern
    Date myDate = df.parse(pref.getString("notification_time",""));
    calendar.setTime(myDate);  // set the time of calendar object
    
    // do other stuff with date here...
    

    这样,您无需费心解析时间,您可以让现有工具为您完成工作。

    简单测试:

    public class Test {
        public static void main(String[] args) throws Exception {
            DateFormat df = new SimpleDateFormat("H:mm");
            Date myDate = df.parse("17:45");
            System.out.println(myDate.toString());
    
            Calendar c = Calendar.getInstance();
            c.setTime(myDate);
    
            int hours = c.get(Calendar.HOUR_OF_DAY);
            int min = c.get(Calendar.MINUTE);
    
            System.out.println("Hour = " + hours + "\nMin = " + min);
        }
    }
    

    生产:

    Thu Jan 01 17:45:00 PST 1970
    Hour = 17
    Min = 45
    

    【讨论】:

    • 这有一个小问题,我收到以下错误:Error:(350, 32) error: non-static method parse(String) cannot be referenced from a static context
    • @just_deko 查看我的编辑,我正在编写代码并误认为是静态方法。您可以使用SimpleDateFormat 指定要查找的模式,在本例中,是基于一天中指定时间的日期对象,采用 24 小时格式。其他格式可以看here
    • 很抱歉再次打扰您。我实现了代码,出现小错误:未处理的异常。好吧,我认为没什么大不了的,只需围绕您的代码进行 try/catch parseexception,但这会成为另一个问题:当我尝试使用 calendar.setTime 设置时间时,它说:“无法解析符号 myDate”跨度>
    • @just_deko 你需要抓住/重新抛出ParseException,是的。您的对象必须在您使用它的范围内,因此请调整您的变量以满足您的需求。
    • 一个小问题,我现在有了你说的方法:public void notificationsetter(Context context, String[] args) throws Exception{},现在我调用方法时如何传递上下文:notificationsetter(getApplicationContext(), "here it says i need to use java.lang string[], what exactly should I write here?");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多