Advertisement
Guest User

Untitled

a guest
May 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. public class TimeConvert
  2. {
  3.  
  4. public String doConverstion(String input)
  5. {
  6. String result = "";
  7. String []array = null;
  8. String ampm = "AM";
  9.  
  10. try
  11. {
  12. array= input.split("[:]");
  13. if (array[0].length() < 2)
  14. {
  15. throw new TimeFormatException();
  16. }
  17.  
  18. int hour = Integer.parseInt(array[0]);
  19. int min = Integer.parseInt(array[1]);
  20.  
  21. if (hour > 12)
  22. {
  23. hour-=12;
  24. ampm = "PM";
  25. }
  26. result = "" + hour + ":" + min + ampm;
  27.  
  28.  
  29. if(hour > 24)
  30. {
  31. System.out.println("Hours must be equal to or less than 24");
  32. }
  33.  
  34. if (min > 59)
  35. {
  36. System.out.println("Minutes must be less than 60");
  37. }
  38. }
  39. catch(TimeFormatException tfe)
  40. {
  41. System.out.println(tfe.getMessage());
  42. }
  43. catch(Exception ex)
  44. {
  45. System.out.println(ex.getMessage());
  46. }
  47. return result;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement