Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1.  
  2. public class Tiempo {
  3.     static private boolean formato24h = false;
  4.     private int horas, minutos, segundos;
  5.    
  6.     public int getMinutos()
  7.     {
  8.         return minutos;
  9.     }
  10.    
  11.     private boolean esPM()
  12.     {
  13.         return horas >= 12;
  14.     }
  15.    
  16.     public int getHoras()
  17.     {
  18.         return horas;
  19.     }
  20.    
  21.     public int getSegundos()
  22.     {
  23.         return segundos;
  24.     }
  25.    
  26.     public void setTiempo(int h, int m, int s)
  27.     {
  28.         if (!(h >= 0 && h <= 23))
  29.             throw new IllegalArgumentException("Mal puesta la hora.");
  30.         if (!(m >= 0 && m <= 59))
  31.             throw new IllegalArgumentException("Mal puesto los minutos.");
  32.         if (!(s >= 0 && s <= 59))
  33.             throw new IllegalArgumentException("Mal puesto los segundos.");
  34.        
  35.         horas = h;
  36.         minutos = m;
  37.         segundos = s;
  38.     }
  39.    
  40.     public void incremSegundos()
  41.     {
  42.         segundos++;
  43.         segundos %= 60;
  44.     }
  45.    
  46.     public void incremMinutos()
  47.     {
  48.         minutos++;
  49.         minutos %= 60;
  50.     }  
  51.    
  52.     public void incremHoras()
  53.     {
  54.         horas++;
  55.         horas %= 24;
  56.     }
  57.    
  58.     public String toString()
  59.     {
  60.         String aux = "";
  61.  
  62.         if (!formato24h)
  63.         {
  64.             if (esPM())
  65.                 aux = String.format("%02d:%02d:%02d pm", (horas==12 ? 12: horas), minutos, segundos);
  66.             else
  67.                 aux = String.format("%02d:%02d:%02d am", horas, minutos, segundos);
  68.         }
  69.         else
  70.             aux = String.format("%02d:%02d:%02d", horas, minutos, segundos);
  71.        
  72.         return aux;
  73.     }
  74.    
  75.     public void setFormato24h(boolean cond)
  76.     {
  77.         formato24h = cond;
  78.     }
  79.        
  80.     public static void main(String[] args) {
  81.         Tiempo t = new Tiempo();
  82.         t.setFormato24h(true);
  83.         t.setTiempo(15, 54, 10);
  84.         System.out.println(t);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement