Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
271 views
Welcome To Ask or Share your Answers For Others

1 Answer

Java 9+

LocalDate.ofInstant(...); arrived in Java 9.

Instant instant = Instant.parse( "2020-01-23T00:00:00Z" );
ZoneId z = ZoneId.of( "America/Edmonton" );
LocalDate ld = LocalDate.ofInstant( instant , z );

See code run live at IdeOne.com.

Notice the date is 22nd rather than 23rd as that time zone uses an offset several hours before UTC.

2020-01-22

Java 8

ZonedDateTime has a .toLocalDate() method in Java 8.

yourInstant.atZone(yourZoneId).toLocalDate(); Will work with earlier versions for LocalDate...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...