2

Let's say i have several TimeStamp objects and i want to know the difference between 2 of them in terms of minutes and seconds.

TimeStamp timestamp1 = new TimeStamp(System.currentTimeMillis());
TimeStamp timestamp2 = new TimeStamp(System.currentTimeMillis());

if i use the method getSeconds (but the concept can be adapted to minutes and hours too), for example, the expected output is "SS".

But when i try to get the difference between the 2 objects (something like:

long differenceInSeconds = (timestamp1.getseconds() - timestamp2.getSeconds());

Now the expected output is just the difference between 2 numbers, so clearly it can be 0, 5, 13, ...

What if i wanted to always have values formatted in XX? 0 would be 00, 13 would be 13 and going on.

Maybe there is some class, or method,regarding timestamp which i'am unaware of?

  • 1
    what is meant by '*the expected output is "SS"*'? Do you mean something like `String.format("%02d", differenceInSeconds)`? – user16320675 Apr 01 '22 at 11:24
  • 1
    sorry for not explaining well. I mean 2 digits, i will try your suggestion and let you know if this works - thanks!! –  Apr 01 '22 at 11:30
  • 1
    BTW I would prefer using the classes of the `java.time` package and its subpackages... if possible – user16320675 Apr 01 '22 at 11:32
  • 2
    Hint: numbers are always just that: numbers. YOu are asking about REPRESENTATION, in other words: string formatting. – GhostCat Apr 01 '22 at 11:52
  • 1
    @GhostCat thanks, yes i think Representation is more accurate :) –  Apr 01 '22 at 12:25
  • 1
    @user16320675 i will look into this. Do you have any recommendation about this specific problem regarding a difference between 2 "time" objects representation? –  Apr 01 '22 at 12:27
  • I too recommend you don’t use `Timestamp`. That class is poorly designed and long outdated. For a timestamp you typically use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Then you may also use `ChronoUnit.SECONDS.between(instant1, instant2)` to get the difference in seconds. – Ole V.V. Apr 06 '22 at 12:21
  • Also the `getSeconds` method is deprecated 25 years ago for good reasons and also does not give you what you want here. `ChronoUnit.SECONDS.between()` does. – Ole V.V. Apr 06 '22 at 12:26

2 Answers2

2

I think the reason why it is not padded is because the Method returns an int value, which is not padded by default. The only way to get a padded value is converting your number to a string and then add leading zeros.

K. K.
  • 136
  • 7
  • 1
    i thought the same solution, i will first try the solution from the comment above (String.format), if that doesn't work i will tr to handle it as a string. Thanks for commenting, can't upvote because of low reputation. –  Apr 01 '22 at 11:32
  • 1
    This doesn't really answer the question. If you don't provide more information, such as example code, it should rather be a comment. Answers are supposed to ANSWER the question. – GhostCat Apr 01 '22 at 11:53
  • The explanation in this answer is nice. And the other answer has wrong code. I consider no code better than wrong code. Admitted, this answer does not directly answer the question, I still consider it helpful. – Ole V.V. Apr 06 '22 at 17:07
1

What you want can be achieved in the following way

long differenceInSeconds = (timestamp1.getseconds() - timestamp2.getSeconds());
String.format("%02d", differenceInSeconds)

The flag %02d means it will convert it into a 2 digit string if the passed number is less than 2 digits. So if you pass 5 it will be converted to string 05. If you pass 13 it will remain 13.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • Yes it solved my problems! I had to the conversion long to string and then to re-assign this value, but it worked. Thanks! –  Apr 01 '22 at 13:14
  • I used `Timestamp` objects `2022-04-04 10:59:59.8` and 0.4 seconds later, `2022-04-04 11:00:00.2`. Expected result: `00`. Observed result: `59`. You have been taking over the errors from the question. – Ole V.V. Apr 06 '22 at 17:05
  • @OleV.V. Why do you think the answer is wrong. Op question was how he could present `differenceInSeconds` in a format of always 2 digits. Op already had provided the code to calculate the `differenceInSeconds` – Panagiotis Bougioukos Apr 06 '22 at 17:15
  • Για σασ, your second code line is correct and fine and helpful. I think it’s fair to let current and future readers who want to use your answer know that you have taken over the errors from the question so that the first line isn’t correct — neither in the question nor in your answer. – Ole V.V. Apr 07 '22 at 05:58