I am trying the format the date timestamp including a time zone with a colon. And I did several experiments to get the result. Here's what I found.
Date date = new Date();
String zonedDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
SimpleDateFormat sdf = new SimpleDateFormat(zonedDateTimeFormat);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(new Date(date.getTime())));
If I set the time zone as UTC, I will get a timestamp like this:
2020-11-03T21:14:07.449Z
But If the time zone is not UTC
Date date = new Date();
String zonedDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
SimpleDateFormat sdf = new SimpleDateFormat(zonedDateTimeFormat);
System.out.println(sdf.format(new Date(date.getTime())));
The timestamp would be like this: 2020-11-03T22:19:43.804+01:00
I am wondering if it is possible to get a timestamp within UTC time zone like: 2020-11-03T21:14:07.449+00:00 instead of ending with a uppercase Z?
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 timestampLocalDate- a date without a time, or any reference to an offset or time-zoneLocalTime- a time without a date, or any reference to an offset or time-zoneLocalDateTime- combines date and time, but still without any offset or time-zoneZonedDateTime- a "full" date-time with time-zone and resolved offset from UTC/GreenwichZoneId- 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With