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
2
votes
2 answers

parsing date from "06/25/2021 10:26:33.0" format to "2021-06-25T10:26:33.000-04:00" using java.time

I have to convert a date for the purpose of comparison using junit. I get a date from DB which is "06/25/2021 10:26:33.0" and I have to convert it to "2021-06-25T10:26:33.000-04:00" before I use it in the asserts. I am trying not to use SimpleDate…
Monnie_tester
  • 439
  • 1
  • 6
  • 20
2
votes
1 answer

Converting from Instant to XMLGregorianCalendar and vice-versa without losing precision

Using, java.time.Instant and javax.xml.datatype.XMLGregorianCalendar, I'm trying to convert from one to the other without losing precision. Why is this test not passing and how to fix it? class FooTest { @Test void…
2
votes
2 answers

Instant plus with negative values

I have this code Instant now = Instant.now(); if (amountDays >= 0) { now = now.plus(amountDays, ChronoUnit.DAYS); } else { now = now.minus(Math.abs(amountDays), ChronoUnit.DAYS); } And I was thinking to simplify it like this Instant now =…
KunLun
  • 3,109
  • 3
  • 18
  • 65
2
votes
1 answer

How to use SQL timestamp with java Instant in jOOQ?

I want to use Instant type to put it to MySQL database (timestamp field). Unfortunately, when using POJO and Record#from(), jOOQ doesn't let me do that for some reason. I have the following to my gradle configuration: forcedTypes { forcedType { …
mrDoctorWho
  • 143
  • 8
2
votes
0 answers

Why java.time.Instant doesn't show any leap seconds?

Edit below with summary of answer: I'm trying to understand a bit more about java.time.Instant and leap seconds. If I run this: System.out.println(Instant.ofEpochSecond(60*60*24*365*50)); I expected to create a time/date that's near the start of…
Toby Eggitt
  • 1,806
  • 19
  • 24
2
votes
2 answers

ZonedDateTime.toInstant().toEpochMilli() losing zone data

I am using TrueTime library which returns Date at the system time zone. I am having problems converting this Date to UTC Date when converted to milliseconds. Here is what I did: // getting true time in GMT [ex : 2020-07-13T18:00:57.192+03:00…
amira
  • 416
  • 1
  • 7
  • 24
2
votes
2 answers

How do you represent MS-DTYP `DATETIME` in Java 8 Instant?

In the Microsoft Spec, DATETIME is represented as 2 32-bit integers: low and high Reference: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/cca27429-5689-4a16-b2b4-9325d93e4ba2 The FILETIME structure is a 64-bit value that…
Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
2
votes
2 answers

Optional to Optional

i have to convert an Option to Optional, and created the code for this but i believe it could be even shorter that the private method is not used. Optional optionalLong ; Optional
2
votes
1 answer

Array of Instants from Strings

I have an ArrayList that I want to be type Instant, but no matter what I try to do on the array it just does not let me convert it to Instant format. The error is that it tries to add a String to an ArrayList of…
Lazar Gugleta
  • 115
  • 1
  • 2
  • 14
2
votes
3 answers

Java Date comparison: Dates in different formats

I have two dates in String format as: say String date1 = 2018-08-29 and in ISO-OFFSET_DATE_TIME format as String date2 = 2018-08-30T00:00:00+10:00. What is the best way to compare date1 and date2 for equality? I am not concerned about time, only…
Krishna
  • 137
  • 1
  • 2
  • 8
2
votes
1 answer

java.sql.Timestamp made from java.time.Instant's MIN/MAX behaves differently than when constructed from Long.MIN_VALUE / Long.MAX_VALUE

I came across this issue while writing a test case where I had to get a range of records between a range of timestamps –– using H2 embedded database with spring-data-jpa. The original issue is located at: Fetching records BETWEEN two…
sidmishraw
  • 390
  • 4
  • 15
2
votes
1 answer

Why is Java's DateTimeFormatter not parsing/formatting round-trip over Instants safely?

Question Maybe I am misusing the API here or missing some piece of information. Is this a bug or a design flaw of the API? Follow up Lately I was using java.time (in scala's REPL but it is not really of interest) to parse and format a date which…
isaias-b
  • 2,255
  • 2
  • 25
  • 38
2
votes
3 answers

java.time.OffsetDateTime: Unable to obtain OffsetDateTime from TemporalAccessor

I'm trying to parse "20140726080320+0400" using "yyyyMMddHHmmssZ" format as follows: System.out.println("********************" + OffsetDateTime .parse("20140726080320+0400", …
2
votes
3 answers

Spring MVC GET request with Java 8 Instant request parameter

I'm trying to write a Spring MVC GET controller that takes a Java 8 Instant as request parameter: @GetMapping @JsonView(OrderListing.class) @Validated public WebSeekPaginatedResultsDto findAll(@Valid…
user2490936
  • 129
  • 2
  • 14
1
vote
0 answers

How to map java.time.Instant to Nodatime.Instant when creating a Xamarin Android Native binding

I'm creating a Xamarin Native binding for an Android library, and one of the issues I notice is that types from java.time (specifically java.time.Instant type) does not get mapped. I assume because of this issue:…
1 2 3
8 9