tl;dr
How do I get the only Datetime information with timezone offset?
OffsetDateTime
.now()
.toString()
2022-04-13T22:47:58.123793+02:00
Or, state your desired/expected time zone explicitly rather than rely implicitly on the JVM’s current default time zone.
OffsetDateTime
.now(
ZoneId.of( "Europe/Berlin" )
)
.toString()
OffsetDateTime
You asked:
How do I get the only Datetime information with timezone offset?
To represent a date with time-of-day as seen in an offset of some number of hours-minutes-seconds from the meridian of UTC, use the OffsetDateTime class.
Calling OffsetDateTime.now() implicitly applies the offset of your JVM’s current default time zone.
OffsetDateTime odt = OffsetDateTime.now() ; // Uses current offset.
See that code run live at IdeOne.com.
2022-04-13T20:47:58.059193Z
The Z on the end means an offset of zero from UTC, and is pronounced “Zulu”.
The IdeOne.com site happens to use a default time zone of UTC itself, a.k.a. “Zulu time”. So let’s specify a time zone to see a different offset in action.
OffsetDateTime odt2 = OffsetDateTime.now( ZoneId.of( "Europe/Berlin" ) ) ; // Use the offset currently in effect for this particular time zone.
2022-04-13T22:47:58.123793+02:00
ZonedDateTime
Usually it is better to use a full time zone rather than a mere offset. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by their politicians.
ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Europe/Berlin" ) ) ;
2022-04-13T22:47:58.126001+02:00[Europe/Berlin]
But occasionally we need to deal with an offset rather than a zone. Doing SQL work with a database is one important example, as the SQL standard was written only for offsets rather than time zones.
Instant
You asked:
How can I get my expected output 2022-04-13T10:22:35.362644+02:00 using Instant?
You cannot.
The Instant class represents a moment as seen with an offset of zero hours-minutes-seconds from the meridian of UTC. An Instant object by definition is always “in UTC”, that is, carries an offset of zero.
If you have an Instant in hand, you can adjust into a time zone or offset. Rather than assume you know the correct offset, I suggest going through a time zone to let java.time look up the offset in effect at the moment.
ZoneId z = ZoneId.of( "Europe/Berlin" ) ;
ZonedDateTime zdt = myInstant.atZone( z ) ;
OffsetDateTime odt = zdt.toOffsetDateTime() ;
Tip: Be aware that politicians change the time zone rules, and therefore the offset, with surprising frequency and disturbingly little forewarning. Be sure to keep the tzdata up to date if any zone of interest to you is changing. You will find tzdata file in (a) your JVM, (b) your host operating system, and (c) perhaps in ancillary systems such as your database server.
LocalDateTime
Never use LocalDateTime class when tracking a moment, a specific point on the timeline. This class purposely lacks the context of a time zone or offset-from-UTC. An object of LocalDateTime carries only a date and a time-of-day, nothing more.
I cannot imagine any scenario where calling LocalDateTime#now or LocalDateTime.ofInstant would be the right thing to do. In doing so, you would be purposely discarding vital information (the time zone or offset).
Instantwhen that doesn't include any offset information? It seems to me that what you need isOffsetDateTime. (And ifZonedDateTimehas all the information you need, but you need a different format, then use aDateTimeFormatterrather than expecting the default string representation to be exactly what you want.)OffsetDateTimeis what you appear to need. When right now I executedOffsetDateTime.now(ZoneId.systemDefault())in German time zone, I got2022-04-13T11:34:10.078283+02:00.