Java Code Examples for java.sql.Timestamp.from()

The following are Jave code examples for showing how to use from() of the java.sql.Timestamp class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: SamaGamesAPI   File: IncrementationAchievement.java   View Source Code Vote up 6 votes
/**
 * Increase the progress of a given player
 *
 * @param player Player
 * @param amount Amount
 */
public void increment(UUID player, int amount)
{
    AchievementProgress progress = this.progress.get(player);

    if (progress == null)
    {
        progress = new AchievementProgress(-1, 0, Timestamp.from(Instant.now()), null, true);
        this.progress.put(player, progress);
    }

    if (progress.getProgress() + amount > this.objective && progress.getUnlockTime() == null)
    {
        progress.unlock();
        progress.setProgress(this.objective);
        this.sendRewardMessage(player);
    }
    else if (progress.getUnlockTime() == null)
    {
        progress.setProgress(progress.getProgress() + amount);
    }
}
 
Example 2
Project: SamaGamesAPI   File: Achievement.java   View Source Code Vote up 6 votes
/**
 * Unlock this achievement for a given player
 *
 * @param player Player
 */
public void unlock(UUID player)
{
    if (this instanceof IncrementationAchievement)
        throw new IllegalStateException("Try to unlock incrementation achievement");

    AchievementProgress progress = this.progress.get(player);

    if (progress != null && progress.getUnlockTime() != null)
        return;

    if (progress == null)
    {
        progress = new AchievementProgress(-1, 0, Timestamp.from(Instant.now()), null, true);
        this.progress.put(player, progress);
    }

    progress.unlock();
    progress.setProgress(1);

    this.sendRewardMessage(player);
}
 
Example 3
Project: minijax   File: InstantConverter.java   View Source Code Vote up 5 votes
@Override
public Timestamp convertToDatabaseColumn(final Instant instant) {
    if (instant == null) {
        return null;
    }
    return Timestamp.from(instant);
}
 
Example 4
Project: OperatieBRP   File: VrijBerichtDeserializer.java   View Source Code Vote up 5 votes
@Override
public VrijBericht deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
    final ObjectCodec oc = jp.getCodec();
    final JsonNode node = oc.readTree(jp);

    final Integer id = JsonUtils.getAsInteger(node, "id");
    VrijBericht vrijBericht;
    if (id != null) {
        // Bij een bestaand vrij bericht alleen indicatieGelezen aanpassen.
        vrijBericht = entityManager.find(VrijBericht.class, id);
        final String ongelezen = JsonUtils.getAsString(node, "ongelezen");
        boolean gelezen;
        switch (ongelezen) {
            case "Nee":
                gelezen = true;
                break;
            case "Ja":
            default:
                gelezen = false;
        }
        vrijBericht.setIndicatieGelezen(gelezen);
    } else {
        final Short soortvrijberId = JsonUtils.getAsShort(node, "soortvrijber");
        final String data = JsonUtils.getAsString(node, "data");
        final SoortVrijBericht soortVrijBericht = entityManager.getReference(SoortVrijBericht.class, soortvrijberId);
        vrijBericht =
                new VrijBericht(SoortBerichtVrijBericht.STUUR_VRIJ_BERICHT, soortVrijBericht,
                        Timestamp.from(DatumUtil.nuAlsZonedDateTimeInNederland().toInstant()), data, null);
        final ArrayNode partijen = (ArrayNode) node.get("partijen");
        for (JsonNode partijNode : partijen) {
            final Short partijId = Short.valueOf(partijNode.asText());
            final VrijBerichtPartij vrijBerichtPartij = new VrijBerichtPartij(vrijBericht, partijRepository.findOne(partijId));
            vrijBericht.getVrijBerichtPartijen().add(vrijBerichtPartij);
        }
    }
    return vrijBericht;
}
 
Example 5
Project: jdk8u-jdk   File: TimestampTests.java   View Source Code Vote up 5 votes
@Test
public void test45() throws Exception {
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    Instant instant = ts1.toInstant();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
}
 
Example 6
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 5 votes
@Test
public void test46() throws Exception {
    Instant instant = Instant.now();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(instant.equals(ts2.toInstant()),
            "Error Instant values do not match");
}
 
Example 7
Project: mid-tier   File: CustomerArchivist.java   View Source Code Vote up 5 votes
public List<Event> findEvents(Optional<Interval> withIn) {
    StringBuilder qs = new StringBuilder("select e from Event e");
    if (withIn.isPresent()) {
        qs.append(" and t.lastModifiedTime>:startsAt and t.lastModifiedTime<:endsAt");
    }
    TypedQuery<Event> q = em.createQuery(qs.toString(), Event.class);
    if (withIn.isPresent()) {
        Timestamp ts = Timestamp.from(withIn.get().getStart().toInstant());
        q.setParameter("startsAt", ts);
        Timestamp te = Timestamp.from(withIn.get().getEnd().toInstant());
        q.setParameter("endsAt", te);
    }
    return q.getResultList();
}
 
Example 8
Project: mid-tier   File: AccountArchivist.java   View Source Code Vote up 5 votes
/**
 * getting a set of transactions filtered according to the APi capabilities
 *
 * @param elementSet the number of element that is wished returned
 * @param withIn     the interval which the returned items needs to stay within
 * @param sortAs     attribute to sort by, default is time and supports sorting by amount
 */
public List<Transaction> getTransactions(String regNo, String accountNo, Optional<Element> elementSet, Optional<Interval> withIn,
        List<Sort> sortAs) {
    StringBuilder qs = new StringBuilder("select t from Transaction t where t.account.regNo=:regNo and t.account.accountNo=:accountNo");
    if (withIn.isPresent()) {
        qs.append(" and t.lastModifiedTime>:startsAt and t.lastModifiedTime<:endsAt");
    }
    sortAs.stream()
            .filter(sort -> "amount".equals(sort.getAttribute()))
            .findAny()
            .ifPresent(sort -> qs.append(" order by t.").append(sort.getAttribute()).append(" ").append(sort.getDirection()));
    TypedQuery<Transaction> q = em.createQuery(qs.toString(), Transaction.class);
    q.setParameter("regNo", regNo);
    q.setParameter("accountNo", accountNo);
    if (withIn.isPresent()) {
        Timestamp ts = Timestamp.from(withIn.get().getStart().toInstant());
        q.setParameter("startsAt", ts);
        Timestamp te = Timestamp.from(withIn.get().getEnd().toInstant());
        q.setParameter("endsAt", te);
    }
    List<Transaction> txs = q.setMaxResults(TX_MAXSIZE).getResultList();
    if ((TX_DEFAULTSIZE < txs.size() && (elementSet.isPresent()))) {
        return reduceElements(elementSet.get(), txs);
    }
    return txs;

}
 
Example 9
Project: mid-tier   File: AccountArchivist.java   View Source Code Vote up 5 votes
public List<Event> findEvents(Optional<Interval> withIn) {
    StringBuilder qs = new StringBuilder("select e from Event e");
    if (withIn.isPresent()) {
        qs.append(" and t.lastModifiedTime>:startsAt and t.lastModifiedTime<:endsAt");
    }
    TypedQuery<Event> q = em.createQuery(qs.toString(), Event.class);
    if (withIn.isPresent()) {
        Timestamp ts = Timestamp.from(withIn.get().getStart().toInstant());
        q.setParameter("startsAt", ts);
        Timestamp te = Timestamp.from(withIn.get().getEnd().toInstant());
        q.setParameter("endsAt", te);
    }
    return q.getResultList();
}
 
Example 10
Project: mid-tier   File: LocationArchivist.java   View Source Code Vote up 5 votes
public List<Event> findEvents(Optional<Interval> withIn) {
    StringBuilder qs = new StringBuilder("select e from Event e");
    if (withIn.isPresent()) {
        qs.append(" and t.lastModifiedTime>:startsAt and t.lastModifiedTime<:endsAt");
    }
    TypedQuery<Event> q = em.createQuery(qs.toString(), Event.class);
    if (withIn.isPresent()) {
        Timestamp ts = Timestamp.from(withIn.get().getStart().toInstant());
        q.setParameter("startsAt", ts);
        Timestamp te = Timestamp.from(withIn.get().getEnd().toInstant());
        q.setParameter("endsAt", te);
    }
    return q.getResultList();
}
 
Example 11
Project: SamaGamesAPI   File: AchievementProgress.java   View Source Code Vote up 4 votes
/**
 * Internal
 * Unlock achievement
 */
void unlock()
{
    this.unlockTime = Timestamp.from(Instant.now());
    this.changed = true;
}
 
Example 12
Project: openjdk-jdk10   File: JavatimeTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) throws Throwable {
    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
        int days  = r.nextInt(50) * 365 + r.nextInt(365);
        long secs = t1970 + days * 86400 + r.nextInt(86400);
        int nanos = r.nextInt(NANOS_PER_SECOND);
        int nanos_ms = nanos / 1000000 * 1000000; // millis precision
        long millis = secs * 1000 + r.nextInt(1000);

        LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
        LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
        Instant inst = Instant.ofEpochSecond(secs, nanos);
        Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
        //System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);

        /////////// Timestamp ////////////////////////////////
        Timestamp ta = new Timestamp(millis);
        ta.setNanos(nanos);
        if (!isEqual(ta.toLocalDateTime(), ta)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ta.toLocalDateTime(), ta);
            throw new RuntimeException("FAILED: j.s.ts -> ldt");
        }
        if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ldt, Timestamp.valueOf(ldt));
            throw new RuntimeException("FAILED: ldt -> j.s.ts");
        }
        Instant inst0 = ta.toInstant();
        if (ta.getTime() != inst0.toEpochMilli() ||
            ta.getNanos() != inst0.getNano() ||
            !ta.equals(Timestamp.from(inst0))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
        }
        inst = Instant.ofEpochSecond(secs, nanos);
        Timestamp ta0 = Timestamp.from(inst);
        if (ta0.getTime() != inst.toEpochMilli() ||
            ta0.getNanos() != inst.getNano() ||
            !inst.equals(ta0.toInstant())) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: instant -> timestamp -> instant");
        }

        ////////// java.sql.Date /////////////////////////////
        // j.s.d/t uses j.u.d.equals() !!!!!!!!
        java.sql.Date jsd = new java.sql.Date(millis);
        if (!isEqual(jsd.toLocalDate(), jsd)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jsd.toLocalDate(), jsd);
            throw new RuntimeException("FAILED: j.s.d -> ld");
        }
        LocalDate ld = ldt.toLocalDate();
        if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ld, java.sql.Date.valueOf(ld));
            throw new RuntimeException("FAILED: ld -> j.s.d");
        }
        ////////// java.sql.Time /////////////////////////////
        java.sql.Time jst = new java.sql.Time(millis);
        if (!isEqual(jst.toLocalTime(), jst)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jst.toLocalTime(), jst);
            throw new RuntimeException("FAILED: j.s.t -> lt");
        }
        // millis precision
        LocalTime lt = ldt_ms.toLocalTime();
        if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(lt, java.sql.Time.valueOf(lt));
            throw new RuntimeException("FAILED: lt -> j.s.t");
        }
    }
    System.out.println("Passed!");
}
 
Example 13
Project: nixmash-blog   File: ZoneDateTimeConverter.java   View Source Code Vote up 4 votes
@Override
public java.sql.Timestamp convertToDatabaseColumn(ZonedDateTime entityValue) {
   return Timestamp.from(entityValue.toInstant());
}
 
Example 14
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test(expectedExceptions = NullPointerException.class)
public void test47() throws Exception {
    Instant instant = null;
    Timestamp.from(instant);
}
 
Example 15
Project: OperatieBRP   File: SelectieTaakServiceImpl.java   View Source Code Vote up 4 votes
@Override
public SelectieTaakDTO slaSelectieTaakOp(SelectieTaakDTO selectieTaak) {
    Dienst dienst = referentieRepository.getReferentie(Dienst.class, selectieTaak.getDienstId());
    ToegangLeveringsAutorisatie
            tla =
            referentieRepository.getReferentie(ToegangLeveringsAutorisatie.class, selectieTaak.getToegangLeveringsautorisatieId());
    Selectietaak entiteit;
    if (selectieTaak.getId() == null) {
        entiteit = new Selectietaak(dienst, tla, selectieTaak.getVolgnummer());
        entiteit.setActueelEnGeldig(true);
    } else {
        entiteit = selectieTaakRepository.vindSelectietaak(selectieTaak.getId());
    }
    entiteit.setDatumPlanning(
            DatumUtil.vanDatumNaarInteger(Optional.ofNullable(selectieTaak.getDatumPlanning()).orElse(selectieTaak.getBerekendeSelectieDatum())));
    entiteit.setVolgnummer(selectieTaak.getVolgnummer());
    entiteit.setActueelEnGeldigStatus(true);
    Selectietaak finalEntiteit = entiteit;
    Optional.ofNullable(selectieTaak.getPeilmomentFormeelResultaat())
            .ifPresent(p -> finalEntiteit.setPeilmomentFormeelResultaat(DatumUtil.vanZonedDateTimeNaarSqlTimeStamp(p)));
    Optional.ofNullable(selectieTaak.getPeilmomentMaterieelResultaat())
            .ifPresent(p -> finalEntiteit.setPeilmomentMaterieelResultaat(DatumUtil.vanDatumNaarInteger(p)));
    // historierecord selectietaak
    Timestamp tsreg = Timestamp.from(ZonedDateTime.now().toInstant());
    SelectietaakHistorie historie = new SelectietaakHistorie(entiteit);
    historie.setDatumPlanning(entiteit.getDatumPlanning());
    historie.setDatumUitvoer(entiteit.getDatumUitvoer());
    historie.setIndicatieSelectielijstGebruiken(Optional.ofNullable(entiteit.isIndicatieSelectielijstGebruiken()).orElse(false));
    historie.setPeilmomentMaterieelResultaat(entiteit.getPeilmomentMaterieelResultaat());
    historie.setPeilmomentFormeelResultaat(entiteit.getPeilmomentFormeelResultaat());
    historie.setDatumTijdRegistratie(tsreg);
    entiteit.getSelectietaakHistorieSet().stream().filter(h -> h.getSelectietaak().getId().equals(finalEntiteit.getId()) && h.getDatumTijdVerval() == null)
            .findFirst()
            .ifPresent(h -> h.setDatumTijdVerval(tsreg));
    entiteit.getSelectietaakHistorieSet().add(historie);
    if (entiteit.getId() == null || statusGewijzigd(entiteit, selectieTaak)) {
        // historierecord selectiestatus
        final SelectietaakStatusHistorie statusHistorie = new SelectietaakStatusHistorie(entiteit);
        statusHistorie.setDatumTijdRegistratie(tsreg);
        statusHistorie.setStatusGewijzigdDoor("Systeem");
        statusHistorie.setStatus(selectieTaak.getStatus());
        statusHistorie.setStatusToelichting(selectieTaak.getStatusToelichting());
        entiteit.getSelectietaakStatusHistorieSet().stream()
                .filter(h -> h.getSelectietaak().getId().equals(finalEntiteit.getId()) && h.getDatumTijdVerval() == null).findFirst()
                .ifPresent(h -> h.setDatumTijdVerval(tsreg));
        entiteit.getSelectietaakStatusHistorieSet().add(statusHistorie);
        //update a-laag
        entiteit.setStatus(selectieTaak.getStatus());
        entiteit.setStatusToelichting(selectieTaak.getStatusToelichting());
    }
    if (selectieTaak.getId() == null) {
        entiteit = selectieTaakRepository.slaOp(entiteit);
    }
    selectieTaak.setId(entiteit.getId());
    return selectieTaak;
}
 
Example 16
Project: jdk8u-jdk   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test(expectedExceptions = NullPointerException.class)
public void test47() throws Exception {
    Instant instant = null;
    Timestamp.from(instant);
}
 
Example 17
Project: jdk8u-jdk   File: JavatimeTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) throws Throwable {
    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
        int days  = r.nextInt(50) * 365 + r.nextInt(365);
        long secs = t1970 + days * 86400 + r.nextInt(86400);
        int nanos = r.nextInt(NANOS_PER_SECOND);
        int nanos_ms = nanos / 1000000 * 1000000; // millis precision
        long millis = secs * 1000 + r.nextInt(1000);

        LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
        LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
        Instant inst = Instant.ofEpochSecond(secs, nanos);
        Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
        //System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);

        /////////// Timestamp ////////////////////////////////
        Timestamp ta = new Timestamp(millis);
        ta.setNanos(nanos);
        if (!isEqual(ta.toLocalDateTime(), ta)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ta.toLocalDateTime(), ta);
            throw new RuntimeException("FAILED: j.s.ts -> ldt");
        }
        if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ldt, Timestamp.valueOf(ldt));
            throw new RuntimeException("FAILED: ldt -> j.s.ts");
        }
        Instant inst0 = ta.toInstant();
        if (ta.getTime() != inst0.toEpochMilli() ||
            ta.getNanos() != inst0.getNano() ||
            !ta.equals(Timestamp.from(inst0))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
        }
        inst = Instant.ofEpochSecond(secs, nanos);
        Timestamp ta0 = Timestamp.from(inst);
        if (ta0.getTime() != inst.toEpochMilli() ||
            ta0.getNanos() != inst.getNano() ||
            !inst.equals(ta0.toInstant())) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: instant -> timestamp -> instant");
        }

        ////////// java.sql.Date /////////////////////////////
        // j.s.d/t uses j.u.d.equals() !!!!!!!!
        java.sql.Date jsd = new java.sql.Date(millis);
        if (!isEqual(jsd.toLocalDate(), jsd)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jsd.toLocalDate(), jsd);
            throw new RuntimeException("FAILED: j.s.d -> ld");
        }
        LocalDate ld = ldt.toLocalDate();
        if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ld, java.sql.Date.valueOf(ld));
            throw new RuntimeException("FAILED: ld -> j.s.d");
        }
        ////////// java.sql.Time /////////////////////////////
        java.sql.Time jst = new java.sql.Time(millis);
        if (!isEqual(jst.toLocalTime(), jst)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jst.toLocalTime(), jst);
            throw new RuntimeException("FAILED: j.s.t -> lt");
        }
        // millis precision
        LocalTime lt = ldt_ms.toLocalTime();
        if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(lt, java.sql.Time.valueOf(lt));
            throw new RuntimeException("FAILED: lt -> j.s.t");
        }
    }
    System.out.println("Passed!");
}
 
Example 18
Project: nakadi-producer-spring-boot-starter   File: EventLogRepositoryImpl.java   View Source Code Vote up 4 votes
private Timestamp toSqlTimestamp(Instant now) {
    if (now == null) {
        return null;
    }
    return Timestamp.from(now);
}
 
Example 19
Project: cas-5.1.0   File: DateTimeUtils.java   View Source Code Vote up 2 votes
/**
 * Gets Timestamp for Instant.
 *
 * @param time Time object to be converted.
 * @return Timestamp representing time
 */
private static Timestamp timestampOf(final Instant time) {
    return Timestamp.from(time);
}
 
Example 20
Project: OperatieBRP   File: DatumTijdElement.java   View Source Code Vote up 2 votes
/**
 * Geeft de waarde van deze datum/tijd terug als timestamp zodat deze kan worden opgeslagen in de database.
 *
 * @return timestamp
 */
public Timestamp toTimestamp() {
    return Timestamp.from(getWaarde().toInstant());
}