martes, 29 de mayo de 2012

Suma y resta de días

 private static void sumarRestarDias(){
      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
     Calendar calHoy= Calendar.getInstance();
     Calendar calAyer= Calendar.getInstance();
     Calendar calMannana= Calendar.getInstance();
     calHoy.setTime(new Date());
     calAyer.setTime(calHoy.getTime());
     calMannana.setTime(calHoy.getTime());
      System.out.println(dateFormat.format(calHoy.getTime()));
     int numeroDia=calHoy.get(Calendar.DAY_OF_WEEK);
     numeroDia -=1;
     System.out.println("fia de la semana: "+numeroDia);

     calAyer.add(Calendar.DATE,-31);
     calMannana.add(Calendar.DATE,(6-numeroDia));
     System.out.println("Fecha primera: "+dateFormat.format(calAyer.getTime()));
     System.out.println("Fecha hoy: "+dateFormat.format(calHoy.getTime()));
     System.out.println("Fecha ultima: "+dateFormat.format(calMannana.getTime()));
    }

Obtener día de la semana

private static void obtenerDiaSemana(){
      String[] dias={"Domingo","Lunes","Martes", "Miércoles","Jueves","Viernes","Sábado"};
        Date hoy=new Date();
      int numeroDia=0;
      Calendar cal= Calendar.getInstance();
      cal.setTime(hoy);
      numeroDia=cal.get(Calendar.DAY_OF_WEEK);
      System.out.println("hoy es "+ dias[numeroDia - 1]);
    }

Revisar formato de hora

private static boolean revisarFecha(String hora) {

        String[] dividirHora = new String[2];

        if (hora.contains(":")) {
            dividirHora = hora.split(":");
            if (dividirHora.length == 2) {

                if (dividirHora[0].equals("") || (Integer.parseInt(dividirHora[0]) < 0) || (Integer.parseInt(dividirHora[0]) > 24)) {
                    return false;
                }
                if (dividirHora[1].equals("") ||(Integer.parseInt(dividirHora[1]) < 0) || (Integer.parseInt(dividirHora[1]) > 60)) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;

    }

Comparar horas

 private static boolean compararHoras() {
        String horaInicial = "13:30";
        String horaFinal = "12:20";
        try {
            if (revisarFecha(horaInicial) && revisarFecha(horaFinal)) {
                DateFormat dateFormat = new SimpleDateFormat("HH:mm");
                Date horaIni;
                Date horaFin;
                horaIni = dateFormat.parse(horaInicial);
                horaFin = dateFormat.parse(horaFinal);
                if (horaIni.compareTo(horaFin) < 0) {
                    System.out.println("correcta");
                    return true;
                } else {
                    System.out.println("incorrecta");
                    return false;
                }
            } else {

                return false;
            }
        } catch (ParseException ex) {
            //Logger.getLogger(Main2.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Posee errores");
            return false;
        }

Transformar un string a Date

private static void fechaTrasformar(){
            SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy HH:mm");
            String strFecha="12/04/2012 13:30";
        Date fecha = null;
        try {

            fecha = formatoDelTexto.parse(strFecha);

        } catch (ParseException ex) {

            ex.printStackTrace();

        }

jueves, 17 de noviembre de 2011

martes, 15 de noviembre de 2011