miércoles, 6 de febrero de 2013

Fechas en Java fácil o dificil?

Metodo logico Dificil():


    public static String fecha() {
        Date fecha = new Date();
        String f = "";
        String d = "";
        String m = "";
        String min = "";
        String hr = "";

        int dia = fecha.getDate();
        int minuto = fecha.getMinutes();
        int segundos = fecha.getSeconds();
        int mes = fecha.getMonth() + 1;
        int hora = fecha.getHours();

        if (dia < 10) {
            d = "0" + dia;
        } else {
            d = "" + dia;
        }
        if (minuto < 10) {
            min = "0" + minuto;
        } else {
            min = "" + minuto;
        }
        if (mes < 10) {
            m = "0" + mes;
        } else {
            m = "" + mes;
        }

        if (hora < 10) {
            hr = "0" + hora;
        } else {
            hr = "" + hora;
        }

        f = d + "-" + m + "-" + (fecha.getYear() + 1900) + "\t" + hr + ":" + min+":"+segundos;

        return f;
    }


Metodo muy Facil():


    public static String fecha() {
        Calendar c1 = GregorianCalendar.getInstance();
        System.out.println("Fecha actual: " + c1.getTime().toLocaleString());
        SimpleDateFormat formatoDate = new SimpleDateFormat("dd-MM-yyyy");
        String fecha = formatoDate.format(c1.getTime());
        System.out.println("Fecha Formateada: " + fecha);

        return fecha;
    }

No hay comentarios:

Publicar un comentario