Example usage for java.time DateTimeException DateTimeException

List of usage examples for java.time DateTimeException DateTimeException

Introduction

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

Prototype

public DateTimeException(String message, Throwable cause) 

Source Link

Document

Constructs a new date-time exception with the specified message and cause.

Usage

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

/**
 * Sets the Timestamp of the embed./*from  w  w  w . ja va  2  s.  co m*/
 *
 * <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;
}