Arun Kumar
Arun Kumar

Reputation: 877

Not able to add 15 minute to 00:00

I have written a function for adding time in a 24 hour format as given below

for (int i = 0; i < cursor.getCount(); i++) {

        String RevisedTime="00:00";     



        // get hour and minute from time string
        StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
        int j = 0;
        int[] val = new int[st1.countTokens()];
        // iterate through tokens
        while (st1.hasMoreTokens()) {
            val[j] = Integer.parseInt(st1.nextToken());
            j++;
        }

        // call time add method with current hour, minute and minutesToAdd,
        // return added time as a string
        String date = addTime(val[0], val[1], 15);



    }

public String addTime(int hour, int minute, int minutesToAdd) {
        Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
        calendar.add(Calendar.MINUTE, minutesToAdd);
        SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
        String date = sdf.format(calendar.getTime());
        return date;
    }

The problem is that while adding 15 minutes to 00:00 I am getting the output as 12.15....

I need to get it as 00:15......Pleas help me.....

Upvotes: 2

Views: 312

Answers (2)

Lalit Poptani
Lalit Poptani

Reputation: 67286

You just have to change a bit and it would work fine.

Replace

SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");

By

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

Upvotes: 1

krtek
krtek

Reputation: 1065

You have to use 0-23 hour format, not 1-24.

Instead of SimpleDateFormat sdf = new SimpleDateFormat("kk:mm"); use SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

Upvotes: 5

Related Questions