NumberFormatException: Invalid int: "HH"

456 views Asked by At

I get the error Invalid int:"HH". It only occurs on some devices. I think the problem is in the conversion , but why does it work on most devices ?

This is the logcat:

java.lang.NumberFormatException: Invalid int: "HH"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:375)
at java.lang.Integer.parseInt(Integer.java:366)
at java.lang.Integer.parseInt(Integer.java:332)
at com.lp.lemiediete.InserisciOra$4.onClick(InserisciOra.java:173)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:148)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

and this is my code(InserisciOra.class):

    breakfast = (EditText) view.findViewById(R.id.timebreak);
      breakfast.setFocusable(false);
      breakfast.setOnClickListener(new OnClickListener() {
  @Override
        public void onClick(View v) {

            int hour = 7;
            int minute = 0;
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    Calendar c =Calendar.getInstance();
                    c.set(Calendar.HOUR_OF_DAY,selectedHour);
                    c.set(Calendar.MINUTE,selectedMinute);
                    String cdate = (String) DateFormat.format("HH:mm", c.getTime());             
                     breakfast.setText(cdate);
                     PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("breakfast", cdate).commit();

                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Inserisci l'orario di colazione ");
            mTimePicker.show();

        }
});
 btniniziaora = (Button) view.findViewById(R.id.btn_iniziaora);
      btniniziaora.setOnClickListener(new OnClickListener() {
          @Override
               public void onClick(View v) {
              String breakfast1=breakfast.getText().toString();
              String lunch1=lunch.getText().toString();
              String dinner1=dinner.getText().toString();
              if(breakfast1.equals("") || lunch1.equals("") || dinner1.equals("")){
                  Toast.makeText(getActivity(), "Completa tutti i campi!", Toast.LENGTH_LONG).show();
                  return;
               }
                else{
                    d=new Database(getActivity());
                    String dataString=d.checkDataString();


                    //COLAZIONE
                    int duepunti = breakfast1.indexOf(":");
                    String hourColazione = breakfast1.substring(0,duepunti);
                    int intHourColazione=Integer.parseInt(hourColazione);
                    String minuteColazione = breakfast1.substring(duepunti + 1);
                    int intMinuteColazione=Integer.parseInt(minuteColazione);

                    int start = dataString.indexOf("/");
                    String dayString = dataString.substring(0,start);
                    String monthString = dataString.substring(start + 1,start+3);
                    String yearString = dataString.substring(start+4);
                    int day = Integer.parseInt(dayString);
                    int month = Integer.parseInt(monthString);
                    int year = Integer.parseInt(yearString); 
                    Calendar calNow = Calendar.getInstance();
                     Calendar c = (Calendar) calNow.clone();
                     c.setTimeInMillis(System.currentTimeMillis());
                     c.set(Calendar.YEAR, year);
                     c.set(Calendar.MONTH, month-1);
                     c.set(Calendar.DAY_OF_MONTH, day);
                    c.set(Calendar.HOUR_OF_DAY, intHourColazione);
                    c.set(Calendar.MINUTE, intMinuteColazione);
                    c.set(Calendar.SECOND, 0);    
                    c.set(Calendar.MILLISECOND, 0);
                    if(c.after(System.currentTimeMillis())){
                         c.add(Calendar.DAY_OF_MONTH, 1);
                    }
                    //PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("keyid", "0").commit();
                    Intent myIntent = new Intent(getActivity(), MyReceiver.class);


                    if(PendingIntent.getBroadcast(getActivity(), 0, 
                           myIntent, 
                           PendingIntent.FLAG_NO_CREATE) == null){

                    pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
                    alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

                 }
4

There are 4 answers

1
Shoeb Siddique On

Please use this -

String cdate = (String) DateFormat.format("hh:mm", c.getTime());

And for 24 Hour format please use this -

String cdate = (String) DateFormat.format("H:m", c.getTime());

instead of this-

String cdate = (String) DateFormat.format("HH:mm", c.getTime());
0
Bhavin Kevadiya On

I think "HH" should be in small latter like

String cdate = (String) DateFormat.format("hh:mm", c.getTime());      
0
Prakash Gajera On
    Fragmenet

private static final int year = 0;
    private static final int monthOfYear = 0;
    private static final int dayOfMonth = 0;


    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
            time=(Button) getActivity().findViewById(R.id.btnsettime);
            date=(Button) getActivity().findViewById(R.id.btnsetdate);

            time.setOnClickListener(this);
            date.setOnClickListener(this);

        }
    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v==time)
            {
                Calendar cal=Calendar.getInstance();
                int hour=cal.get(Calendar.HOUR);
                int minute=cal.get(Calendar.MINUTE);

                TimePickerDialog tpd=new TimePickerDialog(getActivity(), new OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        // TODO Auto-generated method stub

                        tvtime.setText(hourOfDay+":"+minute);
                        calender.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calender.set(Calendar.MINUTE, minute);

                    }

                }, hour, minute, true);
                tpd.show();


            }
            if(v==date)
            {

                Calendar cal=Calendar.getInstance();
                int day=cal.get(Calendar.DAY_OF_MONTH);
                int month=cal.get(Calendar.MONTH);
                int year=cal.get(Calendar.YEAR);

                DatePickerDialog dpg=new DatePickerDialog(getActivity(), new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {
                        // TODO Auto-generated method stub

                        tvdate.setText(dayOfMonth+":"+(monthOfYear+1)+":"+year);
                        calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        calender.set(Calendar.MONTH, monthOfYear);
                        calender.set(Calendar.YEAR, year);
                    }
                }, year, month, day);
dpg.show();

            }
    }
2
calvinfly On

According to doc, Up to API level 17, only adEhkMmszy were supported. Note that this class incorrectly implements k as if it were H for backwards compatibility.

Change 'HH' to 'kk', if you need 24 hour format. Remind that, 'H' refers hour in day (0-23), and 'k' refers hour in day (1-24)