saswat satapathy saswat satapathy - 21 days ago 8
Java Question

How to convert a time whch given in string and find the difference in java and convert back to string

I have following time for a given day which is in sting format.

"begin_time_tz": "00:00 DST",
"end_time_tz": "07:30 DST",


I have to find the difference between them in java. Please help me to find the difference and convert back to string

Note: Date is in 24 hour format(00:00 -- 23:59)

Answer

You can use the below method to get the time difference, just pass HH:MM without the timezone.

public long getTimeDifference(String time1, String time2) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date date1 = format.parse(time1);
        Date date2 = format.parse(time2);
        long difference = date2.getTime() - date1.getTime();
        return difference/1000;
    }
Comments