matghazaryan
matghazaryan

Reputation: 5896

Android Time issue

 private void getSelectedTime(final String json){

        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH); // Note: zero based!
        int day = now.get(Calendar.DAY_OF_MONTH);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        int millis = now.get(Calendar.MILLISECOND);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");


        JSONArray list;
        JSONObject jsonObject;

        try {
            jsonObject = new JSONObject(json);

            list = jsonObject.getJSONArray("3");
            StringTokenizer tokenizer = new StringTokenizer(list.get(0).toString(),"-");
            String startTime = tokenizer.nextToken();
            String endTime = tokenizer.nextToken();
            String temp1 = year+"/"+month+"/"+day+" "+startTime;
            String temp2 = year+"/"+month+"/"+day+" "+endTime;
            System.out.println("temp1="+temp1);
            System.out.println("temp2="+temp2);

            Date date1 = dateFormat.parse(temp1); // temp1=2012/5/25 03:00
            Date date2 = dateFormat.parse(temp2); //temp2=2012/5/25 03:06

            System.out.println("Year1="+date1.getYear());
            System.out.println("Month1="+date1.getMonth());
            System.out.println("Day1="+date1.getDay());
            System.out.println("Hour1="+date1.getHours());
            System.out.println("Minutes1="+date1.getMinutes());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        catch (ParseException e) {
            e.printStackTrace();
        }

    }

}

In my application I am working with time and I have some issue here. Take a look below I have this result.

list.get(0) = 03:00-03:06
temp1=2012/5/25 03:00
temp2=2012/5/25 03:06

But when I am trying do this

System.out.println("Year="+date1.getYear());
System.out.println("Month="+date1.getMonth());
System.out.println("Day="+date1.getDay());
System.out.println("Hour="+date1.getHours());
System.out.println("Minutes="+date1.getMinutes());

I've got this result

Year=112
Month=4
Day=5
Hour=3
Minutes=0

Could anyone tell me why the result is wrong?

Upvotes: 0

Views: 89

Answers (2)

mariomario
mariomario

Reputation: 660

You could also do instead of date1.getYear()

Calendar cal = Calendar.getInstance();
cal.setTime(date1);

int year = cal.get(Calendar.year);

This also works for other time values. Or you can use Joda Time that has been suggested already.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500903

Could anyone tell me why I the result is wrong?

Sure - you're using deprecated methods (you should be getting warnings - don't ignore them!), and you haven't read the docs for them. For example, from Date.getYear():

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

If you want to stick to the JDK, you should use java.util.Calendar instead (populating it with the Date via setTime) in an appropriate time zone. Note that months are still 0-based in Calendar, although the year is at least more sensible.

However, it would generally be better to use Joda Time if you possibly can. It's a much better thought-out API. It may be too large for you to want to use it on Android though - you may wish to see whether there's a cut-down version available.

Upvotes: 5

Related Questions