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.
92 questions
99
votes
7 answers

Is there any way to convert ZoneId to ZoneOffset in java 8?

I have an epoch second and a zoneId,by method1.It can be convert to LocalDateTime with system default zoneId,but I don't find the way to convert epoch second to LocalDateTime by method2,because there is no ZoneOffset.systemDefault.I think it's…
Renkai
  • 1,500
  • 1
  • 9
  • 14
62
votes
6 answers

How to convert from Instant to LocalDate

I have an Instant coming from a source that should, according to the spec, be a LocalDate, but don't see any methods in LocalDate for the conversion. What is the best way to do this?
JL Gradley
  • 1,205
  • 2
  • 11
  • 14
47
votes
5 answers

Java - Convert java.time.Instant to java.sql.Timestamp without Zone offset

In the application I am developing, I need to convert java.time.Instant object to java.sql.Timestamp. When I create Instant object like: Instant now = Instant.now(); I receive something like 2017-03-13T14:28:59.970Z. And when I try to create…
Aleydin Karaimin
  • 794
  • 1
  • 5
  • 13
33
votes
2 answers

Converting string to Instant

I am trying to covert datetime in string to instant using java 8 or utils package. For eg. String requestTime = "04:30 PM, Sat 5/12/2018"; to Instant reqInstant should result in 2018-05-12T20:30:00.000Z reqString is in America/Toronto…
Venky
  • 567
  • 2
  • 8
  • 22
24
votes
2 answers

converting Joda time Instant to Java time Instant

I have an instance of Instant (org.joda.time.Instant) which I get in some api response. I have another instance from (java.time.Instant) which I get from some other call. Now, I want to compare these two object to check which one get the latest one.…
user123475
  • 817
  • 3
  • 9
  • 25
19
votes
2 answers

Why JPA does not support java.time.Instant?

I think that java.time.Instant is the best choice to store a date into DB: it is the most likely TIMESTAMP and you are not depending by timezone, it is just a moment on the time. JPA supports LocalDate, LocalTime, LocalDateTime etc. but not…
Filosssof
  • 1,107
  • 2
  • 15
  • 38
14
votes
1 answer

Instant vs ZonedDateTime

I just do not quite understand which one of those two I should use for the following example: We have an OfferEntity which has a member availableDay which is the date at which the offer is available. Now, the table will look something like…
11
votes
1 answer

java 8 time api - Instant.now(clock) vs LocaldateTime.now(clock)

For following code for java 8 1. System.out.println(LocalDateTime.now(Clock.systemDefaultZone())); 2. System.out.println(Instant.now(Clock.systemDefaultZone())); Line 1 print current time by adding offset but line 2 print current time without…
nantitv
  • 3,063
  • 32
  • 46
10
votes
3 answers

Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()

In Java, we can have many different ways to get the current timestamp, but which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()
Yishu Fang
  • 8,258
  • 13
  • 57
  • 93
10
votes
1 answer

How to store a Java Instant in a MySQL database

With Java Date objects the easiest way to go was to store them as MySql DateTime objects (in UTC). With the switch to Instant this approach won't work anymore because MySQL DateTime does not offer the precision to store nanoseconds. Just truncating…
NotX
  • 402
  • 6
  • 14
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
  • 22,779
  • 27
  • 83
  • 163
8
votes
2 answers

Timestamp to Instant in Java

I have a pojo which has a filed type as Instant. I want to set the Instant getting it from TimsStamp. Would it be possible? For example: I have a java.sql.Timestamp which I want to convert to java.time.Instant. Would it be possible?
user123475
  • 817
  • 3
  • 9
  • 25
7
votes
1 answer

Calculate days, hours and minutes between two instants

I have an instant formatted like this: DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT ) .withLocale( Locale.UK ) .withZone(…
Mateus Henrique
  • 117
  • 1
  • 6
7
votes
5 answers

Why is adding weeks to java.time.Instant not supported?

The following piece of code: Instant inFourWeeks = Instant.now().plus(4L, ChronoUnit.WEEKS); Throws an exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks Why are weeks unsupported? I understand why months and…
Snackoverflow
  • 2,219
  • 3
  • 24
  • 50
7
votes
2 answers

Convert Date from ISO 8601 Zulu string to java.time.Instant in Java 8

I want to convert string date format into java.time.Instant I am getting exception while parsing date. java.lang.IllegalArgumentException: Too many pattern letters: s I am using below code for conversion first from String to date. String…
1
2 3 4 5 6 7