My question is : can a DTO have instance format data methods ?
For instance:
public class CosasDTO{
String cosa_nombre;
String cosa_apellido;
String cosa_fecha;
///-------------------------Constructor
public CosasDTO(CosaExtrema cExtrema) {
cosa_nombre = cExtrema.getName();
cosa_apellido = cExtrema.getApellido();
cosa_fecha = formatDate(cExtrema.getDate());
}
private String formatDate(Timestamp fechaHora) {
String horaFechaFormateadas = new SimpleDateFormat("yyyyMMddhhmmss").format(fechaHora);
return horaFechaFormateadas;
}
}
Is it correct to instantiate a formatting method within the DTO?
Formatting/Localization is the responsibility of the presentation (endpoint) layer so in general no. While persistence you save the date/time in UTC and format it in the presentation layer based on the user’s preference.