You could use the Java 8 Date/Time API, which was heavily influenced by the Joda Time library (and also apparently has some overlap in developer effort), but differs in some respects. And unlike Joda Time, the Java 8 Date/Time API comes native with Java.
The DateTimeFormatter class has these pattern letters:
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00
In your case, lowercase x
should give the result you want.
Example code:
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx");
ZoneId zone = ZoneId.of("UTC");
ZonedDateTime d = ZonedDateTime.now(zone);
System.out.println(d.format(f));
Output:
2020-11-03T22:31:10.928+00:00
It might be worth reading up on the package description for the Java 8 Date and Time API to understand the general philosophy of the API, which is a bit different from that of the java.util Date and Calendar objects.
In short, the main idea is that all date and time objects in this API are immutable, and that you if you want to modify or create dates, you would create other date and time objects with factory methods like of
or functions like with
that return a copy of the the datetime object but with the specified field changed.
Some important classes:
Instant
- a timestamp
LocalDate
- a date without a time, or any reference to an offset or time-zone
LocalTime
- a time without a date, or any reference to an offset or time-zone
LocalDateTime
- combines date and time, but still without any offset or time-zone
ZonedDateTime
- a "full" date-time with time-zone and resolved offset from UTC/Greenwich
ZoneId
- represents a time zone
To convert a java.util.Date object to corresponding objects in the Java 8 Date/Time API, see: Convert java.util.Date to java.time.LocalDate