Time formatting / SimpleDateFormat

E

erictetz

My application is running an 'elapsed time' counter, which I would
like to display in HH:MM:SS format. My question is how best to do this
in Java. Note, I'm not using the latest version of Java which contains
printf (which makes this trivial).

I'm currently building the string by hand, but it's ugly. I tried
using SimpleDateFormat to do the work for me, but for reasons I don't
understand it adds 16 hours to the output. For example:

Date elapsedTime = Date(0); // 0 seconds elapsed, should be 00:00:00
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormat.format(elapsedTime));

This outputs 16:00:00. I have no clue how to get rid of the 16.

What am I doing wrong? Is there a cleaner approach for this?

Cheers,
Eric
 
T

Thomas Fritsch

My application is running an 'elapsed time' counter, which I would
like to display in HH:MM:SS format. My question is how best to do this
in Java. Note, I'm not using the latest version of Java which contains
printf (which makes this trivial).

I'm currently building the string by hand, but it's ugly. I tried
using SimpleDateFormat to do the work for me, but for reasons I don't
understand it adds 16 hours to the output. For example:

Date elapsedTime = Date(0); // 0 seconds elapsed, should be 00:00:00
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormat.format(elapsedTime));

This outputs 16:00:00. I have no clue how to get rid of the 16.
See <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(long)>
The date Date(0) is specified as "January 1, 1970, 00:00:00 GMT", where
"GMT" means London time.
If you live somewhere in America (6 hours west of London), then this
time would be "December 31, 1969, 16:00:00" in your American time zone.
Hence the output would be correct.
What am I doing wrong? Is there a cleaner approach for this?
Normally you won't use this Date(long) constructor, but instead some
other Date constructor or a Calendar method taking
year/month/day[/hour/minute] parameters. They all take your local
timezome into account, so that the problem should disappear.
 
T

Thomas Fritsch

Thomas said:
The date Date(0) is specified as "January 1, 1970, 00:00:00 GMT", where
"GMT" means London time.
If you live somewhere in America (6 hours west of London), then this
Aaah, I meant "(8 hours west of London)"
because 16 + 8 = 24.
And yes, Lew is correct: I should say "(8 hours west of longitude 0°)"
 
R

Roedy Green

My application is running an 'elapsed time' counter, which I would
like to display in HH:MM:SS format. My question is how best to do this
in Java. Note, I'm not using the latest version of Java which contains
printf (which makes this trivial).

see http://mindprod.com/jgloss/calendar.html

The problem is you have to pick a timezone. The date itself is in
UTC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top