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: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon
Accepted Solution

How do I get the device's current milliseconds from GMT milliseconds?

How do I design the following?

I need to get the device's current milliseconds from GMT milliseconds.


I read the KB article "Get time zone offsets with DST" at http://supportforums.blackberry.com/t5/Java-Development/Get-time-zone-offsets-with-DST/ta-p/445189


I believe I need to start with System.currentTimeMillis() and get the GMT with either of the following?

Is this a proper design?

 

 

Calendar rightnow = Calendar.getInstance(TimeZone.getDefault());

// With DST
int offsetDST = TimeZone.getDefault().getOffset(1, rightnow.get(Calendar.YEAR), rightnow.get(Calendar.MONTH), rightnow.get(Calendar.DAY_OF_MONTH), rightnow.get(Calendar.DAY_OF_WEEK), rightnow.get(Calendar.MILLISECOND));

// Without DST
int offset = TimeZone.getDefault().getRawOffset();

 

 

 

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: How do I get the device's current milliseconds from GMT milliseconds?

[ Edited ]

Unless I'm misunderstanding what you're trying to do, System.currentTimeMillis() should do what you need (UTC and GMT are pretty much the same).

 

Edit: read your post again, I did misunderstand...This is what happens when I don't get enough coffee in the morning!

Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: How do I get the device's current milliseconds from GMT milliseconds?

[ Edited ]

*Slaps forehead* then walks away to get coffee.

 

Thank you, I appreciate your help - kudos to you.

 

Edit: No, I think this is what I need?

public static long currentTimeMillis()

Returns the current time in milliseconds.

 

Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

 

 

Edit: No, this didn't work. Still working on this.

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: How do I get the device's current milliseconds from GMT milliseconds?

milliseconds are always in GMT. If you need your time in something other than GMT you need to use a Date, Calendar or a String timestamp.

Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: How do I get the device's current milliseconds from GMT milliseconds?

Understood, however does the System.currentTimeMillis() actually return the difference between the current milliseconds of the device from GMT milliseconds?

 

That's what I need.

 

Or do I need to create a GMT calendar, get the milliseconds from that calendar and subtract that value from the milliseconds value from System.currentTimeMillis()?

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: How do I get the device's current milliseconds from GMT milliseconds?

System.currentTimeMillis() gets the milliseconds since the Epoch, which is always GMT What you are looking for is the timezone offset. Look at the Timezone class and the rawOffset options to get the offset hours for the device.

Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: How do I get the device's current milliseconds from GMT milliseconds?

 

TimeZone tz = TimeZone.getDefault();
Calendar c = Calendar.getInstance(tz);
int offset = 0;

if (tz.useDaylightTime()) {
offset = TimeZone.getDefault().getOffset(1, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),
c.get(Calendar.DAY_OF_WEEK), c.get(Calendar.MILLISECOND));
} else {
offset = TimeZone.getDefault().getRawOffset();
}

long current_seconds = System.currentTimeMillis()
/ DateTimeUtilities.ONESECOND;

int gmt_seconds = offset / DateTimeUtilities.ONESECOND;

deviceSecondsFromGMT = gmt_seconds - current_seconds;

 

I think I'm close, but offset keeps returning the value of '0' when I evaluate the expression within the if and else conditions using the default or GMT Timezone.

 

 

Is what I have above the proper way to get the offset for the device set for it's time zone? (I'm in EST.)

How do I properly use the offset value once I figure out why it is the value of '0'?

 

I do apologize, I know this has been talked about on this board several times, I'm just having a hard time wrapping my head around it.

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: How do I get the device's current milliseconds from GMT milliseconds?

You may have better luck if you use getOffset rather than getRawOffset.

Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: How do I get the device's current milliseconds from GMT milliseconds?

From:

 

 

if (tz.useDaylightTime()) {
			offset = TimeZone.getDefault().getOffset(1, c.get(Calendar.YEAR),
					c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),
					c.get(Calendar.DAY_OF_WEEK), c.get(Calendar.MILLISECOND));
		} else {
			offset = TimeZone.getDefault().getRawOffset();
		}

 

 

I did evaluate offset from within the if condition and that expression value was still 0. At the moment, tz.useDaylightTime is returning false - so I understand what you're pointing out.

 

However, I don't know if offset is set correctly?

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: How do I get the device's current milliseconds from GMT milliseconds?

Make sure your simulator is set to use the right timezone.