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
Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido
Accepted Solution

Date/String Conversions

[ Edited ]

Hi. I've been trying to figure this out all day and I'm at the end of my rope. I'm basically calling a web service from my app, and I must send a Date object as a parameter. When I create a date object like this:

 

Date myDate = new Date();

 

And try to display the date (myDate.ToString()), it gives me an unwanted format. Feb 15 15:00 America/New York etc........ I basically want my Date in the format "yyyy-mm-dd hh-mm-ss +0000" or something similar (yyyy/mm/dd, dd-mm-yyyy), it doesn't matter. How do I convert a string into a Date, or at least set the Date objects format to the above??? I've looked around everywhere and people mention SimpleDateFormat.parse (something I apparently can't even use as my IDE doesn't recognize this method) and people using Calendar objects. I can't seem to find a straight answer (if there exists a straight answer). Any help at all would be greatly appreciated.

 

I've also tried HttpDateFormat (or whatever it's called) with no results at all, same weird format when I say Date.ToString(). And HttpDateFormat doesn't produce a Date object it produces a Long. I know you can say Date myDate = new Date(long), but it gives me the same weird format I mentioned above.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Date/String Conversions

SimpleDateFormat is definitely the path you want to take, but you need to use SimpleDateFormat.format() to do it. http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/i18n/SimpleDateFormat.html has a list of all of the formatting you can do with it, but here's some code to start you off.

 

 

Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd h:mm:ss");
String formattedDate = formatter.format(now, new StringBuffer(), null).toString();

 

 

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Date/String Conversions

You'll need to come up with the "format" for it but you would use the SimpleDateFormat to handle this. For example, there is a C standard library function called ctime. This is how I convert a Calendar to that format:

public static String ctime(Calendar timer)
{
	//ctime format: "Www Mmm dd hh:mm:ss yyyy" and appends \n to the end
	return new SimpleDateFormat(EEE MMM dd hh':'mm':'ss yyyy'\n')).format(timer, new StringBuffer(), null).toString();//Ex: Sun Mar 08 2:10:20 2006
}

Make sure you have the API documentation open. I usually have http://www.blackberry.com/developers/docs/6.0.0api/index.html open and just look at when it was introduced in the API to see if I can use it (It's the "Since: BlackBerry API ?.?.?" thing at the bottom).

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido

Re: Date/String Conversions

Thanks for the response. The only constructors for the Date object that I'm aware of either takes no arguments (default) or one Long as an argument. There's no way to initialize a Date with a string or anything else. So forgive me for sounding "tired" but even if I were to set up a format with the SimpleDateFormat, how would I go about applying it to my Date object, because I'm definitely sending a Date to my web service. The .Net web service I'm calling is actually expecting a DateTime object but Date should be fine. Also, are you able to use SimpleDateFormat.parse(string) ??? I can't and I was told that there's no way I can get it. Which I find strange because other's have mentioned using it.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Date/String Conversions

So you're receiving a formatted date and trying to create a Date object from it?

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Date/String Conversions

I explain in your other thread. As for some example code:

String currentDate = "Bla";
long time = HttpDateParser.parse(currentDate);

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(time));

//The formatter code jprofitt and I have provided examples of.
---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido

Re: Date/String Conversions

jprofitt, yes eventually I will be trying to do that. But right now I'm trying to send a Date object as a parameter to my web service call. I'm not receving any data back as I fear the Date may not be in a proper format, hence my questions on the forums. I have the web service call running fine on another mobile platform (won't mention names but it starts with i) so the web service is fine. I'm sending a Date object, not a string.

Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido

Re: Date/String Conversions

Guys I really appreciate the help and the sample code, but I don't see how this all falls into place (it's Friday and It's been a long day at work). You show examples of SimpleDateFormats, a String containing a format and Calendar methods which is all fine, but when does my Date object get created and set to the right format? Please feel free to say no to this, but would it be possible to show me a more complete example including an actual Date object interacting with the rest of your code (something concrete that I can send as a parameter). My love of problem solving is one of the reasons I got involved in comp science, but today I just need to get this done. Have a great weekend both of you. Thanks and sorry.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Date/String Conversions

Calendar.getTime() returns a Date object

Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido

Re: Date/String Conversions

Great, thank you. That's all I needed to know.