BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-02-2010, 04:33 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Time Difference Calculation ?

Please Login to Remove!

Time Difference Calculation is not showing correct result. What am I doing wrong here?

Code:
             
private long targetTime; 
private long curr;
private long result;
private SimpleDateFormat tf = new SimpleDateFormat("hh:mm:ss");
curr=System.currentTimeMillis();
targetTime= curr+ 60000;
result=targetTime-curr;
Dialog.alert(tf.formatLocal(result));
The result is supposed to be 00:01:00 but this gives 07:01:00
Offline  
Old 09-02-2010, 04:49 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

formatLocal() is most likely adjusting the time to reflect the current time zone set on the device (this is what it does).

Using formatLocal is not the correct way to show a time difference.

You should probably create your own helper class to successively divide out the number of days, hours, minutes, etc.
Offline  
Old 09-02-2010, 07:57 PM   #3 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Quote:
Originally Posted by Dougsg38p View Post
formatLocal() is most likely adjusting the time to reflect the current time zone set on the device (this is what it does).

Using formatLocal is not the correct way to show a time difference.

You should probably create your own helper class to successively divide out the number of days, hours, minutes, etc.
Doug is quite right, but if you really want to use a date format class I wrote this to format time in Zulu:
Code:
public class ZuluDateFormat extends SimpleDateFormat {
    private static boolean  offsetComputed = false;
    private static TimeZone gmt;
    
    private boolean appendZulu;
    
    public ZuluDateFormat() {
        super("H+mm");
        construct();
    }
    
    public ZuluDateFormat(String format) {
        super(format);
        construct();
    }
    
    private void construct() {
        appendZulu = false;
        if (!offsetComputed) {
            gmt = TimeZone.getTimeZone("GMT");
            offsetComputed = true;
        }
    }
    
    public void setAppendZulu(boolean append) {
        appendZulu = append;
    }
    
    public StringBuffer format(Calendar calendar, StringBuffer sb, FieldPosition fp) {
        calendar.setTimeZone(gmt);
        StringBuffer buf = super.format(calendar, sb, fp);
        if (appendZulu) {
            buf.append(" Zulu");
        }
        
        return buf;
    }
    
}
You can use it in date fields if you want to fix the timezone regardless of changes in location or configuration of the device, or using GMT IIRC it will behave the way you want.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 09-08-2010, 03:56 PM   #4 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not able to use your code to calculate the time difference using Calendar.

Pls guide me.

Code:
private long targetTime; 
	private long curr;
	private long result;
	private SimpleDateFormat tf = new SimpleDateFormat("hh:mm:ss");	
             curr=System.currentTimeMillis();
	Dialog.alert("Current : "+tf.formatLocal(curr));
	targetTime= curr+ 60000;
	Dialog.alert("Target : "+ tf.formatLocal(targetTime));
	result=targetTime-curr;
	Dialog.alert("Result :" + tf.formatLocal(result));
My question here is that the first and second alerts are showing the correct time format as I wanted like 04:51:15 and 04:52:15. But the last alert gives the result 07:01:00. It should be 00:01:00, right? Since all the formatLocal() methods are using the same long type, then why is only this result different?
Offline  
Old 09-08-2010, 04:19 PM   #5 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

I think you might be making this harder than it really is.

The time difference is expressed as some number of milliseconds.

seconds = millis / 1000
minutes = seconds / 60
hours = minutes / 60

...etc
Offline  
Old 09-09-2010, 08:22 AM   #6 (permalink)
New Member
 
RockoAndy's Avatar
 
Join Date: Sep 2010
Model: 8900
PIN: N/A
Carrier: Airtel
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi romah,

The reason u were getting that value is u were using 12 hr format = hh:mm:ss use hh:mm:ss:a to gibe u the am pm.

24hr format = HH:mm:ss

the 60000 milli seconds is actually transformed to 12:01:00 in the 12 hr format and will be converted to 00:01:00 in 24hr format.

SimpleDateFormat tf = new SimpleDateFormat("hh:mm:ss:a");
long curr=System.currentTimeMillis();
Dialog.alert("Current : "+tf.formatLocal(curr));
long targetTime= curr+ 60000;
Dialog.alert("Target : "+ tf.formatLocal(targetTime));
long result=targetTime-curr;
Dialog.alert(""+result);
Dialog.alert("Result :" + tf.formatLocal(result));

this will return u 12:01:00 a
__________________
the amazing world of
Offline  
Old 09-09-2010, 08:45 AM   #7 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

Quote:
Originally Posted by romah View Post
I am not able to use your code to calculate the time difference using Calendar.

Pls guide me.

Code:
private long targetTime; 
	private long curr;
	private long result;
	private SimpleDateFormat tf = new SimpleDateFormat("hh:mm:ss");	
             curr=System.currentTimeMillis();
	Dialog.alert("Current : "+tf.formatLocal(curr));
	targetTime= curr+ 60000;
	Dialog.alert("Target : "+ tf.formatLocal(targetTime));
	result=targetTime-curr;
	Dialog.alert("Result :" + tf.formatLocal(result));
My question here is that the first and second alerts are showing the correct time format as I wanted like 04:51:15 and 04:52:15. But the last alert gives the result 07:01:00. It should be 00:01:00, right? Since all the formatLocal() methods are using the same long type, then why is only this result different?
Regardless of what timezone your Blackberry is using, times are stored in milliseconds from the "Unix epoch": midnight UTC January 1, 1970. So in your code result will have the value 60000 which means 1 minute after midnight UTC January 1, 1970. tf.formatLocal(result) formats that time as specified by your format "hh:mm"ss" in your Local Time Zone, which appears to be UTC+7 so you get 07:01:00, and if you displayed the date it would be January 1, 1970. If you use a date format that displays time in the UTC time zone, as my code does, then the display would be 00:01:00.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 09-09-2010, 01:02 PM   #8 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually I want to use built-in method.
It works fine if I use the custom function below.

RockoAndy, I already tried with your method (including a, aa etc). It gives 19:01:00
hrbuckley, I am not using Calendar. If I use Calendar, then how can I add time in current time and calculate the time different?
Dougsg38p, yes I was also thinking your method.

Code:
private long curr=System.currentTimeMillis();
private long targetTime= curr+ 60000;
	
private long result=targetTime-curr;
Dialog.alert(getDateFormat(result));
Here is the method to return the formatted date.

Code:
public String getDateFormat(long pTime)
	{
		long secondInMillis = 1000; 
		long minuteInMillis = secondInMillis * 60; 
		long hourInMillis = minuteInMillis * 60; 
		StringBuffer result=new StringBuffer();
		
		long hrs = pTime / hourInMillis; 
		if(hrs<10)
		{
			result.append("0"+hrs+":");
		}
		else
		{
			result.append(hrs+":");
		}
		pTime = pTime % hourInMillis; 
		
		long mins = pTime / minuteInMillis; 
		if(mins<10)
		{
			result.append("0"+mins+":");
		}
		else
		{
			result.append(mins+":");
		}
		pTime = pTime % minuteInMillis; 
		
		long secs = pTime / secondInMillis;
		if(secs<10)
		{
			result.append("0"+secs);
		}
		else
		{
			result.append(secs);
		}
		return result.toString();
	}
Offline  
Old 09-09-2010, 01:44 PM   #9 (permalink)
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts
Default

You don't have to use Calendar, ZuluDateFormat takes care of that for you. You use ZuluDateFormat instead of SimpleDateFormat:

Code:
private ZuluDateFormat tf = new ZuluDateFormat("hh:mm:ss");
curr=System.currentTimeMillis();
targetTime= curr+ 60000;
result=targetTime-curr;
Dialog.alert(tf.formatLocal(result));
The magic of object oriented programing.
__________________
My other Blackberry is a PlayBook.
Offline  
Old 09-09-2010, 02:55 PM   #10 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2010
Model: 8530
PIN: N/A
Carrier: Sprint
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I have tried your code in same way. The simple mistake was "hh:mm:ss" instead of "HH:mm:ss". I even didn't think to change in 24 hrs format and immediately I went to RockoAndy's solution. He was using 24 hrs format but not using ZuluDateFormat(). So again not correct output. Anyway, this time I got correct output using this code.

Code:
private ZuluDateFormat tf = new ZuluDateFormat("HH:mm:ss");
curr=System.currentTimeMillis();
targetTime= curr+ 60000;
result=targetTime-curr;
Dialog.alert(tf.formatLocal(result));
It's really magic of OOP.
Thank you so much hrbuckley and all other friends.

Last edited by romah : 09-09-2010 at 03:00 PM.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.