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: 207
Registered: ‎07-31-2009
My Device: Not Specified

Convert String to date...

I have a string in the follwing format :   "5 Dec 1987 09:56" 

How do i convert this to date? 

 

I tried  :

 

 

date.setDate(Long.parseLong("5 Dec 1987 09:56")); 

 

But this throws NumberFormatException . 

 

------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Convert String to date...

your code can not work. Long.parse longs just needs a long Smiley Happy

 

You would need some kind of DateParser.

There is one in the API but I think the supported format are a little bit differen (check out HttpDateParser)

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 207
Registered: ‎07-31-2009
My Device: Not Specified

Re: Convert String to date...

[ Edited ]

Yes i did check that , but the format that it supports are all different ( ones that are supported by http 1.1). The thing is at one point in my app i use the net.rim.device.api.ui.component.DateField to create a dateField. This values is later read and placed in a string. This string is again required to populate another datefield. The problem is it needs to be in string format during the intermediate step. Otherwise i would ve retained the original object .

------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Convert String to date...

You could get the date directly as long from the datefield!?

Or use a different format via setDateFormat which you can better parse later

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Convert String to date...

Just adding to what dpreussier has said.

 

You said: "The problem is it needs to be in string format during the intermediate step"

 

Does the String in this intermediate step need to be recognized by anything as a Date, or can it be any format at all as long as it is a String?  Is any format is fine, then the easiest thing to do is to convert the long to String (Long.toString()) and then parse it back (long.parseLong()).

Developer
Posts: 207
Registered: ‎07-31-2009
My Device: Not Specified

Re: Convert String to date...

The thing is the datefield is a part of a  field manager. The value is got by parsing through the manager like :

VerticalFieldManager.getField(index).toString() . When the value is retrieved it comes like how i mentioned in the precious post. So i am not sure how to convert it to long as you mention , but otherwise your idea looks excellent...Smiley Happy

------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Convert String to date...

String fieldString = null;

Field testField = VerticalFieldManager.getField(index);

if ( testField instanceof DateField ) {

DateField convertDate = (DateField) testField;

fieldString = Long.toString(convertDate.getDate());

} else {

fieldString = testField.toString();

}

 

If you review what dpressieur said in his first post, I think you will find that this is basically what he suggested too.

Developer
Posts: 207
Registered: ‎07-31-2009
My Device: Not Specified

Re: Convert String to date...

[ Edited ]

Am sorry. This was something that i was trying as well...But....

 

Quote : Simon

 

String fieldString = null;

Field testField = VerticalFieldManager.getField(index);

if ( testField instanceof DateField ) {

DateField convertDate = (DateField) testField;  //  Error : Cannot cast from Field to DateField 

fieldString = Long.toString(convertDate.getDate());

} else {

fieldString = testField.toString();

}

 

 

------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Convert String to date...

[ Edited ]

"DateField convertDate = (DateField) testField;  //  Error : Cannot cast from Field to DateField "

 

Hm.  This is interesting because I have production code that does just that.

 

Are you seeing a compile error or Runtime Error?

 

Can you confirm that you are using the correct DateField and not the javax.microedition.lcdui.DateField.  Perhaps change the lines to the following:

 

if ( testField instanceof net.rim.device.api.ui.component.DateField ) {

net.rim.device.api.ui.component.DateField  convertDate = (net.rim.device.api.ui.component.DateField) testField;

...

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Convert String to date...

You might want to try HTTPDateParser - it will parse a wide variety of string date formats into a date object.