Skip to content

Commit

Permalink
Fixed usage of deprecated constructor for 'DateTimeType(Calendar)' (#…
Browse files Browse the repository at this point in the history
…7918)

Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored Jun 20, 2020
1 parent abc926d commit 3023a8c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.net.UnknownHostException;
import java.nio.BufferOverflowException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -41,7 +41,6 @@
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.OnOffType;
Expand Down Expand Up @@ -86,7 +85,7 @@ public class BigAssFanHandler extends BaseThingHandler {

protected Map<String, State> fanStateMap = Collections.synchronizedMap(new HashMap<>());

public BigAssFanHandler(@NonNull Thing thing, String ipv4Address) {
public BigAssFanHandler(Thing thing, String ipv4Address) {
super(thing);
this.thing = thing;

Expand Down Expand Up @@ -990,10 +989,8 @@ private void updateTime(String[] messageParts) {
logger.debug("Process time update for {}: {}", thing.getUID(), messageParts[3]);
// (mac|name;TIME;VALUE;2017-03-26T14:06:27Z)
try {
Calendar cal = Calendar.getInstance();
Instant instant = Instant.parse(messageParts[3]);
cal.setTime(Date.from(instant));
DateTimeType state = new DateTimeType(cal);
DateTimeType state = new DateTimeType(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
updateChannel(CHANNEL_TIME, state);
fanStateMap.put(CHANNEL_TIME, state);
} catch (DateTimeParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.openhab.binding.gardena.internal.GardenaBindingConstants.*;
import static org.openhab.binding.gardena.internal.GardenaSmartCommandName.*;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -185,7 +187,7 @@ private State convertToState(Device device, ChannelUID channelUID) throws Garden
case "DateTime":
Calendar cal = DateUtils.parseToCalendar(value);
if (cal != null && !cal.before(VALID_DATE_START)) {
return new DateTimeType(cal);
return new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault()));
} else {
return UnDefType.NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

import static org.openhab.binding.logreader.internal.LogReaderBindingConstants.*;

import java.util.Calendar;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.regex.PatternSyntaxException;

import org.eclipse.smarthome.core.library.types.DateTimeType;
Expand Down Expand Up @@ -162,7 +163,7 @@ public void fileNotFound() {
@Override
public void fileRotated() {
logger.debug("Log rotated");
updateChannelIfLinked(CHANNEL_LOGROTATED, new DateTimeType(Calendar.getInstance()));
updateChannelIfLinked(CHANNEL_LOGROTATED, new DateTimeType(ZonedDateTime.now()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package org.openhab.binding.lutron.internal.grxprg;

import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -884,7 +886,8 @@ private void handleReportTime(Matcher m, String resp) {
final int yr = Integer.parseInt(m.group(5));
c.set(Calendar.YEAR, yr + (yr < 50 ? 1900 : 2000));

_callback.stateChanged(PrgConstants.CHANNEL_TIMECLOCK, new DateTimeType(c));
_callback.stateChanged(PrgConstants.CHANNEL_TIMECLOCK,
new DateTimeType(ZonedDateTime.ofInstant(c.toInstant(), ZoneId.systemDefault())));
} catch (NumberFormatException e) {
logger.error("Invalid time response (can't parse number): '{}'", resp);
}
Expand Down Expand Up @@ -934,12 +937,14 @@ private void handleSunriseSunset(Matcher m, String resp) {
final Calendar sunrise = Calendar.getInstance();
sunrise.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(1)));
sunrise.set(Calendar.MINUTE, Integer.parseInt(m.group(2)));
_callback.stateChanged(PrgConstants.CHANNEL_SUNRISE, new DateTimeType(sunrise));
_callback.stateChanged(PrgConstants.CHANNEL_SUNRISE,
new DateTimeType(ZonedDateTime.ofInstant(sunrise.toInstant(), ZoneId.systemDefault())));

final Calendar sunset = Calendar.getInstance();
sunset.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(3)));
sunset.set(Calendar.MINUTE, Integer.parseInt(m.group(4)));
_callback.stateChanged(PrgConstants.CHANNEL_SUNSET, new DateTimeType(sunset));
_callback.stateChanged(PrgConstants.CHANNEL_SUNSET,
new DateTimeType(ZonedDateTime.ofInstant(sunset.toInstant(), ZoneId.systemDefault())));
} catch (NumberFormatException e) {
logger.error("Invalid sunrise/sunset response (can't parse number): '{}'", resp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -180,13 +182,15 @@ public void handleUpdate(LutronCommandType type, String... parameters) {
} else if (parameters.length >= 2 && ACTION_SUNRISE.toString().equals(parameters[0])) {
Calendar calendar = parseLutronTime(parameters[1]);
if (calendar != null) {
updateState(CHANNEL_SUNRISE, new DateTimeType(calendar));
updateState(CHANNEL_SUNRISE,
new DateTimeType(ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault())));
}

} else if (parameters.length >= 2 && ACTION_SUNSET.toString().equals(parameters[0])) {
Calendar calendar = parseLutronTime(parameters[1]);
if (calendar != null) {
updateState(CHANNEL_SUNSET, new DateTimeType(calendar));
updateState(CHANNEL_SUNSET,
new DateTimeType(ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault())));
}

} else if (parameters.length >= 2 && ACTION_EXECEVENT.toString().equals(parameters[0])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -230,7 +232,8 @@ private void updateChannel(String channelId) {
// Build a State from this value
State state = null;
if (datapoint instanceof Calendar) {
state = new DateTimeType((Calendar) datapoint);
state = new DateTimeType(
ZonedDateTime.ofInstant(((Calendar) datapoint).toInstant(), ZoneId.systemDefault()));
} else if (datapoint instanceof Integer) {
state = getStateForType(channel.getAcceptedItemType(), (Integer) datapoint);
} else if (datapoint instanceof Number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import static org.openhab.binding.onebusaway.internal.OneBusAwayBindingConstants.*;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -165,7 +168,8 @@ private void updatePropertiesFromArrivalAndDeparture(ArrivalAndDeparture data) {
private void publishChannel(ChannelUID channelUID, Calendar now, long lastUpdateTime,
List<ArrivalAndDeparture> arrivalAndDepartures) {
if (channelUID.getId().equals(CHANNEL_ID_UPDATE)) {
updateState(channelUID, new DateTimeType((new Calendar.Builder()).setInstant(lastUpdateTime).build()));
updateState(channelUID, new DateTimeType(
ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastUpdateTime), ZoneId.systemDefault())));
return;
}

Expand Down Expand Up @@ -197,7 +201,8 @@ private void publishChannel(ChannelUID channelUID, Calendar now, long lastUpdate
logger.debug("Not notifying {} because it is in the past.", channelUID.getId());
continue;
}
updateState(channelUID, new DateTimeType(time));
updateState(channelUID,
new DateTimeType(ZonedDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault())));

// Update properties only when we update arrival information. This is not perfect.
if (channelUID.getId().equals(CHANNEL_ID_ARRIVAL)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.openhab.binding.tellstick.internal.TellstickBindingConstants.*;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;

import org.eclipse.smarthome.config.core.Configuration;
Expand Down Expand Up @@ -266,7 +268,8 @@ public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent ev
}
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(event.getTimestamp());
updateState(timestampChannel, new DateTimeType(cal));
updateState(timestampChannel,
new DateTimeType(ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.eclipse.smarthome.core.thing.ThingStatusDetail.CONFIGURATION_ERROR;
import static org.openhab.binding.unifi.internal.UniFiBindingConstants.*;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -194,7 +196,8 @@ protected void refreshChannel(UniFiClient client, ChannelUID channelUID) {
case CHANNEL_LAST_SEEN:
// mgb: we don't check clientOnline as lastSeen is also included in the Insights data
if (client.getLastSeen() != null) {
state = new DateTimeType(client.getLastSeen());
state = new DateTimeType(
ZonedDateTime.ofInstant(client.getLastSeen().toInstant(), ZoneId.systemDefault()));
}
break;

Expand Down

0 comments on commit 3023a8c

Please sign in to comment.