3

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?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Coding_Rabbit
  • 1,287
  • 3
  • 22
  • 44
  • `sdf.format(new Date(date.getTime())).replace("Z", "+00:00")` – Rob Evans Nov 03 '20 at 21:34
  • It is possible to use the [RFC 822 time zone](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#rfc822timezone). Therefore you can use the following format: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. There is only one disadvantage, the colon is missing. – flaxel Nov 03 '20 at 21:46
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 05 '20 at 03:53

1 Answers1

4

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

ahiijny
  • 351
  • 1
  • 6
  • 21