SHARE
TWEET

Untitled

advictoriam Jan 15th, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class TimeConvert
  2. {
  3.    /**
  4.       Converts military time to ordinary time.
  5.       @param milTime, the military time provided: hhmm
  6.       @return a string of the form 9:35 am or 7 pm
  7.    */
  8.    public static String militaryToOrdinaryTime(int milTime)
  9.    {
  10.       if(milTime == 0)
  11.       {
  12.          return "12:00 am";
  13.       }
  14.       if(milTime < 100)
  15.       {
  16.          String min = Integer.toString(milTime);
  17.          if(min.charAt(0) == '0' && min.charAt(1) == '0')
  18.          {
  19.             return "12 am";
  20.          }
  21.          return String.format("12:%c%c am", min.charAt(0), min.charAt(1));
  22.       }
  23.       if(milTime < 1000)
  24.       {
  25.          String time = Integer.toString(milTime);
  26.          if(time.charAt(1) == '0' && time.charAt(2) == '0')
  27.          {
  28.             return String.format("%c am", time.charAt(0));
  29.          }
  30.          return String.format("%c:%c%c am", time.charAt(0), time.charAt(1), time.charAt(2));
  31.       }
  32.       if(milTime < 1200)
  33.       {
  34.          String time = Integer.toString(milTime);
  35.          if(time.charAt(2) == '0' && time.charAt(3) == '0')
  36.          {
  37.             return String.format("%c%c am", time.charAt(0), time.charAt(1));
  38.          }
  39.          return String.format("%c%c:%c%c am", time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3));
  40.       }
  41.       if(milTime < 2200)
  42.       {
  43.          String time = Integer.toString(milTime - 1200);
  44.          if(time.charAt(1) == '0' && time.charAt(2) == '0')
  45.          {
  46.             return String.format("%c pm", time.charAt(0));
  47.          }
  48.          return String.format("%c:%c%c pm", time.charAt(0), time.charAt(1), time.charAt(2));
  49.       }
  50.       if(milTime < 2400)
  51.       {
  52.          String time = Integer.toString(milTime - 1200);
  53.          if(time.charAt(2) == '0' && time.charAt(3) == '0')
  54.          {
  55.             return String.format("%c%c pm", time.charAt(0), time.charAt(1));
  56.          }
  57.          return String.format("%c%c:%c%c pm", time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3));
  58.       }
  59.       return null;
  60.    }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top