Example usage for java.time ZonedDateTime toInstant

List of usage examples for java.time ZonedDateTime toInstant

Introduction

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

Prototype

default Instant toInstant() 

Source Link

Document

Converts this date-time to an Instant .

Usage

From source file:Main.java

public static Timestamp toTimestamp(ZonedDateTime dateTime) {
    return new Timestamp(dateTime.toInstant().getEpochSecond() * 1000L);
}

From source file:Main.java

private static long getMillis(String strDate, DateTimeFormatter formatter, ZoneId zone) {
    LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter);
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone);
    Instant instant = zonedDateTime.toInstant();
    long milli = instant.toEpochMilli();
    return milli;
}

From source file:com.todo.backend.security.JWTUtils.java

public static String createToken(Long userId, UserRole userRole, String secretKey) {

    final ZonedDateTime validity = ZonedDateTime.now(ZoneId.of("UTC")).plusSeconds(VALIDITY);

    return Jwts.builder().setSubject(userId.toString()).claim(AUTHORITIES_KEY, userRole.name())
            .signWith(SignatureAlgorithm.HS512, secretKey).setExpiration(Date.from(validity.toInstant()))
            .compact();/*  ww  w  .j a  va 2 s  .  c o m*/
}

From source file:com.hotelbeds.hotelapimodel.auto.util.AssignUtils.java

public static Timestamp getTimestamp(final ZonedDateTime date) {
    return date != null ? Timestamp.from(date.toInstant()) : null;
}

From source file:io.manasobi.utils.DateUtils.java

public static long convertToTimeMillis(LocalDateTime dateTime) {
    ZonedDateTime zdt = dateTime.atZone(ZoneId.of("Asia/Seoul"));
    return zdt.toInstant().toEpochMilli();
}

From source file:com.example.geomesa.cassandra.CassandraQuickStart.java

static FeatureCollection createNewFeatures(SimpleFeatureType simpleFeatureType, int numNewFeatures) {
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();

    String id;// w  w  w. j a  v  a 2s  .  c  o m
    Object[] NO_VALUES = {};
    String[] PEOPLE_NAMES = { "Addams", "Bierce", "Clemens" };
    Long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L;
    Random random = new Random(5771);
    ZonedDateTime MIN_DATE = ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
    Double MIN_X = -78.0;
    Double MIN_Y = -39.0;
    Double DX = 2.0;
    Double DY = 2.0;

    for (int i = 0; i < numNewFeatures; i++) {
        // create the new (unique) identifier and empty feature shell
        id = "Observation." + Integer.toString(i);
        SimpleFeature simpleFeature = SimpleFeatureBuilder.build(simpleFeatureType, NO_VALUES, id);

        // be sure to tell GeoTools explicitly that you want to use the ID you provided
        simpleFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);

        // populate the new feature's attributes

        // string value
        simpleFeature.setAttribute("Who", PEOPLE_NAMES[i % PEOPLE_NAMES.length]);

        // long value
        simpleFeature.setAttribute("What", i);

        // location:  construct a random point within a 2-degree-per-side square
        double x = MIN_X + random.nextDouble() * DX;
        double y = MIN_Y + random.nextDouble() * DY;
        Geometry geometry = WKTUtils.read("POINT(" + x + " " + y + ")");

        // date-time:  construct a random instant within a year
        simpleFeature.setAttribute("Where", geometry);
        ZonedDateTime dateTime = MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR));
        simpleFeature.setAttribute("When", Date.from(dateTime.toInstant()));

        // another string value
        // "Why"; left empty, showing that not all attributes need values

        // accumulate this new feature in the collection
        featureCollection.add(simpleFeature);
    }

    return featureCollection;
}

From source file:alfio.datamapper.QueryType.java

private static SqlParameterSource extractParameters(Method m, Object[] args) {

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();
    if (parameterAnnotations == null || parameterAnnotations.length == 0) {
        return new EmptySqlParameterSource();
    }//w  w w.ja v  a2  s  .c  o m

    MapSqlParameterSource ps = new MapSqlParameterSource();
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < args.length; i++) {
        String name = parameterName(parameterAnnotations[i]);
        if (name != null) {
            if (args[i] != null && ZonedDateTime.class.isAssignableFrom(parameterTypes[i])) {
                ZonedDateTime dateTime = ZonedDateTime.class.cast(args[i]);
                final ZonedDateTime utc = dateTime.withZoneSameInstant(ZoneId.of("UTC"));
                Calendar c = Calendar.getInstance();
                c.setTimeZone(TimeZone.getTimeZone("UTC"));
                c.setTimeInMillis(utc.toInstant().toEpochMilli());
                ps.addValue(name, c, Types.TIMESTAMP);
            } else {
                ps.addValue(name, args[i], StatementCreatorUtils.javaTypeToSqlParameterType(parameterTypes[i]));
            }
        }
    }

    return ps;
}

From source file:com.epam.parso.impl.CSVDataWriterImpl.java

/**
 * The function to convert a Java 8 LocalDateTime into a string according to the format used.
 *
 * @param currentDate the LocalDateTime to convert.
 * @param format      the string with the format that must belong to the set of
 *                    {@link CSVDataWriterImpl#DATE_OUTPUT_FORMAT_STRINGS} mapping keys.
 * @return the string that corresponds to the date in the format used.
 *//*from  w ww.  j a  va  2 s  .c  om*/
protected static String convertLocalDateTimeElementToString(LocalDateTime currentDate, String format) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_OUTPUT_FORMAT_STRINGS.get(format));
    String valueToPrint = "";
    ZonedDateTime zonedDateTime = currentDate.atZone(ZoneOffset.UTC);
    if (zonedDateTime.toInstant().toEpochMilli() != 0 && zonedDateTime.getNano() != 0) {
        valueToPrint = formatter.format(currentDate);
    }
    return valueToPrint;
}

From source file:cz.muni.fi.pv168.project.hotelmanager.TestReservationManagerImpl.java

private static Clock prepareClockMock(ZonedDateTime now) {
    return Clock.fixed(now.toInstant(), now.getZone());
}

From source file:alfio.util.EventUtil.java

public static Optional<byte[]> getIcalForEvent(Event event, TicketCategory ticketCategory, String description) {
    ICalendar ical = new ICalendar();
    VEvent vEvent = new VEvent();
    vEvent.setSummary(event.getDisplayName());
    vEvent.setDescription(description);/*  w ww . j  a v  a2 s .c o m*/
    vEvent.setLocation(StringUtils.replacePattern(event.getLocation(), "[\n\r\t]+", " "));
    ZonedDateTime begin = Optional.ofNullable(ticketCategory)
            .map(tc -> tc.getTicketValidityStart(event.getZoneId())).orElse(event.getBegin());
    ZonedDateTime end = Optional.ofNullable(ticketCategory)
            .map(tc -> tc.getTicketValidityEnd(event.getZoneId())).orElse(event.getEnd());
    vEvent.setDateStart(Date.from(begin.toInstant()));
    vEvent.setDateEnd(Date.from(end.toInstant()));
    vEvent.setUrl(event.getWebsiteUrl());
    ical.addEvent(vEvent);
    StringWriter strWriter = new StringWriter();
    try (ICalWriter writer = new ICalWriter(strWriter, ICalVersion.V2_0)) {
        writer.write(ical);
        return Optional.of(strWriter.toString().getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        log.warn("was not able to generate iCal for event " + event.getShortName(), e);
        return Optional.empty();
    }
}