# Depreciated Constructor - What is a better way to do this

**URL:** <https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244>\
**Category:** Scripts & Rules\
**Created:** [July 18, 2019, 6:19pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244 "2019-07-18T18:19:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 18, 2019, 6:19pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/1 "2019-07-18T18:19:19Z")

</div>

I use the following to run cron jobs for my lighting based on sunrise/sunset. In the example below, this would set the two variables to the hour and minute 15 minutes before sunset. This seems way over complicated to do something so easy. It is just “the way I figured it out originally” and I’ve left it alone since.

```
var Number sunsethourminus900 = Integer::parseInt(new DateTimeType((new DateTime((Astro_Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)).minusSeconds(900).toCalendar(null)).format("%1$tk"))
var Number sunsetminuteminus900 = Integer::parseInt(new DateTimeType((new DateTime((Astro_Sunset_Time.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)).minusSeconds(900).toCalendar(null)).format("%1$tM"))

```

I now get the following error with the rules when they process:

```
The constructor DateTimeType(Calendar) is deprecated

```

Can anyone suggest a better way to do this?

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [July 18, 2019, 6:25pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/2 "2019-07-18T18:25:13Z")

</div>

Most use the Astro binding and use the offset for the various positions of the sun; unless I’m missing something you are trying to accomplish.

---

<div class="post-metadata">

**Author:** ![Bruce\_Osborne](https://community.openhab.org/user_avatar/community.openhab.org/bruce_osborne/32/38531_2.png) [@Bruce\_Osborne](https://community.openhab.org/u/Bruce_Osborne)\
**Post date:** [July 18, 2019, 6:38pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/3 "2019-07-18T18:38:37Z")

</div>

In the PaperUI you can set an offset to `-15` Minutes to the Sunset#start to accomplish what you wish, I think.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 18, 2019, 7:46pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/4 "2019-07-18T19:46:08Z")

</div>

Sorry I wasn’t being more clear. I have over a dozen different ones for all variety of triggers. I don’t want to adjust the binding itself. I need to do the math after the fact. More or less, I have a cron that runs everyvminutr which says if hour = hour and minute = minute do xyz.

---

<div class="post-metadata">

**Author:** ![anonymous.one](https://community.openhab.org/user_avatar/community.openhab.org/anonymous.one/32/56272_2.png) [@anonymous.one](https://community.openhab.org/u/anonymous.one)\
**Post date:** [July 18, 2019, 8:10pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/5 "2019-07-18T20:10:28Z")

</div>

Ah, have you read @rlkoshak’s DP time of day series?

> [@Design Pattern: Simple State Machine (e.g. Time Of Day)](https://community.openhab.org/t/design-pattern-time-of-day/15407):
>
> Edit: OH 4 Updates Problem Statement Often in home automation there is a need to track certain states that the system is in. Events occur to transition from one state to another state (e.g. at noon we transition to the AFTERNOON state). The collection of all the possible states and the transitions between events is called a “state machine”. This design pattern illustrates some ways OH can implement very simple state machines. One of the most common uses of a state machine like this is to trac…

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 18, 2019, 8:29pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/6 "2019-07-18T20:29:43Z")

</div>

Wow, that is at least one unnecessary transform, probably two.

```
var Number sunsethourminus900 = new DateTime(Astro_Sunset_Time.state.toString).minusSeconds(900).millis

```

> [@morph166955](#):
>
> Sorry I wasn’t being more clear. I have over a dozen different ones for all variety of triggers. I don’t want to adjust the binding itself. I need to do the math after the fact. More or less, I have a cron that runs everyvminutr which says if hour = hour and minute = minute do xyz.

Why not? Just create a new Thing for each offset.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 18, 2019, 8:30pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/7 "2019-07-18T20:30:23Z")

</div>

Yes. I don’t disagree with it either, just not looking to do a rewrite right now. All I’m looking for is a better/newer way of doing those lines for now that doesn’t use depreciated code.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 2:07pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/8 "2019-07-19T14:07:57Z")

</div>

So the problem arises in getting the actual hour or minute value out of it once the math is done. I end up having to take the adjusted DateTime, fire it through toCalendar and put it into a new DateTimeType so that I can run the format() on it at the end. Putting the now Calendar type into DateTimeType is what is causing the barking. I know it seems very convoluted, it was the only way I could find (through trial and error) to get the results I wanted. Hence the post now of “how could I do this better”.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 19, 2019, 3:01pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/9 "2019-07-19T15:01:19Z")

</div>

> [@morph166955](#):
>
> Hence the post now of “how could I do this better”.

It looks a whole lot like the way to do it better is to create separate Astro Things with the offset.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 3:37pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/10 "2019-07-19T15:37:53Z")

</div>

I don’t want to do it that way. Thank you.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 19, 2019, 4:01pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/11 "2019-07-19T16:01:54Z")

</div>

Then you don’t want to do it in a better way and this is really a case of [The XY Problem](http://xyproblem.info/).

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 4:33pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/12 "2019-07-19T16:33:46Z")

</div>

No, it’s me wanting to fix one line of code rather than completely rewrite the thing.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 19, 2019, 4:59pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/13 "2019-07-19T16:59:36Z")

</div>

> - User wants to do X. // get a timestamp for sunset minus an offset
> - User doesn’t know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y. // deal with the calendar method being deprecated
> - User doesn’t know how to do Y either.
> - User asks for help with Y. // this post
> - Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve. // see above
> - After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn’t even a suitable solution for X.

Sure looks like an XY Problem to me.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 5:01pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/14 "2019-07-19T17:01:23Z")

</div>

If you insist on being confrontational to every thread I post, please just don’t reply if you see anything with my name on it. I asked a question, I said thank you for your answer but it wasn’t what I was looking for, you are persisting at this point only to cause confrontation. It’s not appreciated. Thank you.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [July 19, 2019, 5:13pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/15 "2019-07-19T17:13:36Z")

</div>

Sometimes answers are not about you. They are about future readers of this thread.

The _proper_ solution to this problem is to use the Astro binding as it was intended. And when you do that you can even get events from these offset time stamps.

I don’t care what you do. If you want to reject good advice that’s your business. I _do_ care what future readers of this thread read and learn and make sure they understand the better way to solve this problem.

I also don’t recall being confrontational to any other posting you’ve made. But I make so many posts:

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/4/7/47337ed25645056a9d26997e1e280b6600f15cd5.png)

maybe I’m forgetting. Though you are the only person who seems to have a problem with my posts. Hmmm, what’s the common denominator?

But as you wish. Consider yourself silenced. I’ll see no more posting from you.

---

<div class="post-metadata">

**Author:** ![wborn](https://community.openhab.org/user_avatar/community.openhab.org/wborn/32/55946_2.png) [@wborn](https://community.openhab.org/u/wborn)\
**Post date:** [July 19, 2019, 5:13pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/16 "2019-07-19T17:13:59Z")

</div>

There’s a `ZonedDateTime` constructor nowadays that should be used (see [deprecation](https://github.com/openhab/openhab-core/blob/b6aae6907f3d247e5d590bb25f6cb009e17584f8/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java#L68-L69)). But as Rich pointed out you don’t need that constructor at all.

This should also work:

```php
var Number sunsethourminus900 = (Astro_Sunset_Time.state as DateTimeType).zonedDateTime.minusSeconds(900).hour
var Number sunsetminuteminus900 = (Astro_Sunset_Time.state as DateTimeType).zonedDateTime.minusSeconds(900).minute

```

e.g. with the following item:

```php
DateTime Astro_Sunset_Time "Sunset [%1$tH:%1$tM]" <sunset> { channel = "astro:sun:home:set#start" }

```

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 5:28pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/17 "2019-07-19T17:28:04Z")

</div>

Thank you! I will make the change shortly.

---

<div class="post-metadata">

**Author:** ![morph166955](https://community.openhab.org/user_avatar/community.openhab.org/morph166955/32/101025_2.png) [@morph166955](https://community.openhab.org/u/morph166955)\
**Post date:** [July 19, 2019, 5:35pm UTC](https://community.openhab.org/t/depreciated-constructor-what-is-a-better-way-to-do-this/78244/18 "2019-07-19T17:35:12Z")

</div>

Are you really that passive/aggressive that you had to post your stats to some how show that you are superior? Do you really not realize that your reply in itself was confrontational?
