I've been looking at questions similar to this, but I can't find the key. I hope you can help me.
In an exercise I am asked to override the toString() method so that LocalDateTime appears like this:
2020/01/17T01:57:22.579
...instead of like this...
2020-01-17T01:57:22.579
I have tried in various ways. Mainly two:
public String getFecha(){
return fecha.toString();
}
@Override
public String toString(){
return("YYYY/MM/ddEHH:mm:ss.SSS");
}
And also like this...
public String getFecha(){
return fecha.toString();
}
@Override
public String toString(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY/MM/ddEHH:mm:ss.SSS");
String resultado = fecha.format(dtf);
return(resultado);
}
But I do not get any change in how the date is displayed. Thank you very much!
EDIT: After the first two answers I've had, and which I greatly appreciate, I've kept trying and I still can't get the desired date format: It keeps coming out by default.
For all this, I have decided to edit and put all the code that I have in the class, to see if the problem is in another place.
package a_basico;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Movimiento {
private LocalDateTime fecha;
private final String dni;
private final double cantidad;
private final String descripcion;
public Movimiento(double montante, LocalDateTime hoy, String dniOp, String descr){
this.fecha = hoy;
this.dni = dniOp;
this.cantidad = montante;
this.descripcion = descr;
}
public Movimiento(double montante, String dniOp, String descr){
this.fecha = LocalDateTime.now();
this.dni = dniOp;
this.cantidad = montante;
this.descripcion = descr;
}
public double getCantidad(){
return cantidad;
}
public String getFecha(){
return fecha.toString();
}
public String getDescr(){
return descripcion;
}
@Override
public String toString(){
String str = this.fecha.format(DateTimeFormatter.ofPattern("YYYY/MM/ddEHH:mm:ss.SSS"));
return str;
}
/*
@Override
public String toString(){
String resultado = fecha.toString().replace('-', '/');
return resultado;
}
*/
/*
@Override
public String toString(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY/MM/ddEHH:mm:ss.SSS");
String resultado = fecha.format(dtf);
return(resultado);
}
*/
/*
@Override
public String toString(){
return "YYYY/MM/ddEHH:mm:ss.SSS";
}
*/
/*
@Override
public String toString(){
String resultado = fecha.format(DateTimeFormatter.ofPattern("YYYY/MM/ddEHH:mm:ss.SSS"));
return resultado;
}
*/
}
In the end, commented, I have been leaving all my failed attempts.
Thanks again.
I have tried some of your attempts and all of them have given me a result similar to what I expected, but with a small problem in the format, such as this:
For the same I have changed the format to:
And the result is:
I have overwritten the method
toString()
similar to one of your attempts.I have used the class
Movimiento
that you have shared and I have created the objectmovi
and called the methodtoString()
in this way:Result in the IDE (I find myself using NetBeans):
In the same way it has served in an online compiler
EDIT:
When calling the method
getFecha()
, you get the format incorrectly, as you mention, since you are making the calltoString()
that ownsLocalDateTime
You could do the getDate() method like this:
Although
toString()
being the method of the class itMovimiento
should return the complete information of the object:Result:
You could use the replace(oldChar, newChar) method to replace the characters you need.
Something like that:
You cannot override the method
toString()
of the classLocalDateTime
. It is declared asfinal
, and a classfinal
cannot be extended to overload (@Override
) the methodtoString()
.The most you can get is to make a wrapper that has a
LocalDateTime
as an attribute, and make a@Override
methodtoString()
of that wrapper class. (Similar to what @Iván-Salgado did.)