Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TimeConvert
- {
- public String doConverstion(String input)
- {
- String result = "";
- String []array = null;
- String ampm = "AM";
- try
- {
- array= input.split("[:]");
- if (array[0].length() < 2)
- {
- throw new TimeFormatException();
- }
- int hour = Integer.parseInt(array[0]);
- int min = Integer.parseInt(array[1]);
- if (hour > 12)
- {
- hour-=12;
- ampm = "PM";
- }
- result = "" + hour + ":" + min + ampm;
- if(hour > 24)
- {
- System.out.println("Hours must be equal to or less than 24");
- }
- if (min > 59)
- {
- System.out.println("Minutes must be less than 60");
- }
- }
- catch(TimeFormatException tfe)
- {
- System.out.println(tfe.getMessage());
- }
- catch(Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement