02-04-2011 03:50 PM - edited 02-04-2011 03:53 PM
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.
Solved! Go to Solution.
02-04-2011 03:57 PM
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
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd h:mm:ss");
String formattedDate = formatter.format(now, new StringBuffer(), null).toString();
02-04-2011 03:58 PM
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
02-04-2011 04:09 PM
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.
02-04-2011 04:12 PM
So you're receiving a formatted date and trying to create a Date object from it?
02-04-2011 04:16 PM
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.
02-04-2011 04:45 PM
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.
02-04-2011 05:00 PM
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.
02-04-2011 05:04 PM
Calendar.getTime() returns a Date object
02-04-2011 05:09 PM
Great, thank you. That's all I needed to know.