0

I'm building my custom application for Android. I have implemented a datepicker dialog which it takes time when a time is picked. The problem is that time picker doesn't format correctly what I select (for example if 03 is selected, datepicker returns 3).

For fixing this issue I've implemented this code:

    TimePickerDialog.OnTimeSetListener timePickerListener = 
            new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int selectedHour,
                int selectedMinute) {

            hour = selectedHour;
            minute = selectedMinute;

            long date = calendarView.getDate();
            DateFormat df = new DateFormat();

            String format = (String) df.format("dd/MM/yyyy", date);

            Date hourdate = new Date();

            hourdate.setHours(hour);
            hourdate.setMinutes(minute);


            SimpleDateFormat hourformatter = new SimpleDateFormat("HH");
            SimpleDateFormat minuteformatter = new SimpleDateFormat("mm");

            String formattedhour = hourformatter.format(hourdate);
            String formattedminute = minuteformatter.format(hourdate);

            Log.v(StorybookContentProvider.TAG_LOG, "formattedhour" + Integer.parseInt(formattedminute) + formattedminute);


            as.setDate(format);
            as.setRangeStart("");
            as.setRangeFinish("");
            as.setHour(Integer.parseInt(formattedhour));
            Log.v(StorybookContentProvider.TAG_LOG, "formattedhoursaving" + Integer.parseInt(formattedminute));
            as.setMinutes(Integer.parseInt(formattedminute));
            as.setTabColor(as.tabHost, true);

The problem is: formattedhour is formatted in a way that I want (if selected 03 it returns 03), but if I do Integer.parseInt(formattedhour), it returns 3! It removes the format. Is this an error?

syb0rg
  • 8,057
  • 9
  • 41
  • 81
user1377034
  • 21
  • 1
  • 5
  • 2
    All this text & code just for asking if `Integer.parseInt ("03")` returns `3` ? well yes, that's what parseInt does. Read the [*manual*](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String))! – Nir Alfasi Jan 16 '13 at 22:18
  • 3 and 03 are the same, they both represent three – Steve Kuo Jan 17 '13 at 00:06

2 Answers2

3

There is no bug. It is working as expected.

Formatting is for display purpose, if you want 03, then use formatted one. Leading zero hasd different meaning while using with integer, that is why it will be treated as 3.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 1
    A leading zero in a Java integer literal is significant - it indicates that the value is octal rather than decimal. This isn't a problem parsing strings with leading zeroes using the one-argument version of `parseInt` because it assumes you want a decimal interpretation, but it can be a problem if you declare something like `int x = 030`, which assigns the decimal value 24 to x. – Dan Dyer Jan 16 '13 at 22:39
  • @DanDyer: You are correct. That skipped my mind. Deleted unnecessary text. – kosa Jan 16 '13 at 22:43
0

you have to delete the zero that is on the left of the integer, then it should work well

KhalidTaha
  • 453
  • 1
  • 5
  • 11