I want to get to know the time difference from two date/time. In JAVA, there is no method available built in to find out the differences between two dates. Hence, we have to getTime in long format of first date and it should be subtracted with another date. Using this long value, we can form the date format.
Here in the below code snippets, start and end time caputed using System.currentTimeMillis(), we have to come to know how long it take to complete our business.
long startTime = System.currentTimeMillis();
//do your business
...
long endTime= System.currentTimeMillis();
//calculate difference
long timeDiff_long = endTime - startTime;
long days = timeDiff_long / (60 * 60 * 24);
long backdays = days * 60 * 60 * 24;
long hour = (timeDiff_long - backdays) / (60 * 60);
long backhour = hour * 60 * 60;
long minute = (timeDiff_long - (backdays + backhour) ) / 60;
long backminute = minute * 60;
long seconds = (timeDiff_long - (backdays + backhour + backminute )) ;
System.out.println("DAYS: " + days);
System.out.println("HOUR: " + hour);
System.out.println("MINUTE: " + minute);
System.out.println("SECOND: " + seconds);
Using the above code, we can format Date object as our wish.
No comments:
Post a Comment