Date Conversion/Formatting Problems

Asked

Viewed 80 times

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 setVencimento gets a Date just pass calendar.getTime() for that is already a Date no need to parse. Can’t parse a Date which is what you’re trying to do

  • A detail, when you do new Date(), is creating a Date corresponding to the current date/time. And when calling Calendar.getInstance(), is creating a Calendar corresponding to the current date/time. Then create the Date and then move on to Calendar with setTime is redundant and unnecessary (it would only make sense if the Date did not correspond to the current date/time).

1 answer

2

If the method expects a type util.Date, Why convert? Pass the Date direct by method getTime() class Calendar, because she already returns a type util.Date and you don’t even have to create that much code:

meuPojo.setVencimento(calendar.getTime());

If you have a problem with incomparable types, check the Mports if you are not using the class sql.Date or instead of util.Date.


Still, I recommend you learn about the new java date API, because she’s a lot easier to deal with and she’s more precise.

  • I can’t pass new Date(), I need the dates that are inside Calendar, it receives dates according to some conditions, so I have to format below.

  • @Java you didn’t mention this in the code or in the question.

  • I wrote up there, in the comments of the code

  • @Java then go direct gettime, the return is already a Date.

  • more ai he returns me that date Tue Nov 22 00:00:00 BRST 2017, ai wanted to format it in dd/MM/yyyy

  • @Java you said that the method gets a Date type, it is not formatted that way. To format as you want, you need to convert to string. One-night stand in this answer to better understand.

  • 1

    @Java the problem is not in set but in the place where you write/show the date. This is where you have to apply the formatting. @Articuno Very interesting this API recommendation of dates, which I personally didn’t know.

  • is that it would be easier to save it format there, the hard to convert to string, is that then setVencimento will not accept it.

  • @Java see the Datehelper class that Victor did in response to a problem I had in this post, she will probably solve your problem.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.