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
dn8
Posts: 46
Registered: ‎06-16-2009
My Device: Not Specified
Accepted Solution

SimpleDateFormat from string question

What am I missing here?

 

SimpleDateFormat fmt = new SimpleDateFormat("h:mm a");
String time = fmt.formatLocal(HttpDateParser.parse("2010-02-19 10:33:13"));

 

returns 2:33 am

 

my phone is set -8 hours pacific time...

 

input time is 24 hour format.

 

What do I need to do to simply parse the string and return it in my desired format? 12 hour format!

 

10:33 AM

 

Developer
DAquilina
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: SimpleDateFormat from string question

It's not what YOU'RE missing -- it's what your project is missing. SimpleDataFormat, along with a fair number of other useful APIs, is part of J2SE, or Java Standard Edition. Since the BlackBerry is a mobile device, it uses J2ME, or Java Micro Edition. Unfortunately SimpleDataFormat does not exist as part of J2ME.

 

I tried that too lol. Don't feel too bad.

 

Good luck!

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: SimpleDateFormat from string question

Don't know about SimpleDataFormat, but SimpleDateFormat (Date, not Data) is alive and well in the Blackberry API....

 

I believe your problem is Time Zones.  The date is being processed as UTC (GMT with no daylight saving) and then formatted for local time.  

 

TimeZone.getOffset can be added to this, and then you can use SimpleDateFormat.

 

Alternatively you could just parse the string yourself.

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

Re: SimpleDateFormat from string question

[ Edited ]

Peter,

Rolling my own as you suggested was the fastest easiest way!

 

Here is the code, maybe this can help someone else...

 

String date = "2010-02-19 13:33:13";

String _24H = date.substring(11,13);
String Minutes = date.substring(13,16);

String _12H;
String ampm = "AM";
        
        if(_24H.intern() == "00")
        {
            _12H = "12";
        } 
        else 
        {
            int theHour = Integer.parseInt(_24H);
            
            if(theHour < 13)
            {
        	       _12H = String.valueOf(theHour);
            }
            else
            {
                _12H = String.valueOf((theHour - 10) - 2);
                ampm = "PM";
            }
        }

String formattedTime = _12H + Minutes + " " + ampm;

//1:33 PM

 

 

 

Developer
ShimmyShine
Posts: 46
Registered: ‎07-25-2009
My Device: Torch 9800

Re: SimpleDateFormat from string question

If all you want is the time with the daylight savings time & the timezone built in just use this:

 

 

    Date rightNow = new Date();
    SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getInstance(DateFormat.TIME_SHORT);
    String stringDate = sdf.format(rightNow);

 

 

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

Re: SimpleDateFormat from string question

Hi ShimmyShine,

Yes that formats the current time, but that is not what I was after.

 

I am given a date in the past, returned from a server request, that I need formatted from 24 hour to 12 hour...