How to construct ZonedDateTime from an Instant and a time string?

YAM

Given an object of Instant, a time string representing the time at a specific ZoneId, How to construct a ZonedDateTime object with the date part (year, month, day) from the instant at the given ZoneId and the time part from the given time string?

For example:

Given an object of Instant of value 1437404400000 (equivalent to 20-07-2015 15:00 UTC), a time string 21:00, and an object of ZoneId representing Europe/London, I want to construct an object of ZonedDateTime equivalent to 20-07-2015 21:00 Europe/London.

Jon Skeet

You'll want to parse the time string to a LocalTime first, then you can adjust a ZonedDateTime from the Instant with the zone, and then apply the time. For example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm", Locale.US");
LocalTime time = LocalTime.parse(timeText, formatter);
ZonedDateTime zoned = instant.atZone(zoneId)
                             .with(time);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get `Instant` from `ZonedDateTime` in java.time

Date from ZonedDateTime instant not in utc

How to construct javascript date object from date and time string

NodaTime: Instant to ZonedDateTime without skewing time

Format ZonedDateTime and Instant to different string format

How to construct a Date from a String

How to format Date string to date and time using ZonedDateTime?

How to construct an ObjectId from a negative time value

How to format a ZonedDateTime to a String?

How to format a ZonedDateTime to a String?

How to Construct a String from Data Dictionary in Python?

How to construct a timedelta object from a simple string

How to construct a String instance from a sequence of integers?

How to construct pymongo query string from dictionary

How to construct a Currency Locale from a String?

How to Construct Object from Xml string

Kotlin/java - How to convert a date time string to Instant?

How to set format of string for java.time.Instant using objectMapper?

How to serialize java.time.Instant as ISO string in Spring Webflux

How to compare a ZonedDateTime's time with another ZonedDateTime but with only time and not date?

How to get the day (ex: Monday) from a time Instant in FreeMarker

Java ZonedDateTime to Instant conversion

Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist) using RabbitMQ

Parsing a Date and Time string into a ZonedDateTime object

Convert String into java.time.ZonedDateTime

Instant time from week of year

How to convert from org.joda.time.DateTime to java.time.ZonedDateTime

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive