0

Possible Duplicate:
Calculating the Difference Between Two Java Date Instances

I am not able to solve below problem please help me.That is i am adding existing time to some some more time (minutes ,seconds or hours).Iam not able to write bellow wt type I wrote i mention that is not given correct answer,

String allexam_end_time="18:59:00";//48540000
long oneminute=60000;
try{
    SimpleDateFormat sdfkk = new SimpleDateFormat("hh:mm:ss");
    Date reqEndtimekk = sdfkk.parse(allexam_end_time);
    long total=reqEndtimekk.getTime()+oneminute;
    System.out.println("TOtal miliseconds         "+reqEndtimekk.getTime()+oneminute);
    System.out.println("total---------"+total);
    int s = (int)(total / 1000) % 60;
    int m = (int)(total / (1000 * 60)) % 60;
    int h=  (int) (total / (1000 * 60 * 60)) % 24;
    System.out.println(h+":"+m+":"+s);
}catch(Exception e){

}

Output

    output is TOtal miliseconds4854000060000
    total---------48600000
    13:30:0

But I need answer that if added one minute means outputshould 19:00:00 How can solve?

Community
  • 1
  • 1
user1906191
  • 51
  • 2
  • 7

3 Answers3

1

You can use GregorianCalendar, here is a code example:

    String allexam_end_time = "18:59:00";// 48540000
    SimpleDateFormat sdfkk = new SimpleDateFormat("hh:mm:ss");
    Date reqEndtimekk = sdfkk.parse(allexam_end_time);
    Calendar instance = GregorianCalendar.getInstance();
    instance.setTime(reqEndtimekk);
    //add one minute        
    instance.add(GregorianCalendar.MINUTE, 1);

    System.out.println(sdfkk.format(instance.getTime()));

The output

07:00:00
fmodos
  • 4,472
  • 1
  • 16
  • 17
  • I want in 19:00:00 this format – user1906191 Jan 24 '13 at 05:28
  • just change the 'hh' of the SimpleDateFormat to 'HH' and it will the show 19 instead 7 :) – fmodos Jan 24 '13 at 05:37
  • Hi please tell me how can i add milliseconds, I do not want to pass as 60,i want to pass milliseconds how can i add 18:59:00+miliseconds want ans in 19:00:00 format please – user1906191 Jan 24 '13 at 05:53
  • change the GregorianCalendar.MINUTE to GregorianCalendar.MILLISECOND... so to one minute it should be: instance.add(GregorianCalendar.MILLISECOND, 60000); – fmodos Jan 24 '13 at 05:58
1

Maybe this will help!

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtility {

    public static void main(String[] args) throws ParseException {
        String examEndTime = "18:59:00";

        System.out.println(modifyTime(examEndTime, 60));
    }

    private static String modifyTime(String origTime, int modSecs) throws ParseException {

        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        timeFormat.setLenient(false);
        Date time = timeFormat.parse(origTime);
        time.setSeconds(modSecs);
        return timeFormat.format(time);
    }

}

The output will be
19:00:00

aa8y
  • 3,854
  • 4
  • 37
  • 62
  • Hi please tell me how can i add milliseconds, I do not want to pass as 60,i want to pass milliseconds how can i add 18:59:00+miliseconds want ans in 19:00:00 format please – user1906191 Jan 24 '13 at 05:50
0

It appears to me that your code, as it is, may be doing some time zone conversions. For example, I calculate 18:59:00 to be (18*60*60 + 59*60)*1000 = 68340000 ms. But if I put the same thing into Java and call getTime(), I get 39540000 ms, and you get 48540000.

Some of these other replies are better ways of solving your issue (one major thing is calling sdfkk.format(reqEndtimekk) at the end rather than getting h/m/s again), but your but seems to be around time zones.

Trasvi
  • 1,207
  • 1
  • 15
  • 23