0

I'm implementing some services in JAVA to a call center. I need to record the duration of a call so I'm using this to do that:

last = Instant.now();
Duration duration = Duration.between(first, last);
tfinal=Timestamp.from(last);
Long minutos;
Long hours;
duracion = "00 horas y ";
minutos = duration.toMinutes();
if (minutos > 60) {
    hours = duration.toHours();
    duracion = hours.toString() + " horas y ";
}
duracion = duracion + minutos.toString() + " minutos";

Which works perfectly...the thing is I have a cancel button on my screen which cleans all my fields or reset them to 0....What I haven´t figure out is how to return my last variable to 0... or something that clears this field which contains my last Instant type variable. I can set to "" my duracion variable because it is a String...but how can I achieve this with my java.time.Instant variabale? I'm also using primefaces.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Liz Castillo
  • 59
  • 1
  • 9
  • I know, I was about to write "but that's irrelevant" :3 – Liz Castillo Feb 21 '18 at 22:13
  • 2
    This may sound like an [XY Problem](http://xyproblem.info/). What makes you think you need to clear `last`? If you keep `last` local to the method where you use it, I guess it would eliminate any problem with clearing it, is that right? – Ole V.V. Feb 22 '18 at 11:37
  • 1
    As an aside I don’t think your code works perfectly. With a duration of 70 minutes I get a `duracion` of `1 horas y 70 minutos` — not intended, was it? BTW I find it confusing to have variables `duration` and `duracion`, don’t you? – Ole V.V. Feb 22 '18 at 11:43
  • 1
    To answer the question, there is no such thing as a zero or emtpy `Instant`. You may set the varialbe to `null`, of course, you just need to make sure that your code can handle that. I don’t think it’s the best solution. Not knowing your design it’s hard to give better suggestions. – Ole V.V. Feb 22 '18 at 11:49
  • That's right, if I keep `last` local I will not have to clear it. The thing is I am displaying it in the view. I have to display The initial time, the end time and the duration of the call. When you just mentioned "there is no such thing as a zero or empty Instant" I just realized how silly it sounds to _empty_ an `Instant` -.- I guess I will have to do the thing I was avoiding: create another variable which stores only the string value and can be cleared. x_x – Liz Castillo Feb 22 '18 at 14:57
  • And yes, you're right, the approach for the minutes-hours problem is just sad x_x ... I haven´t realized either, I think I need some sleep (haha!) thank you @Ole V.V. ... – Liz Castillo Feb 22 '18 at 15:10

0 Answers0