Advertisement
rachmadi

TimeCalculation

Mar 23rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. public class TimeCalculation {
  2.  
  3.     public int calculateDifference(String timeFrom, String timeTo){
  4.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  5.         Date startDate = simpleDateFormat.parse("22:00");
  6.         Date endDate = simpleDateFormat.parse("07:00");
  7.  
  8.         long difference = endDate.getTime() - startDate.getTime();
  9.         if(difference<0)
  10.         {
  11.             Date dateMax = simpleDateFormat.parse("24:00");
  12.             Date dateMin = simpleDateFormat.parse("00:00");
  13.             difference=(dateMax.getTime() -startDate.getTime() )+(endDate.getTime()-dateMin.getTime());
  14.         }
  15.         int days = (int) (difference / (1000*60*60*24));  
  16.         int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60));
  17.         int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
  18.         Log.i("log_tag","Hours: "+hours+", Mins: "+min);
  19.  
  20.         return hours;
  21.     }
  22.      
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement