Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified
Accepted Solution

Problem with converting getDate() to Date format?

Hi Guys,

 

I need to convert the selected date from DateField to some string format (for example: yyyy-mm-dd).

 

I had set the DateField date format as "mm-dd-yyyy'.

 

I tried to get the date from that DateField and tried to convert it into "yyyy-mm-dd" format.

 

When I tried I got some wrong output.

 

For example, when I selected 12 as month it returned as "00" with the following coding.

 

 

SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-mm-dd"); Date dtToConvert = new Date(mydatefield.getDate()); strRequired = dtFormat.formatLocal(dtToConvert);

 

 

 Can anyone explain what is the bug with this code or any other way to get the output as my requirement?

Thanks in advance,

Sivalingaraja

 

New Developer
Posts: 14
Registered: ‎07-10-2009
My Device: Not Specified

Re: Problem with converting getDate() to Date format?

This is code I have used in the past :

 

DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); long dateLong = field.getDate(); String dateString =df.format(dateLong);

 

 

That should give you thedate String you want.

Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: Problem with converting getDate() to Date format?

Thanks for the reply.

 

I am using eclipse plugin for the development for Storm.

 

When I tried to use the date format as like you given it showing the error.

 

  dtFormat.format(this.ctlDate.getDate());

 

The method format(Object) in the type Format is not applicable for the arguments (long)

 

 

Can you tell whether it is correct code?

 

 

New Developer
Posts: 14
Registered: ‎07-10-2009
My Device: Not Specified

Re: Problem with converting getDate() to Date format?

Oops, sorry, code should have been...

 

 

DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); long dateLong = field.getDate(); Date date = new Date(dateLong); String dateString =df.format(date);

 

 

Highlighted
Developer
Posts: 157
Registered: ‎07-09-2009
My Device: Not Specified

Re: Problem with converting getDate() to Date format?

Thanks bigkebabman,

 

It works good.

 

Have a nice day..Smiley Tongue