BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-14-2010, 05:26 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert String to Date/Time

Please Login to Remove!

I am trying to get time(long type) from the given datetime string.
While searching, I found these two ways but both are not working for me.

Code:
//import net.rim.device.api.i18n.DateFormat;
//import java.text.DateFormat;
String dateString="9/14/2010 3:59:27 PM";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); 
Date date=df.parse(dateString);
long time=date.getTime();
In above code, the problem is in df.parse(dateString);
There is no parse method for net.rim.device.api.i18n.DateFormat.
and when I tried to import "java.text.DateFormat", it says "cannot be resolved", i.e., package not found.



Code:
String dateString="9/14/2010 3:59:27 PM";
Date date= new Date(HttpDateParser.parse(dateString));
long time=date.getTime();
In this code, the second line date will result 0 millisecond.

What will be the solution?
Offline  
Old 09-16-2010, 12:23 PM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found that it works fine if the dateString is changed to the format "yyyy-MM-dd hh:mm:ss".

Code:
String dateString="2009-07-31 07:59:17";
Date formatter = new Date(HttpDateParser.parse(dateString));
long time=formatter.getTime();
But our web service, which is not modifiable, returns the format "MM/dd/yyyy hh:mm:ss aa". Is there any work around or solution?

One more thing is that HttpDateParser.parse() is only accessible by signed applications. Although it won't be the problem for my application, again if there is any other way to solve it, please post the solution. Thanks !

Last edited by romah : 09-16-2010 at 12:30 PM.
Offline  
Old 09-16-2010, 02:51 PM   #3 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

Since the date format is fixed, why don't you just write your own parser? You'll extract the various date elements, place them in a Calendar instance, and then get the Date() from the calendar.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.