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
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Convert string from military time to AM/PM

Hello,

 

I am trying to write a method that will convert a time (in military format) to an AM/PM time.  The format of the original time is:

17:00:00

 

After conversion, I would like it to be:

 

5:00 PM

 

I have the following method so far:

 

 

public String formatTime(String time) {
		String result;

		if (!isNullOrBlank(time)) {
			Date date = new Date(HttpDateParser.parse(time));
			DateFormat sdf = new SimpleDateFormat("h:mm a");

			if (date == null) {
				result = time;
			} else {
				result = sdf.format(date).toLowerCase();
			}
		} else {
			result = time;
		}

		return result;

	}

 

Now, this will format the time without a problem, however the time is wrong.  This is because it is in a GMT format (as this post explains, the time will be formatted based on the local time zone: http://supportforums.blackberry.com/t5/Java-Development/How-to-get-the-current-time-in-GMT/m-p/18328...)

 

Does anyone have any experience formatting time without taking into consideration time zone?

 

Thanks.

 

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Convert string from military time to AM/PM

 

SimpleDateFormat takes the timezone into account, but you can use the Timezone class to remove it again.

DateFormat is timezone-independent, but does not offer as many format options.

for your case http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/i18n/DateFormat.html#TIME_SHOR... should work, however.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Convert string from military time to AM/PM

Hi simon_hain,

 

I'm not sure that I'm using TimeZone correctly.  I've tried two methods, using "17:00:00" as the base time.  In the end, this should be converted to "5:00 PM":

 

Method #1:

 

DateFormat dateFormat = new SimpleDateFormat(DateFormat.TIME_SHORT);
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.setTime(new Date(HttpDateParser.parse(time)));
String formattedDate = dateFormat.format(c, new StringBuffer(), null).toString();
return formattedDate;

 ...This yields: 12:00 am

 

Method #2:

 

result = new SimpleDateFormat(DateFormat.TIME_SHORT).format(new Date(HttpDateParser.parse(time)));

 ...This yields: 7:00 pm

 

Do I just need to fiddle with Method #1 until I find the correct Time Zone which has an offset equal to 5:00 PM?

 

Thanks.

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Convert string from military time to AM/PM

I suspect the results you are getting from the HttpDateParser parse are in fact all 0.  Which means the GMT time you will get is Midnight Jan 1 1970, and so the Time you will get is 12:00 am.  However I suspect your local time is 5 hours before GMT, so when outputting in local time, you get 7:00 pm the previous day. 

 

Do you want to check that?

If you are getting 0, then I think you are going to have to parse the date yourself - which is actually not that hard and there have been a few Threads for people attempting to do just this.

Developer
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Convert string from military time to AM/PM

You're right, peter_strange, I am in fact receiving 0 from the HttpDateParser.  I had hoped it wouldn't come to parsing the date myself, however I'm always up for a challange!  I will look around on the forums for those threads that you mentioned. Thanks!

Developer
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: Convert string from military time to AM/PM

[ Edited ]

I think just simple thing to do is manually format string :

 

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String eventDateString;

 

// GMT will always be supported by getTimeZone

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

eventDateString = dateFormat.format(calendar.getTime());

Calendar calendarLocal = Calendar.getInstance();
eventDateString = dateFormat.format(calendarLocal.getTime());
           
timeString = formatTime(eventDateString);

public String formatTime(String time) {
        String result;
        StringBuffer buffer = new  StringBuffer();
        int hour =  Integer.parseInt(time.substring(0,2));
        String amPM = null;
        if(hour > 12)
        {
            amPM = " PM";
            buffer.append(hour-12);
        }
        else
        {
            amPM = " AM";
        }
        buffer.append(":");
        buffer.append(time.substring(3,5));
        buffer.append(amPM);
        return buffer.toString();
    }

 

Regards,
Arsen
Developer
Posts: 144
Registered: ‎03-15-2011
My Device: blackberry smart phone
My Carrier: Airtel

Re: Convert string from military time to AM/PM

i was searching something like that and get here .... thanks arsenk01
Developer
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: Convert string from military time to AM/PM

[ Edited ]

I did some mistakes in above code,so here is the right one:

private String formatTime(String time) {
    StringBuffer buffer = new StringBuffer();
    int hour = Integer.parseInt(time.substring(0, 2));
    String amPM = null;
    if (hour > 12) {
	amPM = " PM";
	buffer.append(hour - 12);
    } else {
	buffer.append(hour);
	amPM = " AM";
    }
	buffer.append(":");
	buffer.append(time.substring(3, 5));
	buffer.append(amPM);
	return buffer.toString();
}

 

 

Regards,
Arsen