Java TimeZone

4 November, 2008

Recently, i was working with J2ME apps to get current time system. I faced several confusion when i tried to execute the code below :

long time = System.currentTimeMillis(); did not return the current hours on my computer, instead it return the hours based on Standard Time +0000 UTC. That means, no matter which machine or device you try to retrieve the current time, it always give you the time based on UTC. You can check the current UTC time on this link.

Since, i am in Melbourne/Australia, where my current timezone is UTC+11 and begin of the day light savings. One way to workaround to get the current time is using Calendar and Date in Java:

Calendar myCal= Calendar.getInstance (TimeZone.getTimeZone ("UTC"));

//The point here is to calculate the minutes based on UTC+11
Date date = new Date (myCal.getTime().getTime() + (11*60*60*1000));

long time = date.getTime();
int milliseconds = (int) (time % 1000);
int seconds = (int) ((time / 1000) % 60);
int minutes = (int) ((time / 60000) % 60);
int hours = (int) ((time / 3600000) % 24);

The above result will return in milliseconds when you use date.getTime() to get the value.

Result : 01:10:22 (based on the time i complie and run the code)

Useful Link. The world Clock.
Tips : If it is the end of Day Light Savings, you should -1, that means, UTC 10 instead of UTC 11.

Entry Filed under: J2ME. Tags: , .

1 Comment Add your own

  • 1. Evans  |  7 November, 2008 at 1:38 AM

    Nice tip!

    The last time I developed in J2ME was 5 years ago – but I still enjoy seeing people working with it though.

    Good luck!

    Reply

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Charles Ling

Founder of ecomjunction.com

My Favourite Web

 

November 2008
M T W T F S S
« Oct   Jan »
 12
3456789
10111213141516
17181920212223
24252627282930

Categories

Tags

Adobe bitcomet book coding Firefox Intelligent Intelligent Web J2ME Java javascript javascript framework jQuery Jsp jsr 172 kite runner LWUIT Machine Learning Mysql novel Nuance Talks painted veil parse Personalization Photoshop PHP plug-in privacy programming protocol buffer proxy Security server tcp Technology Tomcat web web 2.0 Web Service web services wep Wireless WSDL wsf wso2 xml

Archives

Delicious Bookmark

Join ME