Example usage for java.time Instant ofEpochMilli

List of usage examples for java.time Instant ofEpochMilli

Introduction

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

Prototype

public static Instant ofEpochMilli(long epochMilli) 

Source Link

Document

Obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.

Usage

From source file:edu.usu.sdl.openstorefront.common.util.TimeUtil.java

/**
 * Get the end of the day passed in//from  ww w .  j  av a 2  s  .  c o  m
 *
 * @param date
 * @return end of the day or null if date was null
 */
public static Date endOfDay(Date date) {
    if (date != null) {
        Instant instant = Instant.ofEpochMilli(date.getTime());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        localDateTime = localDateTime.withHour(23).withMinute(59).withSecond(59)
                .with(ChronoField.MILLI_OF_SECOND, 999);
        return new Date(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli());
    }
    return date;

}

From source file:org.sakaiproject.contentreview.service.BaseContentReviewService.java

@Override
public Instant getUserEULATimestamp(String userId) {
    Instant timestamp = null;/*  w w w . j  a v  a  2 s . c o  m*/
    try {
        ResourceProperties pref = preferencesService.getPreferences(userId)
                .getProperties(PROP_KEY_EULA + getProviderId());
        if (pref != null) {
            timestamp = Instant.ofEpochMilli(pref.getLongProperty(PROP_KEY_EULA_TIMESTAMP));
        }
    } catch (EntityPropertyNotDefinedException e) {
        //nothing to do, prop is just not set
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return timestamp;
}

From source file:com.inversoft.json.ZonedDateTimeDeserializer.java

@Override
public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();/*from ww  w .j a  va 2  s .co m*/
    long value;
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
        value = jp.getLongValue();
    } else if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) {
            return null;
        }

        try {
            value = Long.parseLong(str);
        } catch (NumberFormatException e) {
            throw ctxt.mappingException(handledType());
        }
    } else {
        throw ctxt.mappingException(handledType());
    }

    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC);
}

From source file:io.kamax.mxisd.threepid.session.ThreePidSession.java

public ThreePidSession(IThreePidSessionDao dao) {
    this(dao.getId(), dao.getServer(), new ThreePid(dao.getMedium(), dao.getAddress()), dao.getSecret(),
            dao.getAttempt(), dao.getNextLink(), dao.getToken());
    timestamp = Instant.ofEpochMilli(dao.getCreationTime());
    isValidated = dao.getValidated();//  w  w  w . j a va 2 s  .c  om
    if (isValidated) {
        validationTimestamp = Instant.ofEpochMilli(dao.getValidationTime());
    }

    isRemote = dao.isRemote();
    remoteServer = dao.getRemoteServer();
    remoteId = dao.getRemoteId();
    remoteSecret = dao.getRemoteSecret();
    remoteAttempt = dao.getRemoteAttempt();
    isRemoteValidated = dao.isRemoteValidated();
}

From source file:org.primeframework.mvc.parameter.convert.converters.ZonedDateTimeConverter.java

protected Object stringToObject(String value, Type convertTo, Map<String, String> attributes, String expression)
        throws ConversionException, ConverterStateException {
    if (emptyIsNull && StringUtils.isBlank(value)) {
        return null;
    }//from   www .jav  a  2s  . co  m

    String format = attributes.get("dateTimeFormat");
    if (format == null) {
        // Try checking if this is an instant
        try {
            long instant = Long.parseLong(value);
            return ZonedDateTime.ofInstant(Instant.ofEpochMilli(instant), ZoneOffset.UTC);
        } catch (NumberFormatException e) {
            throw new ConverterStateException("You must provide the dateTimeFormat dynamic attribute for "
                    + "the form fields [" + expression + "] that maps to DateTime properties in the action. "
                    + "If you are using a text field it will look like this: [@jc.text _dateTimeFormat=\"MM/dd/yyyy hh:mm:ss aa Z\"]\n\n"
                    + "NOTE: The format must include the time and a timezone. Otherwise, it will be unparseable");
        }
    }

    return toDateTime(value, format);
}

From source file:org.lizardirc.beancounter.commands.earthquake.GeoJsonFeatureProperty.java

public ZonedDateTime getEventTime() {
    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(eventTime), ZoneId.systemDefault());
}

From source file:Main.java

public static String getStringFromMillis(final Number millis) {
    return FORMATTER
            .format(LocalDateTime.ofInstant(Instant.ofEpochMilli(millis.longValue()), ZoneId.systemDefault()));
}

From source file:org.lizardirc.beancounter.commands.earthquake.GeoJsonFeatureProperty.java

public ZonedDateTime getUpdatedTime() {
    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(updated), ZoneId.systemDefault());
}

From source file:com.teradata.benchto.driver.service.BenchmarkServiceClient.java

@Retryable(value = RestClientException.class, backoff = @Backoff(1000))
public Instant getServiceCurrentTime() {
    Map<String, String> requestParams = ImmutableMap.of("serviceUrl", serviceUrl);
    Long serviceCurrentTime = postForObject("{serviceUrl}/v1/time/current-time-millis", null, Long.class,
            requestParams);/*from  w w w.  jav  a 2 s  .  c  o  m*/
    return Instant.ofEpochMilli(requireNonNull(serviceCurrentTime, "service returned null time"));
}

From source file:be.wegenenverkeer.common.resteasy.json.Iso8601AndOthersLocalDateTimeFormat.java

/**
 * Parse string to date.//from  w  ww.j a va 2 s  .c  o  m
 *
 * @param str string to parse
 * @return date
 */
public LocalDateTime parse(String str) {
    LocalDateTime date = null;

    if (StringUtils.isNotBlank(str)) {
        // try full ISO 8601 format first
        try {
            Date timestamp = ISO8601Utils.parse(str, new ParsePosition(0));
            Instant instant = Instant.ofEpochMilli(timestamp.getTime());
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        } catch (IllegalArgumentException | ParseException ex) {
            // ignore, try next format
            date = null; // dummy
        }
        // then try ISO 8601 format without timezone
        try {
            return LocalDateTime.from(iso8601NozoneFormat.parse(str));
        } catch (IllegalArgumentException | DateTimeParseException ex) {
            // ignore, try next format
            date = null; // dummy
        }

        // then try a list of formats
        for (DateTimeFormatter formatter : FORMATS) {
            try {
                TemporalAccessor ta = formatter.parse(str);
                try {
                    return LocalDateTime.from(ta);
                } catch (DateTimeException dte) {
                    return LocalDate.from(ta).atStartOfDay();
                }
            } catch (IllegalArgumentException | DateTimeParseException e) {
                // ignore, try next format
                date = null; // dummy
            }
        }
        throw new IllegalArgumentException("Could not parse date " + str
                + " using ISO 8601 or any of the formats " + Arrays.asList(FORMATS) + ".");

    }
    return date; // empty string
}