Why do I get a date-to-string error?

0

Good day I have a question about why mark error does not let me convert the string data I tried with this:

String fecha_termino;
Calendar fechaInicialcalendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date fecha_fin = fechaInicialcalendar.getTime();
fecha_termino=format.parse(fecha_fin);

And I also tried this way:

Date fecha_fin = fechaInicialcalendar.getTime();
fecha_termino=fecha_fin.toString();
format.parse(fecha_termino);

In the first it says "incompatible Types: Date can not coverted to String" and in the second it does String but it does not give the format "dd / MM / yyyy". I hope someone can solve my doubt.

    
asked by David Melo 07.03.2018 в 05:20
source

1 answer

1

Greetings David,

  

The first one says "incompatible Types: Date can not coverted to String"

What happens is that the parse method is used to convert a text string ( String ) to Date , not the other way around, which will result in an incompatible conversion. Instead of this method, try to use the format method:

fecha_termino = format.format(fecha_fin); // convierte Date a String
  

in the second it does String but it does not give the format "dd / MM / yyyy"

In this case, what happens is that you are not assigning any type of format, but you are converting your date to a text string, and then converting it back to a date.

    
answered by 07.03.2018 / 05:30
source