Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.Timestamp;
  3. import java.util.Calendar;
  4.  
  5. public class CalculateCost {
  6. public static void main(String[] args) {
  7.  
  8. java.util.Date date = new java.util.Date();
  9. Timestamp timestamp1 = new Timestamp(date.getTime()); //ใส่เวลาเข้าที่นี่
  10.  
  11. Calendar cal = Calendar.getInstance();
  12. // cal.setTimeInMillis(timestamp1.getTime());
  13.  
  14. cal.add(Calendar.SECOND, 6000);
  15.  
  16. Timestamp timestamp2 = new Timestamp(cal.getTime().getTime()); //ใส่เวลาออกที่นี่
  17.  
  18. long milliseconds = timestamp2.getTime() - timestamp1.getTime();
  19. int seconds = (int) milliseconds / 1000;
  20. int hours = seconds / 3600;
  21. int minutes = (seconds % 3600) / 60;
  22. int totalTime=0;
  23. int totalCost=0;
  24. System.out.println("เวลาเข้าจอด : " + timestamp1);
  25. System.out.println("เวลาออกจอด : " + timestamp2);
  26.  
  27. System.out.println("Difference: ");
  28. System.out.println(" Hours: " + hours);
  29. System.out.println(" Minutes: " + minutes);
  30.  
  31. if(hours >= 4 && minutes > 0){
  32. totalTime = hours+1;
  33. totalCost = 20+((totalTime-4)*10);
  34. }
  35.  
  36. if(hours==4 && minutes==0){
  37. totalTime = hours;
  38. totalCost=20;
  39. }
  40.  
  41. if(hours > 1 && hours <= 4 ){
  42. totalTime = hours;
  43. totalCost=20;
  44. }
  45.  
  46. if(hours == 1 && minutes == 0){
  47. totalTime=1;
  48. totalCost=0;
  49. }
  50. if(hours == 0 && minutes <= 59){
  51. totalTime=1;
  52. totalCost=0;
  53. }
  54.  
  55. System.out.println("ชั่วโมง : " + hours);
  56. System.out.println("จำนวนชั่วโมงที่จอด : " + totalTime);
  57. System.out.println("จำนวนเงินค่าจอด : " + totalCost);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement