11-30-2009 04:13 AM
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 .
11-30-2009 04:19 AM
your code can not work. Long.parse longs just needs a long ![]()
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)
11-30-2009 04:30 AM - edited 11-30-2009 04:31 AM
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 .
11-30-2009 04:38 AM
You could get the date directly as long from the datefield!?
Or use a different format via setDateFormat which you can better parse later
11-30-2009 04:55 AM
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()).
11-30-2009 05:58 AM
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...![]()
11-30-2009 06:22 AM
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.
11-30-2009 06:43 AM - edited 11-30-2009 06:46 AM
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();
}
11-30-2009 08:23 AM - edited 11-30-2009 08:24 AM
"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;
...
11-30-2009 08:35 AM
You might want to try HTTPDateParser - it will parse a wide variety of string date formats into a date object.