help error can not format given object as a date

1
 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given      
Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at examen.rutina.RutinaArchivos.obtenerNombreZip(RutinaArchivos.java:85)
at examen.rutina.RutinaArchivos.main(RutinaArchivos.java:80)

This is my class in which I execute

 public class RutinaArchivos {



public static void main(String[] args) {

    GestorTexto gTexto = GestorTexto.getInstance();
    GestorSerialJava gSerial = GestorSerialJava.getInstance();
    GestorXml gXML = GestorXml.getInstance();

    FileUtils fUtils = FileUtils.getInstance();
    String rutaCarpeta = "C:/reportes";
    fUtils.crearCarpetaSiNoExiste(rutaCarpeta);

    // litsa de los objetos
    List<Dispositivo> listaDispositivo = new ArrayList<Dispositivo>();
    listaDispositivo.add(new Movil("ABC123", new Date(), "Samsung",
            "galaxy s7", 'n', true, 'e', "1225616"));
    listaDispositivo.add(new Enrutador("XSD45", new Date(), "IPHONE", "6S",
            'C', true, 153, "1235.15.", 15));

    // definjir ruta
    String rutacarpeta = "C:/reportes/";
    String rutaReporte = rutacarpeta + "reporte.txt";
    final String SEPARADOR_LINEA = System.getProperty("line.separator");

    // crear  archivo de texto
    StringBuilder contenido =  new StringBuilder();
    contenido.append("numero de dispositivos encontrados")
    .append(listaDispositivo.size())
    .append(SEPARADOR_LINEA)
    .append("fecha de resporte:")
    .append(obtenerfecharepo())
    .append(SEPARADOR_LINEA)
    .append("codigo\tDispositivo\tTipo\tMarca\tModelo")
    .append(SEPARADOR_LINEA);
    System.out.println(contenido);
    //Recorrer 
    for (Dispositivo d : listaDispositivo) {
        contenido.append(d).append(SEPARADOR_LINEA);
        //guarda  xml  y ser
        if (d instanceof Movil) {
            gXML.escribirArchivo(rutaCarpeta+ ".xml",d);
            gSerial.escribirArchivo(rutaCarpeta+ ".ser",d);
        }

    }
    System.out.println(contenido);

    // guardar el archivo
    gTexto.escribirArchivo(rutaReporte, contenido.toString());

    //hacer reporte
    String rutaRespaldo ="c:/respaldo/";
    fUtils.comprimirContenidoDeCarpeta(rutaCarpeta, rutaRespaldo+obtenerNombreZip());
}

private static String obtenerNombreZip() {
 SimpleDateFormat formateador= new  SimpleDateFormat("dd-mm-yyyy");
 return formateador.format(new Date()+".zip");
}

private static Object obtenerfecharepo() {
    SimpleDateFormat formateador = new SimpleDateFormat();
    return formateador.format(new Date());
}
}
    
asked by Lrawls 21.10.2016 в 21:50
source

2 answers

3

The error is in the obtenerNombreZip() method. You are concatenating ".zip" before to format the date:

return formateador.format(new Date()+".zip");

Rather, you have to concatenate ".zip" after to format the date:

return formateador.format(new Date()) + ".zip";

Otherwise, new Date()+".zip" produces a String at a time with a value that is not accepted by SimpleDateFormat.format() .

    
answered by 21.10.2016 / 21:56
source
3

I think you are asking him to format the date taking the month as minutes, try changing the method line getZipName () to:

SimpleDateFormat formateador= new  SimpleDateFormat("dd-MM-yyyy");

That is, change the "mm" to "MM". Check the documentation for SimpleDateFormater .

    
answered by 21.10.2016 в 21:55