# Safe way to compute the duration to sleep

**URL:** <https://community.temporal.io/t/safe-way-to-compute-the-duration-to-sleep/11032>\
**Category:** Community Support\
**Tags:** java-sdk, general-impl\
**Created:** [February 16, 2024, 2:11am UTC](https://community.temporal.io/t/safe-way-to-compute-the-duration-to-sleep/11032 "2024-02-16T02:11:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duwayne\_Sy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/duwayne_sy/32/4958_2.png) [@Duwayne\_Sy](https://community.temporal.io/u/Duwayne_Sy)\
**Post date:** [February 16, 2024, 2:11am UTC](https://community.temporal.io/t/safe-way-to-compute-the-duration-to-sleep/11032/1 "2024-02-16T02:11:16Z")

</div>

I have a situation where I want to compute the duration to put the workflow to sleep before running any activities. Is it safe for my workflow method to do something like:

Workflow.sleep(Duration.ofHours(Duration.between(from, to).toHours()))

Here from = ZonedDateTime.now(), and to = ZonedDateTime retrieved from DB and passed into the workflow method.

My intuition is it isn’t deterministic and therefore unsafe. If so, how do I make this safe?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [February 16, 2024, 3:40pm UTC](https://community.temporal.io/t/safe-way-to-compute-the-duration-to-sleep/11032/2 "2024-02-16T15:40:47Z")

</div>

> Here from = ZonedDateTime.now(), and to = ZonedDateTime retrieved from DB and passed into the workflow method.

What do you mean by passed to the workflow method? If they are passed as workflow arguments or returned as an activity result then they are recorded and the code will be deterministic.

In the most cases `Workflow.currentTimeMillis()` is the best way to get the current time.

You can also use `Workflow.getInfo().getRunStartedTimestampMillis()` to get the time workflow was started.

---

<div class="post-metadata">

**Author:** ![Duwayne\_Sy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/duwayne_sy/32/4958_2.png) [@Duwayne\_Sy](https://community.temporal.io/u/Duwayne_Sy)\
**Post date:** [February 20, 2024, 4:11pm UTC](https://community.temporal.io/t/safe-way-to-compute-the-duration-to-sleep/11032/3 "2024-02-20T16:11:48Z")

</div>

Thanks Maxim. I will call Workflow.currentTimeMillis() in the workflow method to get “now”. That solves the problem.
