jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Adding 'time' to a timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding Watch "Adding New topic

Adding 'time' to a timestamp

Daniel Palombo

Joined: Feb 28, 2006
Posts: 10
Hello all,
I wanted do do this:

Timestamp in hh:mm:ss
00:00:59 + 00:00:05 = 00:01:04

I made this code

And this is the output:

I don't want this!!! I don't understand this.
How can I reach my goal?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Are you sure that's the output? This is what I get when I run your program.

java.text.ParseException: Unparseable date: "00:00:59"
at java.text.DateFormat.parse(DateFormat.java:335)
at timstampDemo.main(timstampDemo.java:11)
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I get the same error as Keith.

From this code, one thing that stand out to me is that the endTime is the start time plus the difference between the start time and end time, so that part should look like this:

and considering the output you are looking for, I would suggest using the SimpleDateFormat class to do your parsing/formating.

Although this use assumes all the times you wish to parse are in the 24 hour format.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Daniel Palombo

Joined: Feb 28, 2006
Posts: 10
Hello Keith Lynn and Garrett Rowe,
strange that it doesn't compile or run wrong on your machines, because I sartet it on another machine and it compiled and run well (did a copy and paste from this site):

C:\Temp>java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

C:\Temp>javac timstampDemo.java

C:\Temp>java timstampDemo


It's a Windows XP SP2 and the locale is german ...

I'am afraid I was misunerstood:
Let's say I recorded a time since the start of an event, so just 59 seconds passed since the start.
Now I just want to add to this 59 seconds another 5 seconds. So wath I get are 64 seconds, but we don't speak of 64 seconds, we say "One minute and four seconds". That's it: 1:04 or in a given format of HH:mm:ss -> 00:01:04

That's what I want.
By the way, using the SimpleDateFormat class the result is the same, as you can see from my quote.

So, what do you think is wrong here, and - more important - how can I convince java to do what I want?
Daniel Palombo

Joined: Feb 28, 2006
Posts: 10
I've got it!

... and must admit that I'soooo 'asinine'

I didn't realise that time in java is relative to the 1.1.1970 !!! So what I did was adding a _day_ to another, resulting perhaps in a day in the fututre ... I really was !!!

I just have to add 5 seconds. That is adding 5000 millisekonds.
This does the trick:

Sometimes talking about a problem is like clening your glasses.
Thanx Ranch!
I agree. Here's the link: http://aspose.com/file-tools
subject: Adding 'time' to a timestamp
It's not a secret anymore!