Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TimeConvert
- {
- /**
- Converts military time to ordinary time.
- @param milTime, the military time provided: hhmm
- @return a string of the form 9:35 am or 7 pm
- */
- public static String militaryToOrdinaryTime(int milTime)
- {
- if(milTime == 0)
- {
- return "12:00 am";
- }
- if(milTime < 100)
- {
- String min = Integer.toString(milTime);
- if(min.charAt(0) == '0' && min.charAt(1) == '0')
- {
- return "12 am";
- }
- return String.format("12:%c%c am", min.charAt(0), min.charAt(1));
- }
- if(milTime < 1000)
- {
- String time = Integer.toString(milTime);
- if(time.charAt(1) == '0' && time.charAt(2) == '0')
- {
- return String.format("%c am", time.charAt(0));
- }
- return String.format("%c:%c%c am", time.charAt(0), time.charAt(1), time.charAt(2));
- }
- if(milTime < 1200)
- {
- String time = Integer.toString(milTime);
- if(time.charAt(2) == '0' && time.charAt(3) == '0')
- {
- return String.format("%c%c am", time.charAt(0), time.charAt(1));
- }
- return String.format("%c%c:%c%c am", time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3));
- }
- if(milTime < 2200)
- {
- String time = Integer.toString(milTime - 1200);
- if(time.charAt(1) == '0' && time.charAt(2) == '0')
- {
- return String.format("%c pm", time.charAt(0));
- }
- return String.format("%c:%c%c pm", time.charAt(0), time.charAt(1), time.charAt(2));
- }
- if(milTime < 2400)
- {
- String time = Integer.toString(milTime - 1200);
- if(time.charAt(2) == '0' && time.charAt(3) == '0')
- {
- return String.format("%c%c pm", time.charAt(0), time.charAt(1));
- }
- return String.format("%c%c:%c%c pm", time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3));
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement