File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding 'time' to a timestamp

 
Daniel Palombo
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
23:01:04



I don't want this!!! I don't understand this.
How can I reach my goal?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Palombo
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
23:01:04

C:\Temp>

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
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic