2
I’m having trouble formatting a date for useful, I need to format a date that this Tue Nov 22 00:00:00 BRST 2017 (example) for date in format dd/MM/yyyy.
I’ve been seeing some links, like: How to convert a string to date? but it’s not working, I believe I don’t know how to use - los.
Date data = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(data);
//eu ultilizo o calendar em alguns calculos, depois eu seto ele no meu pojo.
//porém ele vem nesse formato, exemplo: Tue Nov 22 00:00:00 BRST 2017
//quero converter em dd/MM/yyyy
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
meuPojo.setVencimento(sdf.parse(calendar.getTime())); // minha tentativa de converter, porém da erro de: "incompatible types: Date cannot be converted to String"
//exemplo de como é o meu método setVencimento
public void setVencimento(Date data){
this.data = data;
}
If the
setVencimentogets aDatejust passcalendar.getTime()for that is already aDateno need to parse. Can’t parse aDatewhich is what you’re trying to do– Isac
A detail, when you do
new Date(), is creating aDatecorresponding to the current date/time. And when callingCalendar.getInstance(), is creating aCalendarcorresponding to the current date/time. Then create theDateand then move on toCalendarwithsetTimeis redundant and unnecessary (it would only make sense if theDatedid not correspond to the current date/time).– hkotsubo