Example usage for java.util Date toInstant

List of usage examples for java.util Date toInstant

Introduction

In this page you can find the example usage for java.util Date toInstant.

Prototype

public Instant toInstant() 

Source Link

Document

Converts this Date object to an Instant .

Usage

From source file:io.stallion.utils.GeneralUtils.java

@Deprecated
public static String formatLocalDateFromJDate(Date date, String formatPattern) {
    if (date == null) {
        return "";
    }/*from  ww  w  . j  ava2s  . c o  m*/
    return formatLocalDateFromZonedDate(ZonedDateTime.ofInstant(date.toInstant(), UTC), formatPattern);
}

From source file:com.thinkbiganalytics.nifi.v2.ingest.GetTableData.java

private static LocalDateTime toDateTime(Date date) {
    return date == null ? LocalDateTime.MIN
            : LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC.normalized());
}

From source file:com.ikanow.aleph2.core.shared.utils.TimeSliceDirUtils.java

/** Given a pair of (optional) human readable strings (which are assumed to refer to the past)
 *  returns a pair of optional dates//from  w  ww  .ja va  2s .c o  m
 * @param input_config
 * @return
 */
public static Tuple2<Optional<Date>, Optional<Date>> getQueryTimeRange(
        final AnalyticThreadJobInputConfigBean input_config, final Date now) {
    Function<Optional<String>, Optional<Date>> parseDate = maybe_date -> maybe_date
            .map(datestr -> TimeUtils.getSchedule(datestr, Optional.of(now))).filter(res -> res.isSuccess())
            .map(res -> res.success())
            // OK so this wants to be backwards in time always...
            .map(date -> {
                if (date.getTime() > now.getTime()) {
                    final long diff = date.getTime() - now.getTime();
                    return Date.from(now.toInstant().minusMillis(diff));
                } else
                    return date;
            });

    final Optional<Date> tmin = parseDate.apply(Optional.ofNullable(input_config.time_min()));
    final Optional<Date> tmax = parseDate.apply(Optional.ofNullable(input_config.time_max()));

    return Tuples._2T(tmin, tmax);
}

From source file:org.sonar.core.issue.DefaultIssue.java

@CheckForNull
private static Date truncateToSeconds(@Nullable Date d) {
    if (d == null) {
        return null;
    }//from   ww  w. j  a v a2s  . c  om
    Instant instant = d.toInstant();
    instant = instant.truncatedTo(ChronoUnit.SECONDS);
    return Date.from(instant);
}

From source file:org.sakaiproject.sitestats.impl.event.detailed.refresolvers.PodcastReferenceResolver.java

/**
 * Utility method to retrieve the 'published' date of a given Podcast.
 * @param podcastResource The podcast resource
 * @param podServ The Podcast service object
 * @return A string representation of the podcast's publish date/time, matching the format from the Podcast tool
 *///w  w  w .  ja  v  a  2 s. com
private static Instant getPodcastPublishDateTime(ContentResource podcastResource, PodcastService podServ) {
    if (podcastResource == null) {
        return Instant.EPOCH;
    }

    Date releaseDateTime;
    if (podcastResource.getReleaseDate() == null) {
        try {
            ResourceProperties podcastProperties = podcastResource.getProperties();
            releaseDateTime = podServ
                    .getGMTdate(podcastProperties.getTimeProperty(PodcastService.DISPLAY_DATE).getTime());
        } catch (EntityPropertyNotDefinedException | EntityPropertyTypeException ex) {
            log.warn("Unable to retrieve release date for resource with ID = " + podcastResource.getId(), ex);
            return Instant.EPOCH;
        }
    } else {
        releaseDateTime = new Date(podcastResource.getReleaseDate().getTime());
    }

    return releaseDateTime.toInstant();
}

From source file:com.example.app.support.AppUtil.java

/**
 * Convert the given Date to a ZonedDateTime at the given TimeZone
 *
 * @param date the Date//from w ww. j av  a  2s  .c o  m
 * @param zone the TimeZone to convert to
 *
 * @return a ZonedDateTime that represents the same instant as the Date, at the TimeZone specified.
 */
@Nullable
public static ZonedDateTime toZonedDateTime(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    return ZonedDateTime.ofInstant(date.toInstant(), zone.toZoneId());
}

From source file:com.example.app.support.AppUtil.java

/**
 * Convert the given Date from UTC to a ZonedDateTime at the given TimeZone
 *
 * @param date the UTC date/*from   ww  w  . ja v a2 s  .  c  o m*/
 * @param zone the TimeZone to convert the time to
 *
 * @return a ZonedDateTime that represents the same instant as the UTC date, but at the given TimeZone.
 */
@Nullable
public static ZonedDateTime convertFromPersisted(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    ZonedDateTime from = ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
    return from.withZoneSameInstant(zone.toZoneId());
}

From source file:org.silverpeas.core.util.DateUtil.java

public static LocalDate toLocalDate(Date date) {
    if (date == null) {
        return null;
    }// ww w  . j  a va2 s  .  c  o  m
    return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

From source file:com.codepalousa.restlet.raml.DateTimeSerializeConverter.java

@Override
public String convert(Date value) {
    return LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()).format(formatter);
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

/**
 * Read a date time value from the Cell.
 * //from   ww  w  .  j  ava  2 s  . c  o  m
 * @param object
 *            the object
 * @param field
 *            the {@link Field} to set
 * @param cell
 *            the {@link Cell} to read
 * @param xlsAnnotation
 *            the {@link XlsElement} element
 * @throws ConverterException
 *             the conversion exception type
 */
protected static void localDateTimeReader(final Object object, final Field field, final Cell cell,
        final XlsElement xlsAnnotation) throws ConverterException {
    if (StringUtils.isNotBlank(readCell(cell))) {
        try {
            if (StringUtils.isBlank(xlsAnnotation.transformMask())) {
                field.set(object,
                        cell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            } else {
                String date = cell.getStringCellValue();

                String tM = xlsAnnotation.transformMask();
                String fM = xlsAnnotation.formatMask();
                String decorator = StringUtils.isEmpty(tM)
                        ? (StringUtils.isEmpty(fM) ? CellStyleHandler.MASK_DECORATOR_DATE : fM)
                        : tM;

                SimpleDateFormat dt = new SimpleDateFormat(decorator);
                Date dateConverted = dt.parse(date);
                field.set(object, dateConverted.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            }
        } catch (ParseException | IllegalArgumentException | IllegalAccessException e) {
            /*
             * if date decorator do not match with a valid mask launch
             * exception
             */
            throw new ConverterException(ExceptionMessage.CONVERTER_LOCALDATETIME.getMessage(), e);
        }
    }
}