Skip to content

Instantly share code, notes, and snippets.

@VirenMohindra
Created February 7, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VirenMohindra/c0af3774b3712e8a428e to your computer and use it in GitHub Desktop.
Save VirenMohindra/c0af3774b3712e8a428e to your computer and use it in GitHub Desktop.
Write code to input military time and print the hour and the whether it is AM or PM. Hours only.
import javax.swing.*;
public class MilitaryTime {
public static void main(String[] args) {
String militaryTime_in = JOptionPane.showInputDialog("Enter time in military format: ");
int militaryTime = Integer.parseInt(militaryTime_in);
String halfDay = militaryTime >= 1200 ? "PM" : "AM";
switch(militaryTime){
case 0:
JOptionPane.showMessageDialog(null, "12 " + halfDay);
break;
case 100:
JOptionPane.showMessageDialog(null, "1 " + halfDay);
break;
case 200:
JOptionPane.showMessageDialog(null, "2 " + halfDay);
break;
case 300:
JOptionPane.showMessageDialog(null, "3 " + halfDay);
break;
case 400:
JOptionPane.showMessageDialog(null, "4 " + halfDay);
break;
case 500:
JOptionPane.showMessageDialog(null, "5 " + halfDay);
break;
case 600:
JOptionPane.showMessageDialog(null, "6 " + halfDay);
break;
case 700:
JOptionPane.showMessageDialog(null, "7 " + halfDay);
break;
case 800:
JOptionPane.showMessageDialog(null, "8 " + halfDay);
break;
case 900:
JOptionPane.showMessageDialog(null, "9 " + halfDay);
break;
case 1000:
JOptionPane.showMessageDialog(null, "10 " + halfDay);
break;
case 1100:
JOptionPane.showMessageDialog(null, "11 " + halfDay);
break;
case 1200:
JOptionPane.showMessageDialog(null, "12 " + halfDay);
break;
case 1300:
JOptionPane.showMessageDialog(null, "1 " + halfDay);
break;
case 1400:
JOptionPane.showMessageDialog(null, "2 " + halfDay);
break;
case 1500:
JOptionPane.showMessageDialog(null, "3 " + halfDay);
break;
case 1600:
JOptionPane.showMessageDialog(null, "4 " + halfDay);
break;
case 1700:
JOptionPane.showMessageDialog(null, "5 " + halfDay);
break;
case 1800:
JOptionPane.showMessageDialog(null, "6 " + halfDay);
break;
case 1900:
JOptionPane.showMessageDialog(null, "7 " + halfDay);
break;
case 2000:
JOptionPane.showMessageDialog(null, "8 " + halfDay);
break;
case 2100:
JOptionPane.showMessageDialog(null, "9 " + halfDay);
break;
case 2200:
JOptionPane.showMessageDialog(null, "10 " + halfDay);
break;
case 2300:
JOptionPane.showMessageDialog(null, "11 " + halfDay);
break;
case 2400:
JOptionPane.showMessageDialog(null, "12 " + halfDay);
break;
default:
JOptionPane.showMessageDialog(null, "Hours only, please try again.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment