Example usage for java.time OffsetDateTime ofInstant

List of usage examples for java.time OffsetDateTime ofInstant

Introduction

In this page you can find the example usage for java.time OffsetDateTime ofInstant.

Prototype

public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) 

Source Link

Document

Obtains an instance of OffsetDateTime from an Instant and zone ID.

Usage

From source file:org.silverpeas.core.date.DateTime.java

/**
 * Gets the time of this datetime by taking into account the offset from UTC/Greenwich in the
 * ISO-8601 calendar system.//from   ww w . j a va2 s  .co  m
 * @return the time of this datetime.
 */
public OffsetTime getOffsetTime() {
    return OffsetDateTime.ofInstant(toInstant(), timeZone.toZoneId()).toOffsetTime();
}

From source file:com.amazonaws.services.kinesis.io.ObjectExtractor.java

/**
 * {@inheritDoc}//from  w ww. ja  va 2s  . c  o m
 */
@Override
public List<AggregateData> getData(InputEvent event) throws SerializationException {
    if (!validated) {
        try {
            validate();
        } catch (Exception e) {
            throw new SerializationException(e);
        }
    }

    try {
        List<AggregateData> data = new ArrayList<>();

        Object o = serialiser.toClass(event);

        // get the value of the reflected labels
        LabelSet labels = new LabelSet();
        for (String key : this.aggregateLabelMethods) {
            labels.put(key, aggregateLabelMethodMap.get(key).invoke(o).toString());
        }

        // get the unique ID value from the object
        String uniqueId = null;
        if (this.uniqueIdMethodName != null) {
            switch (this.uniqueIdMethodName) {
            case StreamAggregator.REF_PARTITION_KEY:
                uniqueId = event.getPartitionKey();
                break;
            case StreamAggregator.REF_SEQUENCE:
                uniqueId = event.getSequenceNumber();
                break;
            default:
                Object id = uniqueIdMethod.invoke(o);
                if (id != null) {
                    uniqueId = id.toString();
                }
                break;
            }
        }

        // get the date value from the object
        if (this.dateMethod != null) {
            eventDate = dateMethod.invoke(o);

            if (eventDate == null) {
                dateValue = OffsetDateTime.now(ZoneId.of("UTC"));
            } else {
                if (eventDate instanceof Date) {
                    dateValue = OffsetDateTime.ofInstant(((Date) eventDate).toInstant(), ZoneId.of("UTC"));
                } else if (eventDate instanceof Long) {
                    dateValue = OffsetDateTime.ofInstant(Instant.ofEpochMilli((Long) eventDate),
                            ZoneId.of("UTC"));
                } else {
                    throw new Exception(String.format("Cannot use data type %s for date value on event",
                            eventDate.getClass().getSimpleName()));
                }
            }
        }

        // extract all summed values from the serialised object
        if (this.aggregatorType.equals(AggregatorType.SUM)) {
            // lift out the aggregated method value
            for (String s : this.sumValueMap.keySet()) {
                summaryValue = this.sumValueMap.get(s).invoke(o);

                if (summaryValue != null) {
                    if (summaryValue instanceof Double) {
                        sumUpdates.put(s, (Double) summaryValue);
                    } else if (summaryValue instanceof Long) {
                        sumUpdates.put(s, ((Long) summaryValue).doubleValue());
                    } else if (summaryValue instanceof Integer) {
                        sumUpdates.put(s, ((Integer) summaryValue).doubleValue());
                    } else {
                        String msg = String.format("Unable to access  Summary %s due to NumberFormatException",
                                s);
                        LOG.error(msg);
                        throw new SerializationException(msg);
                    }
                }
            }
        }

        data.add(new AggregateData(uniqueId, labels, dateValue, sumUpdates));

        return data;
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}

From source file:net.dv8tion.jda.core.EmbedBuilder.java

/**
 * Sets the Timestamp of the embed./*  w w w.  jav a 2 s .  c om*/
 *
 * <p><b><a href="http://i.imgur.com/YP4NiER.png">Example</a></b>
 *
 * <p><b>Hint:</b> You can get the current time using {@link java.time.Instant#now() Instant.now()} or convert time from a
 * millisecond representation by using {@link java.time.Instant#ofEpochMilli(long) Instant.ofEpochMilli(long)};
 *
 * @param  temporal
 *         the temporal accessor of the timestamp
 *
 * @return the builder after the timestamp has been set
 */
public EmbedBuilder setTimestamp(TemporalAccessor temporal) {
    if (temporal == null) {
        this.timestamp = null;
    } else if (temporal instanceof OffsetDateTime) {
        this.timestamp = (OffsetDateTime) temporal;
    } else {
        ZoneOffset offset;
        try {
            offset = ZoneOffset.from(temporal);
        } catch (DateTimeException ignore) {
            offset = ZoneOffset.UTC;
        }
        try {
            LocalDateTime ldt = LocalDateTime.from(temporal);
            this.timestamp = OffsetDateTime.of(ldt, offset);
        } catch (DateTimeException ignore) {
            try {
                Instant instant = Instant.from(temporal);
                this.timestamp = OffsetDateTime.ofInstant(instant, offset);
            } catch (DateTimeException ex) {
                throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + temporal
                        + " of type " + temporal.getClass().getName(), ex);
            }
        }
    }
    return this;
}

From source file:net.dv8tion.jda.core.entities.EntityBuilder.java

public void createPresence(Object memberOrFriend, JSONObject presenceJson) {
    if (memberOrFriend == null)
        throw new NullPointerException("Provided memberOrFriend was null!");

    JSONObject gameJson = presenceJson.isNull("game") ? null : presenceJson.getJSONObject("game");
    OnlineStatus onlineStatus = OnlineStatus.fromKey(presenceJson.getString("status"));
    Game game = null;//  w  ww  . jav  a  2s  .  c om

    if (gameJson != null && !gameJson.isNull("name")) {
        String gameName = gameJson.get("name").toString();
        String url = gameJson.isNull("url") ? null : gameJson.get("url").toString();

        Game.GameType gameType;
        try {
            gameType = gameJson.isNull("type") ? Game.GameType.DEFAULT
                    : Game.GameType.fromKey(Integer.parseInt(gameJson.get("type").toString()));
        } catch (NumberFormatException e) {
            gameType = Game.GameType.DEFAULT;
        }

        game = new GameImpl(gameName, url, gameType);
    }
    if (memberOrFriend instanceof Member) {
        MemberImpl member = (MemberImpl) memberOrFriend;
        member.setOnlineStatus(onlineStatus);
        member.setGame(game);
    } else if (memberOrFriend instanceof Friend) {
        FriendImpl friend = (FriendImpl) memberOrFriend;
        friend.setOnlineStatus(onlineStatus);
        friend.setGame(game);

        OffsetDateTime lastModified = OffsetDateTime.ofInstant(
                Instant.ofEpochMilli(presenceJson.getLong("last_modified")),
                TimeZone.getTimeZone("GMT").toZoneId());

        friend.setOnlineStatusModifiedTime(lastModified);
    } else
        throw new IllegalArgumentException(
                "An object was provided to EntityBuilder#createPresence that wasn't a Member or Friend. JSON: "
                        + presenceJson);
}