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
1
vote
0 answers

Spring Data Jpa : java 8 instant comparaison

In my app i need to search an order by his creation_date. In my entity, the type of the creationDate field is java.time.Instant. In my class ManualOrderSpecification i need to check the creation_date. I would like to compare the date of creation…
loic
  • 511
  • 1
  • 4
  • 5
1
vote
2 answers

postgresql Date vs java Instant

Suppose I have a field in Java app of type Instant and store it in PostgreSQL database as type DATE. If I save for example 21.02.2019 14:03:59 it will be stored as 21.02.2019 but how will it be re-translated when I read from database? Will it…
LechP
  • 731
  • 1
  • 7
  • 8
1
vote
1 answer

Why is Java 8 Instant.now() showing the wrong UTC on my local server?

Java8 Instant.now() gives the current time already formatted in UTC. I created a simple sandbox test on CompileJava.net to verify my intented results: Timestamp createdDateUtc =…
amallard
  • 1,189
  • 1
  • 12
  • 24
1
vote
2 answers

java.time.Instant (1.8) is thread safe?

Instant instant; void updateBy(){ instant = Instant.now(); } if yes, How to prove Instant is thread safe?
1
vote
1 answer

How can I test a spring-cloud-contract containing a java.time.Instant field

I want to test a contract where one field is of type java.time.Instant. But not all instances of an Instant are handled as I expect by spring-cloud-contract. Given the following simple contract: Contract.make { description("Get a version") …
1
vote
4 answers

String to Long in Java

My requirement: I get value in EpochMillis public void Method(){ //long val = epoch time in millis //Format the (val)epochtime in MMddHHmmss and set ZoneOffset.UTC //long formattedTime = formatted time; …
msa
  • 31
  • 1
  • 7
1
vote
2 answers

Check that Instant now happens during the java.time.Period / Duration

My intent is to set a condition to true only during a period. The java.time.* API looked what I need. import java.time.Period import java.time.LocalTime import java.time.Instant import java.time.Duration // java.time.Period LocalTime start =…
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
1
vote
3 answers

Timestamp.from not heeding timezone from Instant

When I try to convert a ZonedDateTime to a Timestamp everything is fine until I call Timestamp.from() in the following code: ZonedDateTime currentTimeUTC = ZonedDateTime.now(ZoneOffset.UTC); currentTimeUTC =…
maio290
  • 6,440
  • 1
  • 21
  • 38
1
vote
1 answer

How can I get a specific time Today in UTC java

I am trying to get a specific time in UTC for "today". Say 5pm UTC Instant.now().truncatedTo(ChronoUnit.DAYS).plus(1, ChronoUnit.DAYS) OR Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS) This i believe gets me to midnight…
Pradyot
  • 2,897
  • 7
  • 41
  • 58
1
vote
0 answers

String to LocalDateTime raises an DateTimeParseException

I'm facing some trouble trying to convert String timestamp to LocalDateTime I receive this string from database 2018013014364112 and it contains date and time which should be 2018-01-30 14:36:41.12 written in more readable format. When I try to…
1
vote
1 answer

java.time.Instant.plus(long amountToAdd, TemporalUnit unit) Unsupported unit

I trying to add few years to current time. My code looks like: // ten yeas ago int backYears = 10; Instant instant = ChronoUnit.YEARS.addTo(Instant.now(), -backYears); But I got an exception: java.time.temporal.UnsupportedTemporalTypeException:…
Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
1
vote
1 answer

Confused about Instant, ZonedDateTime and UTC

im trying to understand the concept of UTC and the new TimeApi from Java 8. Instant from = Instant.from(ZonedDateTime.of(2016, 12, 11, 00, 23, 24, 245, ZoneId.systemDefault())); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd…
Hakan Kiyar
  • 1,199
  • 6
  • 16
  • 26
0
votes
2 answers

Unable to obtain Instant from TemporalAccessor: 2023-08-31T20:37:49.005832800 of type java.time.LocalDateTime

I am using parmeterised method for Temporal class and passing LocalDateTime to Temporal argument and getting error in Instant.from(Temporal t) method where, t instancof LocalDateTime in my case. Here is program, public Class DateTimeUtils { public…
gajesh
  • 5
  • 2
0
votes
2 answers

Java PreparedStatement reading local timezone when using Timestamp.from(Instant)

I'm attempting to populate a UTC timestamp into a SQL table, but when I use Instant.now() to get the current UTC time, a conversion of Timestamp.from(instant) is writing local time zones into the table. Is there a way to write UTC into the…
Randy B
  • 31
  • 3
0
votes
2 answers

Instant incorrectly parsing epoch string with rest api

I have a rest API with an input value which is an epoch. However, storing as an Instant ignores the nanos, thus storing it as some date in the future (I think). I could be storing them in the wrong type also as had thought about storing them as a…
Droid_Interceptor
  • 613
  • 3
  • 14
  • 37
1 2 3
8 9