02-23-2009 06:44 PM
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/mess
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.
02-23-2009 06:58 PM
I'm pretty certain that HTTPDateParser will handle your format - have you tried it?
02-23-2009 07:19 PM - edited 02-23-2009 07:21 PM
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
02-23-2009 08:18 PM
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; }
07-22-2009 02:12 AM
RexDoug i understood ur code, but how can i convert m_checkinDate back to desired date format?
01-20-2011 05:36 AM
Can you please provide an example?