Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Trusted Contributor
KJake
Posts: 156
Registered: 11-21-2010
My Carrier: Virgin Mobile

HttpDateParser and SimpleDateFormat

English locale the string date is "Oct 27, 2011 10:20 AM"

French locale the string date is "27 oct. 2011 10:20"

 

I need to get "2011-10-27 10:20 AM" from both strings.  I've tried using httpdatparser and simpledateformat but contin to get a 1969 date.  Any ideas?

 

 

Please use plain text.
Developer
searingmedia
Posts: 152
Registered: 08-06-2010
My Carrier: Sasktel

Re: HttpDateParser and SimpleDateFormat

sdf should be able to handle either, give us an example of your code.

Riley
Please use plain text.
Trusted Contributor
KJake
Posts: 156
Registered: 11-21-2010
My Carrier: Virgin Mobile

Re: HttpDateParser and SimpleDateFormat

Here is the code that produces the 1969 date...

 

String a = "Oct 21, 2011 10:30 AM";

Dialog.inform(StringToDate(a));

 

 

   

publicstatic String StringToDate(String dateToParse) {

        Date formatter =

new Date(HttpDateParser.parse(dateToParse));

        SimpleDateFormat dateFormat =

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        formatter.setTime(formatter.getTime());

        String strCustomDateTime = dateFormat.format(formatter);

       

return strCustomDateTime;

    }

Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: HttpDateParser and SimpleDateFormat

check if the httpdateparser can correctly parse your string, i.e. if the Date formatter contains the correct timestamp.
otherwise you have to adjust your string or parse it manually.

formatter.setTime(formatter.getTime()); seems to be quite useless btw.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.
Trusted Contributor
KJake
Posts: 156
Registered: 11-21-2010
My Carrier: Virgin Mobile

Re: HttpDateParser and SimpleDateFormat

Thanks Simon.  The string is coming directly from the OS based on language.  I assume httpdateparser should understand all formats that come the OS based on local.  My concern is that if my app is to be bilingual (en / fr) and I need a consistent date format regardless of English of French, how do I produce this consistent format (yyyy-MM-dd HH:mm:ss) when Eng displays a date one way and French another way?

Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: HttpDateParser and SimpleDateFormat

you should check your "understanding" against the api, the parser is limited:
http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/io/http/HttpDateParser.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.
Trusted Contributor
KJake
Posts: 156
Registered: 11-21-2010
My Carrier: Virgin Mobile

Re: HttpDateParser and SimpleDateFormat

Thanks. Do you have any recommendations on how I can get a consistent date format regardless of language on drvice? Appreciate any feedback. Thanks a lot.
Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: HttpDateParser and SimpleDateFormat

what is the source of your date?
i usually have a date-object (or long value) which i can format as i like to.
currently i don't understand your usecase.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.
Trusted Contributor
KJake
Posts: 156
Registered: 11-21-2010
My Carrier: Virgin Mobile

Re: HttpDateParser and SimpleDateFormat

I do store the date as a long (Long answerDateTime) then put that Long into a DateField.

 

DateField  dfAnswerDateTime = new DateField("", answerDateTime.longValue(), DateField.DATE_TIME);

 

I then use dfAnswerDateTime.toString(); to get the answer date as a string.  Only issue is that is is completely different format based on language.  Is there a parameter I can use in the DateField to maintain an exact format regardless of language?

 

I appreciate your assistance.  Thanks much.

Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: HttpDateParser and SimpleDateFormat

well, don't use toString, use SimpleDateFormat with the Date created from the long value.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.