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

How can I add 18 as day to current month using java Instant

I can get the current date using Instant.now() I am looking to get 18--
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
0
votes
1 answer

Why is conversion from LocalDateTime to Instant not working?

Do you know why 4. and 6. prints have wrong hours in following code? LocalDateTime ldtNow = LocalDateTime.now(); LocalDateTime ldtNextMonth = ldtNow.plusMonths(1); System.out.println("1. " + ldtNow); System.out.println("2. " +…
APTower
  • 343
  • 2
  • 8
0
votes
2 answers

Calculate the difference between 2 variables of type Instant

For my exercise I have to calculate the difference (long duration) between the variables Instant inHour and Instant outHour . In other words, I have to calculate the time that a person stayed in the parking to calculate the price. This is the first…
0
votes
3 answers

How to convert Sting into instant in Java

How to convert this string into instant: String date = "Fri Sep 30 00:00:00 IST 2022"; Exception: java.time.format.DateTimeParseException: Text 'Fri Sep 30 00:00:00 IST 2022' could not be parsed at index 0 at…
0
votes
2 answers

Different ZonedDateTime of GMT and BST become same Instant after toInstant conversion in Scala

to summarize my question, I have 2 different ZonedDateTime objects with GMT and BST but same local time. They become the same Instant values after toInstant conversion. I am expecting after conversion, they should be different values because…
0
votes
0 answers

Is it bad practice to use LocalDateTime for Postgres timestamps on a UTC machine as an alternative for Instant?

First of all I'm aware of this excellent answer on the difference between Instant and LocalDateTime in Java. Both have nano-second precision but the value of LocalDateTime.now() depends on the machine timezone. I save time in Postgres database using…
hitchhiker
  • 1,099
  • 5
  • 19
  • 44
0
votes
1 answer

Convert Java Instant to String with timezone

I have to compare a instant with a date String but format are differents. The Instant is initiate with "now" but without "+nnnn" part (I think it's timezone). How can I create a Instant and convert it to String with this format for compare with my…
0
votes
1 answer

incompatible types: long cannot be converted to Instant

I'm testing an example from here. But getting this error incompatible types: long cannot be converted to Instant . Code: import java.time.Instant; import java.time.temporal.Temporal; import java.time.temporal.ChronoUnit; class HelloWorld { …
Rifat
  • 1,700
  • 3
  • 20
  • 51
0
votes
0 answers

My Instant.now() don't work, but when it does i get another problem

This is some basic program that I trying to build to learn some Java before I'm starting in programming courses. I can't get the time to pulloff in the right time interval between Start = Instant.now() and Ends = Instant.now(). But the code works if…
Amblox
  • 1
0
votes
1 answer

how to create a Instant class variable in kotlin with my own timestamp

Till now i was using val date = Instant.now(Clock.system(ZoneId.of("UTC"))) to generate the instant timestamp. Now I need to substitute it with the date that I want to specify for example "2021-05-03T00:00:00.000Z". When i insert it as a string…
Saba
  • 416
  • 3
  • 14
0
votes
0 answers

Java.time not found - JDK 13

compilation snap-1 compilation snap-2 I'm trying to upgrade my project from java 1.8 to version 13.0.2. There were some unsupported functions which I removed. Unfortunately toInstant() is one of them. My project uses it in a java module like…
0
votes
3 answers

convert java timestamp string to java timestamp object

I have following timestamp string "2021010112:12:12.10:00" and I want to convert it to java.time.Instant. The issue in parsing string through DateTimeFormatter is due to absence of time zone sign before last 4 digits. Logically it is…
0
votes
0 answers

MariaDB driver not supporting reading Instant from DB, but supports write

I am aware that JDBC 4.2 drivers support reading and writing java.time objects. I tried storing and retrieving an Instant object using MariaDB 2.6 driver. I am using Apache Commons DB Utils to run the queries. I can store the Instants just fine, but…
nikel
  • 3,402
  • 11
  • 45
  • 71
0
votes
2 answers

Java DateTimeFormatter Month Of Year Format

I am trying to convert an Instant to a String with the format like "10 Jul 2021, 10:00 PM". DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLL yyyy, hh:mm a"); String formattedDate =…
0
votes
2 answers

convert instant to LocalDate and got java.time.DateTimeException: Invalid value for Year

I converted an instant to LocalDate and here is my implementation: theInstant.atZone(ZoneId.systemDefault()).toLocalDate(); However, I got the exception in Junit test: java.time.DateTimeException: Invalid value for Year (valid values -999999999 -…
1 2 3
8 9