02-19-2010 03:43 PM
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
Solved! Go to Solution.
02-19-2010 03:51 PM
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
02-19-2010 05:14 PM
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.
02-19-2010 07:52 PM - edited 02-19-2010 07:56 PM
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
02-19-2010 10:22 PM
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);
02-22-2010 02:16 PM
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...