-3

I have duration parsed and ready, Duration is of type java.time.Duration with this as input I have tried to calculate end datetime by adding LocalDateTime.now() with duration (java.time.duration). But haven't got appropriate results

Was able to see that there is something on similar lines with ReadableDuration, but not much documentation on how to use it.

java.time.Duration duration = java.time.Duration.parse(timeinISOSTD);
Date localDate =  (Date) duration.addTo(java.time.LocalDateTime.now());
String stringDate = localDate.toString();

I need an end date-time Or end date object which will help me in determining SLA end DateTime.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 4
    "haven't got appropriate results" is not a useful description. What, specifically, did you expect, and what, specifically, did you obtain? What is the value of `timeinISOSTD`? – Amadan Jul 30 '19 at 03:48
  • 2
    Shouldn't that cast to `Date` fail? – Slaw Jul 30 '19 at 03:59
  • 2
    and more, what is `Date` (java.util or java.sql, or ...) - actually it should not be possible to cast a `LocalDateTime` (returned by `addto`) to `Date` - why not `LocalDateTime.now().plus(duration)` which does not require cast.... – user85421 Jul 30 '19 at 04:01
  • 2
    **Don't. Use. `Date`.** You are on the right track by already using classes from the `java.time` package, but you should also drop `Date` altogether. Unless you absolutely have to – but you don't. – MC Emperor Jul 30 '19 at 21:22
  • @OleV.V. done, but hard to know what really are the expected "appropriate results" – user85421 Jul 30 '19 at 23:13
  • 1
    Be aware that `LocalDateTime` is for generic 24-hour days, and *not* for actual moments. If your tracking actual elapsed time, you should be using `Instant`, `OffsetDateTime`, or `ZonedDateTime` rather than `LocalDateTime`. – Basil Bourque Jul 31 '19 at 03:05
  • What do you mean by "SLA"? – Basil Bourque Jul 31 '19 at 03:06
  • What do you mean by `ReadableDuration`? There is no such class bundled with Java. – Basil Bourque Jul 31 '19 at 03:20
  • @BasilBourque SLA = service level agreement, `ReadableDuration` confusing something from Joda-Time. Not that my guesses are better than yours… – Ole V.V. Jul 31 '19 at 05:08
  • 1
    @OleV.V. It is up to the OP to explain their use of acronyms and their relevance. – Basil Bourque Jul 31 '19 at 06:47

2 Answers2

4

I will suppose that parsing is working, that is, duration has got a valid value. The statement

(Date) duration.addTo(java.time.LocalDateTime.now());

should throw an Exception. According the documentation of addTo

This returns a temporal object of the same observable type as the input...

it must be returning a LocalDateTime which is not a Date, that is, the cast will throw an Exception (should have been posted with the question, so I can only assume that was the problem).

Simple solution:

LocalDateTime localDate = (LocalDateTime) duration.addTo(java.time.LocalDateTime.now());

Or, even better, also as suggested by the same documentation

In most cases, it is clearer to reverse the calling pattern...

that is:

LocalDateTime localDate = LocalDateTime.now().plus(duration);

Note: to get this formatted as a String use the format method with a DateTimeFormatter for the needed representation, e.g. localDate.format(DateTimeFormatter.ofPattern("HH:mm:ss"))

user85421
  • 28,957
  • 10
  • 64
  • 87
4

tl;dr

Instant                   // Represent a moment in UTC, with a resolution of nanoseconds.
.now()                    // Capture the current moment as seen in UTC. Returns an `Instant` object.
.plus(                    // Add a span-of-time, resulting in a different moment.
    Duration              // Represent a span-of-time unattached to the timeline on the scale of hours-minutes-seconds.
    .parse( "PT3H" )      // Parse a string in standard ISO 8601 format. Returns a `Duration` object.
)                         // Returns another `Instant` object.

Details

You seem to be unwisely mixing different kinds of data types.

  • LocalDateTime represents a date and a time-of-day without a time zone or offset-from-UTC. So it cannot represent a moment. Do not use this class if you are tracking actual moments in time.
  • Date, both java.util.Date and java.sql.Date, are terrible legacy classes that should never be used. They were supplanted by the modern java.time classes years ago with the adoption of JSR 310. Specifically, Instant replaces the first, and LocalDate replaces the second.
  • Duration represents a span of days (in terms of 24-hour chunks of time, not calendar days), hours, minutes, seconds, and fractional second. For a span-of-time in terms of years-months-days, use Period.

LocalDateTime.now()

I cannot think of any situation where this call is the right thing to do. As mentioned above, this class by definition cannot represent a moment. So trying to capture the current moment as LocalDateTime is almost certainly the wrong thing to do.

Apparently you want to parse a string representing a number of hours-minutes-seconds. Easy to do if your input string is in standard ISO 8601 format.

String input = "PT4H30M" ;           // Four-and-a-half hours.
Duration duration = Duration.parse( input ) ;

Then you apparently want to capture the current moment.

Instant instant = Instant.now() ;    // Capture the current moment as seen in UTC.

And lastly you want to add the span-of-time to that current moment.

Instant later = instant.plus( duration ) ;

Generate a string in standard ISO 8601 format for this new moment.

String output = later.toString() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154