Questions tagged [java.time.instant]

The Java class to represent an instantaneous point on the time-line. This might be used to record event time-stamps in the application. Use this tag for questions about the java.time.Instant class or its ThreeTen Backport equoivalent, org.threeten.bp.Instant. If your question involves more classes from the java.time API, rather consider the java-time tag and/or the jsr310 tag.

The Instant class is part of the modern Java date and time API known as java.time or JSR-310. It replaces the old classes java.util.Date and java.sql.Timestamp.

As part of the modern API Instant is built-in with Java 8 and later, and is available in Java 6 and 7 through the ThreeTen Backport, the backport of JSR-310 to Java 6 and 7.

The Instant class is immutable and therefore threadsafe.

Motivation: this tag was created in part because the instant tag was often incorrectly used for questions about java.time.Instant.

Resources:

  1. The official documentation of java.time.Instant (javadoc)
  2. ThreeTen Backport - provides a backport of the Java SE 8 date-time classes to Java SE 6 and 7.
133 questions
9
votes
1 answer

What is the main purpose of the new interface java.time.InstantSource over existing abstract class Clock?

A new interface java.time.InstantSource was added in Java 17. What is the use case of that additional abstraction if all implementations of that interface are Clock implementations too anyway?
9
votes
2 answers

What Java DateTime class should I use?

We have a library used for generating reports. It reads from a data file (SQL, XML, JSON, etc.), the datetime may then be modified in a user written equation, and then it is formatted as specified for the report output. The use in an equation can be…
David Thielen
  • 28,723
  • 34
  • 119
  • 193
8
votes
1 answer

Mapstruct LocalDateTime to Instant

I am new in Mapstruct. I have a model object which includes LocalDateTime type field. DTO includes Instant type field. I want to map LocalDateTime type field to Instant type field. I have TimeZone instance of incoming requests. Manually field…
8
votes
3 answers

Java 8 date/time: instant, could not be parsed at index 19

I have following piece of code: String dateInString = "2016-09-18T12:17:21:000Z"; Instant instant = Instant.parse(dateInString); ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Kiev")); System.out.println(zonedDateTime); It gives me…
Green Root
  • 644
  • 2
  • 10
  • 28
7
votes
1 answer

Create Java DateTime Instant from microseconds

There has been changes in Java Date & Time API Since Java 9. LocalDateTime now has microseconds precision. Java 9 has a fresh implementation of java.time.Clock capable of capturing the current moment in resolution finer than milliseconds (three…
DJViking
  • 832
  • 1
  • 12
  • 29
7
votes
2 answers

java.time and JPA

Classes as LocalDateTime from the package java.time are value based classes. If I have an entity using such an object as a field I run into the following "problem": Value based classes shouldn't be serialized. However, the JPA entity has to…
itsme
  • 852
  • 1
  • 10
  • 23
7
votes
1 answer

Java Years between 2 Instants

In Joda, we can calculate years between 2 Datetime using Years.between(dateTime1, dateTime2); Is there any easy way to find years between 2 instants using the java.time API instead without much logic? ChronoUnit.YEARS.between(instant1,…
user1578872
  • 7,808
  • 29
  • 108
  • 206
6
votes
3 answers

How to return Java 'java.time.Instant' property as json value in Restful API body response?

I have a Spring Boot restful API service that returns a Java object in its response which is translated into json. One of the Java object properties is a 'Java.time.Instant'. How should I translate this for the json object being…
user2868835
  • 1,250
  • 3
  • 19
  • 33
5
votes
5 answers

Ordering Set by Instant

I have a Java Set that I give information to: Set dataLocations = getData(location); I would like to sort this Set I have tried a sortedSet and couldn't get it to work, so I tried this dataLocations =…
TOTOROCATBUS
  • 172
  • 1
  • 2
  • 17
5
votes
2 answers

Query on Date only with Spring Boot Data JPA / Java 8 Instant?

I have a Spring boot 1.4.x application that uses the starter jpa in combination with H2/Postgresql. I have an entity that stores a date as an Instant (with Instant.now(), based on Java 8: What's the difference between Instant and LocalDateTime?…
Busata
  • 1,088
  • 1
  • 10
  • 24
4
votes
2 answers

Convert timestamp with offset to UTC

Conversion I have a timestamp with an offset (-6) in the following format: 2019-11-30T00:01:00.000-06:00 and I want to convert it to an UTC timestamp, like: 2019-11-30T06:01:00.000Z Attempt I tried it the following way: String text =…
F0cus
  • 585
  • 3
  • 18
  • 52
4
votes
2 answers

Converting LocalDateTime to Instant give different values

I am trying to use LocalDateTime to manipulate dates in my application but I have noticed that getting epoch seconds from it returns different values from what I expected val now1 = Instant.now().epochSecond - 60 val now2 =…
Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53
4
votes
1 answer

Round instant with more than 6 digits (nanoseconds) after comma

I have postgresql with TIMESTAMP field, which have Instant with date and time. For example: 2021-10-13T15:04:24.944921Z. As you can see, there are 6 digits after comma - 944921. But what if I have more digits, for example:…
SlandShow
  • 443
  • 3
  • 13
4
votes
4 answers

How to find all Instants which correspond to the given LocalDateTime and ZoneId?

When converting Instant to LocalDateTime it may happen that several different Instants are converted into the same LocalDateTime. Eg. in time zones with day light saving time. My question is whether it's possible to write a general function which…
Radek Micek
  • 447
  • 2
  • 9
4
votes
1 answer

Insert Instant datetime into MySql database with millisecond precision in java

I am using mysql 5.7.x version. Also i am using java 8. I am trying to insert java.time.instant current datetime in millisecond precision into my mysql database from java code. For that I am using preparedstatement. The table in my database…
Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31
1
2
3
8 9