so I have two Instant from JAVA8,
Instant time1 = Instant.parse("2020-11-03T11:35:02.510Z")
Instant time2 = Instant.parse("2020-10-03T07:15:22.432Z")
and I want to remove the date information in these two instants and only keep the time(which is hour, minute, second, and millisecond with the timezone) and compare them. May I know how to do that? Appreciate your help!
Asked
Active
Viewed 797 times
-2

yueran zhang
- 33
- 3
-
An `Instant` is a moment in time, and at that moment it is never the same date everywhere on Earth. So which date do you want? – Ole V.V. Nov 05 '20 at 03:42
-
Does this answer your question? [How to convert from Instant to LocalDate](https://stackoverflow.com/questions/52264768/how-to-convert-from-instant-to-localdate). I believe it would have been more helpful to close this question as a duplicate than as unclear. – Ole V.V. Nov 05 '20 at 03:44
1 Answers
1
Like this. But you will need to provide an appropriate ZoneId
and of course any special formatting for the date. This uses LocalDate's toString()
method.
Instant time1 = Instant.parse("2020-11-03T11:35:02.510Z");
LocalDate ld = LocalDate.ofInstant(time1,ZoneId.of("GMT"));
System.out.println(ld);
Since you have the String to parse, you could do it like this.
LocalDate ld1 = LocalDateTime.parse("2020-1103T11:35:02.510Z",
DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.toLocalDate();
Prints
2020-11-03

WJS
- 36,363
- 4
- 24
- 39
-
thx, I have tried but my ide says cannot resolve method 'ofInstant' in 'LocalTime'.. and I import the java.time.LocalTime – yueran zhang Nov 03 '20 at 16:42
-
-