I have a DateConverter
class:
public class DateConverter extends StringConverter<LocalDate> {
String pattern = "EEE, dd. MM. uuuu";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern,Locale.US);
@Override
public String toString(LocalDate object) {
try {
return formatter.format(object);
} catch(Exception ex) {
return "";
}
}
@Override
public LocalDate fromString(String string) {
try {
return LocalDate.parse(string, formatter);
} catch(Exception ex) {
return null;
}
}
public String getPattern(){
return pattern;
}
}
And I have this piece of code:
allProd.forEach((prod) -> {
DateConverter dc = new DateConverter();
LocalDate onMarket = dc.fromString(dc.toString(prod.getOnMarket()));
System.out.println("localdate:" + onMarket);
System.out.println("string:"+dc.toString(onMarket));
}
Which prints this:
localdate:2012-11-08
string:Thu, 08. 11. 2012
localdate:2011-11-13
string:Sun, 13. 11. 2011
localdate:2002-04-11
string:Thu, 11. 04. 2002
What I want is that my LocalDate
value is formatted like the string value is. Because I have a class which has a LocalDate
field and the field should always be formatted. So I don't want to change the field datatype to string.