Need help with Time

First I get the current time in milliseconds using:

System.currentTimeMillis()

Then I convert it to hh:mm:ss format using the following code:

public String millisecondsToString(long time)

{

int seconds = (int)((time/1000) % 60);

int minutes = (int)((time/60000) % 60);

int hours = (int)((time/3600000) % 24);

String secondsStr = (seconds<10 ? "0" : "")+seconds;

String minutesStr = (minutes<10 ? "0" : "")+minutes;

String hoursStr = (hours<10 ? "0" : "")+hours;

return hoursStr+":"+minutesStr+":"+secondsStr;

}

The minutes and seconds are correct, but the hour is 2 hours too slow.

Any ideas what I've done wrong?

Thanks:-)

Message was edited by:

paulinnorway

[779 byte] By [paulinnorwaya] at [2007-11-27 5:37:16]
# 1

Probably you are not in the UTC Timezone...

the javadoc of System.currentTimeMillis says: "...Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC..."

I suggest using SimpleDateFormat or String.format to do the job:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

String asString = sdf.format(new Date());

orString asString = String.format("%tT", System.currentTimeMillis());

[]

S_i_m_ua at 2007-7-12 15:09:10 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the reply simu :-)

I'll have a look at those soon.

It seems like I get the correct time when I use a Date object and SimpleDateFormat, but when I use other methods I'm 2 hours behind.....odd.

Can anyone recomend a Class that is best for using time, especially one that allows you to increase/decrease the time by several minutes. At this stage of my Java leaning it seems like there limitations on each and also I find that it's hard to get things in the same "type". I've messed around with Date objects, Calendar objects, longs, and strings.......and I still don't know which is best to use.

Hopefully in a few days I'll have a better understanding of the "big picture" when using time, but at the moment I'm hoping someone can give me a point in the right direction.

paulinnorwaya at 2007-7-12 15:09:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> It seems like I get the correct time when I use a

> Date object and SimpleDateFormat, but when I use

> other methods I'm 2 hours behind.....odd.

well, as I (tryed) wrote before, you are probably not in a region using UTC, but currentTimeMillis is returned in UTC. Maybe you are in Europe? At least in Germany I got 2 hours behind = time diference to UTC (GMT).

Also have a look at java.util.Calendar

[]

S_i_m_ua at 2007-7-12 15:09:10 > top of Java-index,Java Essentials,Java Programming...
# 4

I had a much closer look at Calendar and I had much more success with that.

I really liked CalendarObject.add(int field, int amount) for increasing the time by a certain amount.

I've changed the program to use mostly Calendar objects so I haven't had any problems with correct times any more.

Thanks again Simu for taking the time to give advice.

paulinnorwaya at 2007-7-12 15:09:10 > top of Java-index,Java Essentials,Java Programming...