Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Tiempo {
- static private boolean formato24h = false;
- private int horas, minutos, segundos;
- public int getMinutos()
- {
- return minutos;
- }
- private boolean esPM()
- {
- return horas >= 12;
- }
- public int getHoras()
- {
- return horas;
- }
- public int getSegundos()
- {
- return segundos;
- }
- public void setTiempo(int h, int m, int s)
- {
- if (!(h >= 0 && h <= 23))
- throw new IllegalArgumentException("Mal puesta la hora.");
- if (!(m >= 0 && m <= 59))
- throw new IllegalArgumentException("Mal puesto los minutos.");
- if (!(s >= 0 && s <= 59))
- throw new IllegalArgumentException("Mal puesto los segundos.");
- horas = h;
- minutos = m;
- segundos = s;
- }
- public void incremSegundos()
- {
- segundos++;
- segundos %= 60;
- }
- public void incremMinutos()
- {
- minutos++;
- minutos %= 60;
- }
- public void incremHoras()
- {
- horas++;
- horas %= 24;
- }
- public String toString()
- {
- String aux = "";
- if (!formato24h)
- {
- if (esPM())
- aux = String.format("%02d:%02d:%02d pm", (horas==12 ? 12: horas), minutos, segundos);
- else
- aux = String.format("%02d:%02d:%02d am", horas, minutos, segundos);
- }
- else
- aux = String.format("%02d:%02d:%02d", horas, minutos, segundos);
- return aux;
- }
- public void setFormato24h(boolean cond)
- {
- formato24h = cond;
- }
- public static void main(String[] args) {
- Tiempo t = new Tiempo();
- t.setFormato24h(true);
- t.setTiempo(15, 54, 10);
- System.out.println(t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement