tl;dr
Instead of thinking in terms of one elaborate formatting pattern for parsing, think in terms of combining parts.
Here we get the current moment as seen in UTC. Then we move to your desired time-of-day.
OffsetDateTime.now( ZoneOffset.UTC ).with( LocalTime.parse( "13:35:23" ) ).toInstant().toString()
Details
LocalTime
Parse your input appropriately.
LocalTime lt = LocalTime.parse( "13:35:23" ) ;
ZonedDateTime
Then combine with a date and time zone to determine a moment.
For any given moment, the date varies around the globe by time zone. So a time zone is crucial here.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate ld = LocalDate.now( z ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
Instant
Adjust to UTC, an offset of zero hours-minutes-seconds, by extracting a Instant
.
Instant instant = zdt.toInstant() ;
Generate your string output, in standard ISO 8691 format.
String output = instant.toString() ;
OffsetDateTime
If you want the date and your time input to be seen as being for UTC eprather than some other time zone, use ZoneOffset.UTC
constant. Use OffsetDateTime
rather than ZonedDateTime
. Use with
to use al alternate part, such as here where we substitute the current time-of-day part with your input time-of-day.
OffsetDateTime // Represent a moment as date,time, and offset -from-UTC (a number of hours-minutes-seconds).
.now( ZoneOffset.UTC ) // Capture current moment as seen in UTC.
.with(
LocalTime.parse( "13:35:23" )
)
.toInstant() // Extract the more basic `Instant` object, always in UTC by definition.
.toString() // Generate text representing the value of this date-time object. Use standard ISO 8601 format.
