Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Time formatting / SimpleDateFormat

Thread view: 
erictetz@gmail.com - 13 Aug 2007 22:24 GMT
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
Thomas Fritsch - 13 Aug 2007 23:12 GMT
erictetz@gmail.com schrieb:
> 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
[quoted text clipped - 10 lines]
>
> 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.

Signature

Thomas

Lew - 14 Aug 2007 00:10 GMT
> "GMT" means London time.

Only in the winter time do they coincide.  Actually it means "the mean solar
time at longitude 0°".
<http://en.wikipedia.org/wiki/Time_zone#Standard_time_zones>
<http://en.wikipedia.org/wiki/UT1>

Signature

Lew

Roedy Green - 22 Aug 2007 12:20 GMT
>> "GMT" means London time.
>
>Only in the winter time do they coincide.  Actually it means "the mean solar
>time at longitude 0°".

When you mean standard time, say UTC which has no DST correction ever.
see http://mindprod.com/jgloss/timezone.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Thomas Fritsch - 14 Aug 2007 01:27 GMT
> 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°)"
> time would be "December 31, 1969, 16:00:00" in your American time zone.
> Hence the output would be correct.

Signature

Thomas

Roedy Green - 22 Aug 2007 12:19 GMT
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.