tl;dr
You asked:
set an Instant to 5:10am
OffsetDateTime // Represent a moment, a specific point on the timeline, as seen through a number of hours-minutes-seconds ahead or behind the baseline of UTC.
.now( ZoneOffset.UTC ) // Capture the current moment as seen at UTC, that is, with an offset of zero hours-minutes-seconds from UTC. Returns a `OffsetDateTime` object.
.with( LocalTime.of( 5 , 10 ) ) // Adjust from current time-of-day to this specific time-of-day. Keep the date the same, keep the offset the same. Returns a new second `OffsetDateTime` object, per immutable objects pattern.
.toInstant() // Extract an `Instant` object, representing a moment as seen strictly in UTC, always with an offset of zero.
.toString() // Generate text representing the value of this `Instant` object, in standard ISO 8601 format.
2021-02-16T05:10:00Z
Details
Let's say I want to set an Instant to 5:10am.
It sounds like you want to start with the current date as seen in UTC (an offset of zero hours-minutes-seconds), and assign a certain time-of-day.
- Represent time-of-day with
LocalTime.
- Use
OffsetDateTime first, then extract Instant.
The Instant class is the basic building-block class of java.time. It represents a moment, a specific point on the timeline, as seen in UTC (that is, with an offset of zero hours-minutes-seconds from UTC). This class by itself has a limited feature set, as it is intended to be used by more complicated classes such as OffsetDateTime & ZonedDateTime.
The OffsetDateTime class, in contrast to Instant, is more flexible, with more features. One of those features is the with method. You can pass a LocalTime object to adjust to that specific time-of-day.
Notice that java.time use immutable objects. So with method generates a new fresh OffsetDateTime object with values based on the original. The original is unchanged.
LocalTime localTime = LocalTime.of( 5 , 10 ) ; // 5:10 AM, in 24-hour clock.
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ; // Current moment as seen in UTC, an offset of zero hours-minutes-seconds.
OffsetDateTime odtAt0510 = odt.with( localTime ) ;
Instant instant = odt.toInstant() ;
See this code run live at IdeOne.com.
instant.toString(): 2021-02-16T05:10:00Z
Regarding your code:
.withHour(StdIn.readInt())
.withMinute(StdIn.readInt())
Generally it is better to first gather your inputs from user, then perform your business logic. Segregating your code will make it easier to read, test, and debug.
…
int hour = StdIn.readInt() ;
int minute = StdIn.readInt() ;
…
LocalTime localTime = LocalTime.of( hour , minute ) ;
…