Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. //calculateTotalHours()
  2. public static String calculateTotalHours(Cell inTime, Cell outTime, Cell breakStart, Cell breakEnd)
  3. {
  4. System.out.println(LOG + "calculateTotalHours");
  5. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
  6. Date in, out, start, end;
  7.  
  8. if(null != inTime && null != outTime && null != breakStart && null != breakEnd)
  9. {
  10. try
  11. {
  12. in = timeFormat.parse(inTime.getStringCellValue());
  13. out = timeFormat.parse(outTime.getStringCellValue());
  14. start = timeFormat.parse(breakStart.getStringCellValue());
  15. end = timeFormat.parse(breakEnd.getStringCellValue());
  16.  
  17. long lunchTotal = end.getTime() - start.getTime();
  18. long totalWork = out.getTime() - in.getTime();
  19. long totalTime = totalWork - lunchTotal;
  20. long diffHours = totalTime/HOURS % 24;
  21. totalTime -= diffHours;
  22. long diffMinutes = totalTime/MINUTES % 60;
  23. totalTime -= diffMinutes;
  24.  
  25. return String.format("%02d:%02d", diffHours, diffMinutes);
  26. }
  27. catch (ParseException e) {e.printStackTrace();}
  28. }
  29. else
  30. {
  31. try
  32. {
  33. if(null == breakStart || null == breakEnd) {
  34. in = timeFormat.parse(inTime.getStringCellValue());
  35. out = timeFormat.parse(outTime.getStringCellValue());
  36.  
  37. long totalTime = out.getTime() - in.getTime();
  38. long diffHours = totalTime/HOURS % 24;
  39. totalTime -= diffHours;
  40. long diffMinutes = totalTime/MINUTES % 60;
  41. totalTime -= diffMinutes;
  42.  
  43. return String.format("%02d:%02d", diffHours, diffMinutes);
  44. }
  45. else if(null == inTime.getStringCellValue())
  46. {
  47. System.out.println(LOG + "inTime is blank");
  48. return "-1";
  49. }
  50. }
  51. catch (ParseException e) {e.printStackTrace();}
  52. }
  53. return "-1";
  54. }
Add Comment
Please, Sign In to add comment