Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/02/2014, 02:25
SilverDante
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 4 meses
Puntos: 1
Respuesta: Cambiar formato de hora 24 a 12.

Voy a poner todo el código para que quede más claro lo que estoy preguntando.

Clase main:
Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reloj;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Joan
 */
public class Aplicacion {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner teclado=new Scanner(System.in);
        
        
        ArrayList <Reloj> Horas = new  ArrayList <Reloj>();

        int op=-1;
        while (op!=0)
        {
            
            do{

System.out.println("\n"+"        MENU        ");
verMenu();
op=teclado.nextInt();


switch(op){

                case 1: Reloj.ponerEnHora(Horas);
                        break;

                case 2: Reloj.imprimir24H(Horas);
                        break;

                case 3: Reloj.imprimir12H(Horas);
                        break;

                //case 4: Reloj.MostrarPersona(Horas);
                        //break;

                case 0: System.out.println("Se ha cerrado el programa.");
                        break;
    
default: System.out.println("Error" ); break;

}
}while(op!=0);
            
    }

}
    public static void verMenu(){
         System.out.println("1.Poner hora");
         System.out.println("2.Mostrar hora en formato 24");
         System.out.println("3.Mostrar hora en formato 12");
         System.out.println("4.Mostrar Persona");
         System.out.println("0.Salir");
         
   }
}
Clase reloj:
Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reloj;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author Joan
 */
public class Reloj {
    
    int hora=0;
    int minutos=0;
    int segundos=0;
    
     public static void ponerEnHora(ArrayList <Reloj> Horas){
        Reloj tiempo;

        tiempo=new Reloj();
        
        Scanner teclado = new Scanner(System.in);

        System.out.println("Introduce hora: ");
        tiempo.hora=teclado.nextInt();
        
        System.out.println("Introduce minutos: ");
        tiempo.minutos=teclado.nextInt();
        
        System.out.println("Introduce segundos: ");
        tiempo.segundos=teclado.nextInt();

        Horas.add(tiempo);
    }
     
    public static void imprimir24H(ArrayList <Reloj> Horas){
        
        int i=0;

        Iterator it = Horas.iterator(); //Porque utilizar esto --> http://goo.gl/EfvDx0
        if (!it.hasNext())
            System.out.println("No hay horas almacenadas."+"\n");
        else
         while (it.hasNext())
         {
            it.next();
            System.out.println("Tiempo nº: " +(i+1)                      +"\n"+
                               "Hora:   " +Horas.get(i).hora    +"\n"+
                               "Minutos: " +Horas.get(i).minutos +"\n"+
                               "Segundos: " +Horas.get(i).segundos  +"\n\n");
            i++;
         }
    
    }
    
    public static void imprimir12H(ArrayList <Reloj> Horas){
        
        int i=0,h=0;

        Iterator it = Horas.iterator(); //Porque utilizar esto --> http://goo.gl/EfvDx0
        
            
        
        if (!it.hasNext()){
            System.out.println("No hay horas almacenadas."+"\n");
        }
        else{
         while (it.hasNext())
         {
            it.next();
            i=i+1;
            h=((int)Horas.get(i).hora);
            h=h-12;
            
            System.out.println("Tiempo nº: " +(i)                      +"\n"+
                               "Hora:   " +Horas.get(i).hora    +"\n"+
                               "Minutos: " +Horas.get(i).minutos +"\n"+
                               "Segundos: " +Horas.get(i).segundos  +"\n\n");
            i++;
         }
        }
    
    }
    
}