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
Developer
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Converting a string to a Date

I want to take a string in this format: "yyyy-MM-dd HH:mm:ss" and get a Date object.  However, DateFormat.parse isn't supported  (http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=619&query.id=2604...).

 

Also, it seems that HttpDateParser.parse does not allow you to specify the format of the string that you want to parse.

 

How can I get a date from a string and specify the format?

 

Thank you.

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Converting a string to a Date

I'm pretty certain that HTTPDateParser will handle your format - have you tried it?

Developer
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Re: Converting a string to a Date

[ Edited ]

It worked in the simulator, but when run on a device it offsets by 5 hours (I assume because the format I give it expects GMT in there somewhere).

 

Here's the code:

 

 

Date eventDate = new Date(HttpDateParser.parse(eventString)); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String eventDateString = dateFormat.format(eventDate); System.out.println("xml date=" + eventString + ", parsed string=" + eventDateString);

 

"xml date" should equal "parsed string" but I get:

 

xml date=2009-02-23 18:26:32, parsed string=2009-02-23 13:26:32
xml date=2009-02-23 18:20:37, parsed string=2009-02-23 13:20:37
xml date=2009-02-23 18:20:24, parsed string=2009-02-23 13:20:24
xml date=2009-02-23 18:20:07, parsed string=2009-02-23 13:20:07
xml date=2009-02-23 18:19:45, parsed string=2009-02-23 13:19:45
xml date=2009-02-23 18:19:38, parsed string=2009-02-23 13:19:38
xml date=2009-02-23 18:19:17, parsed string=2009-02-23 13:19:17
xml date=2009-02-23 18:13:37, parsed string=2009-02-23 13:13:37
xml date=2009-02-23 18:13:07, parsed string=2009-02-23 13:13:07
xml date=2009-02-23 18:07:15, parsed string=2009-02-23 13:07:15
xml date=2009-02-23 18:06:55, parsed string=2009-02-23 13:06:55
xml date=2009-02-23 18:06:07, parsed string=2009-02-23 13:06:07
xml date=2009-02-23 18:05:36, parsed string=2009-02-23 13:05:36
xml date=2009-02-23 18:02:33, parsed string=2009-02-23 13:02:33
xml date=2009-02-23 18:02:05, parsed string=2009-02-23 13:02:05
xml date=2009-02-23 18:00:13, parsed string=2009-02-23 13:00:13
xml date=2009-02-23 17:59:25, parsed string=2009-02-23 12:59:25

 

Message Edited by bo on 02-23-2009 07:21 PM
Highlighted
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Converting a string to a Date

You just need to make a timezone adjustment.

 

You are probably in GMT -5 (Eastern time).

 

Here is a similar code snippet that adjusts for time zone:

/** * Set the checkin date * @param strDate Date in the format YYYY-MM-DD hh:mm -tttt */ public void setCheckinDate(String strDate) { // get the checkin date (local time) m_checkinDate = HttpDateParser.parse(strDate); StringBuffer tz = new StringBuffer(); int blanks = 0; int len = strDate.length(); //int index = 0; for (int i = 0; i < len; i++){ char c = strDate.charAt(i); if (blanks == 2){ tz.append(c); } if (c == ' ') blanks++; } // parse the string int zone = Integer.parseInt(tz.toString()); // adjust to be # of hours zone = zone / 100; // get time zone object String strZone = "GMT" + Integer.toString(zone); m_timeZone = TimeZone.getTimeZone(strZone); // get the raw GMT offset for this time zone int adj = m_timeZone.getRawOffset(); // adjust the checkin date so that it is GMT time m_checkinDate -= adj; }

 

Developer
Posts: 46
Registered: ‎06-23-2009
My Device: Not Specified

Re: Converting a string to a Date

RexDoug i understood ur code, but how can i convert m_checkinDate back to desired date format?

New Contributor
Posts: 9
Registered: ‎01-18-2011
My Device: Not Specified

Re: Converting a string to a Date

Can you please provide an example?