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
1 answer

java.time.Instant - error while parsing a huge date

I'm trying to parse a really large date (but still way less than Instant.MAX) using the Instant.parse method but getting an error. String input = "78000000-01-01T00:00:00Z"; Instant instant = Instant.parse(input); Exception: Exception in thread…
Sogawa-sps
  • 147
  • 10
1
vote
1 answer

Set datetime field now in MapStruct?

I have an Instant field in my POJO class and want to set its value now() while creating record. As far as I see, MapStruct let this kind of feature, but I could not set it properly: mapper: @Mapper(componentModel = "spring", imports =…
user21263160
1
vote
0 answers

Spring @Scheduled cron job in combination with Instant.now()

In Spring I use standard CRON job scheduling to do some business level measurements every 10 minutes with this example @Async @Scheduled(cron = "0 0/10 * * * ?") public void takeMeasurements() { Instant dateTimeNow = Instant.now(); //…
Lukas
  • 31
  • 3
1
vote
2 answers

How to elegantly convert from MSSQL Datetime2 to java.time.Instant

I have a simple spring boot REST API application, using plain jdbc to fetch data from a MSSQL DB. I am trying to figure out how best to retrieve a DATETIME2 column from the DB (which stores no timezone info), and serialize it as a UTC timestamp (and…
BoomShaka
  • 1,571
  • 7
  • 27
  • 40
1
vote
1 answer

ofInstant is not a member of object java.time.LocalDate

I have following scala code: LocalDate.ofInstant(instant, zoneId) instant is java.time.Instant, zoneId is java.time.ZoneId. Intellij project is configured to jdk 17 and language level to 15. But I get error: value ofInstant is not a member of…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
1
vote
1 answer

How can I convert an Instant during a Restassured request?

I want to perform a Restassured GET request. The response contains a list of objects which in turn have an Instant property. Currently I get an exception: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type…
du-it
  • 2,561
  • 8
  • 42
  • 80
1
vote
3 answers

Converting string date time with +0000 To instant

How do I convert the datetime that is passed as string 2018-08-10T18:25:00.000+0000 to a Instant? I tried the below and it did not work. public static Instant toInstant(final String timeStr) { if (StringUtils.isBlank(timeStr)) { throw…
serah
  • 2,057
  • 7
  • 36
  • 56
1
vote
0 answers

ZonedDateTime to Instant is right in Java 8 but off by 1h in Java 9+

When running the following code in Java 8, the Instant produced from parsing the String "2021-03-26T23:43:03+01:00[Europe/London]" is "2021-03-26T23:43:03Z" (which afaik is right since in 2021 DST kicked in on March the 28th, so on the 26th London…
MTdP
  • 11
  • 3
1
vote
2 answers

How to use Instant Java class to parse a date time with offset from UTC?

I am trying to parse a date time with Instant.parse() with time zone offset. I read the documentation and it said I must do that using the ISO-8601. I read the Wikipedia's ISO-8601 article and I copied this date time "2007-11-03T13:18:05-03:00" from…
R0land013
  • 80
  • 1
  • 8
1
vote
2 answers

Timestamp can't be parsed Issue java.time.format.DateTimeParseException: Text '9/25/2020, 12:46:00 PM' could not be parsed at index 0

I am attempting to capture the time before and after I check a timestamp value on a webpage and then confirm my timestamp falls in between those times. However, I'm struggling to convert my String timestamp into a comparable format in a clean way. …
1
vote
1 answer

Exception during deserialize java.time.Instant from redis cache

I keep getting following exception while reading data from cache. org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist):…
1
vote
1 answer

modernizer-maven-plugin Joda Instant

I am trying to construct an org.joda.time.Instant from a LocalDate. Ordinarily it is as simple as; new org.joda.time.Instant(myDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()); But the project I am working on uses the…
1
vote
0 answers

use pattern with @JsonFormat for java.time.Instant. Do not want to manually write code for it and use annotations

unsupportedtemporaltypeexception with @JsonFormat on POJO with the property of type java.time.Instant Error java.time.temporal.unsupportedtemporaltypeexception unsupported field yearofera I do not want to write a manual code for conversion. Please…
1
vote
1 answer

Incorrect milliseconds in long value retrieved from MySQL in Java

First of I was trying to insert timestamp in millisecond precision in my mysql database, which I got success by this code: Instant instant = Instant.now().truncatedTo(ChronoUnit.MILLIS); try (Connection conn = DbConnection.getCon(); …
Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31
1
vote
3 answers

Java Timestamp with Fractional Seconds

How can I print Java Instant as a timestamp with fractional seconds like 1558766955.037 ? The precision needed is to 1/1000, as the example shows. I tried (double) timestamp.getEpochSecond() + (double) timestamp.getNano() / 1000_000_000, but when I…
J Freebird
  • 3,664
  • 7
  • 46
  • 81
1 2 3
8 9