Date conversion / formatting issues

2

I'm having trouble formatting a date to be useful, I need to format a date that is Tue Nov 22 00:00:00 BRST 2017 (example) to date in dd/MM/yyyy format.

I've been seeing some links, such as: How to convert one string in date or date? but it 's not working, I think I' m not knowing how to use them.

        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;
        }
    
asked by anonymous 08.09.2017 / 02:24

1 answer

2

If the method expects a util.Date type, why do conversion? Pass% direct% by method Date of class getTime() , since it already returns a type Calendar and does not even have to create this much code:

meuPojo.setVencimento(calendar.getTime());

If you run into incompatible types, check the imports if you are not using the util.Date class instead of sql.Date .

Still, I recommend that you learn about the new java date API because it is much easier and is more accurate.

    
08.09.2017 / 02:32