-2

I need to use a date in a URL, therefore the date cannot contain any symbols like hyphen - , dash / or colon :.

I think 20220615T160543Z is the only suitable format, but the issue is that it is in UTC, but I need it in some other time zone.

Is there anything like 20220615T160543+2:00, where the +2:00 part indicates the time zone offset from UTC?

If yes, can I print it out of a ZonedDateTime object?

The object I currently have is

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(date.getStartsOn().toInstant(), timezone);

Thanks in advance!

Update:

Thanks for all the responses!

It looks like my suggested approach is not doable, because it could be like 20220615T160543-2:00 where there is a hyphen...

Apparently percentage-encoding is the only way to do it.

Thanks again!

powerseed
  • 1,090
  • 3
  • 17
  • 29
  • 2
    You do realise percent-encoding is a thing right? – Sweeper Jun 15 '22 at 16:59
  • 2
    but what about time zones like `-02:00`? – user16320675 Jun 15 '22 at 17:07
  • 1
    [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) allows leaving out hyphens and colons as in `20220615T160543+02:00`. It requires two digits hours in the offset but allows leaving out the colon there too, so the offset becomes `+0200` or `+02:00`. And yes, a `DateTimeFormatter` can produce all of that. – Ole V.V. Jun 15 '22 at 17:08
  • Thanks, @Sweeper, for pointing out the apparent [XY problem](https://en.wikipedia.org/wiki/XY_problem). And its solution. – Ole V.V. Jun 15 '22 at 17:11
  • Is there any reason you can't use an Instant? This way you can present it in any timezone you want. – BeWu Jun 15 '22 at 17:23
  • While a hyphen is allowed in a URL (also @user16320675), a `+` (plus) will be interpreted as a space, so your `20220615T160543+2:00` would not come through intact. Seems you need to go with [percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding). `URLEncoder.encode(yourZonedDateTime.toString(), StandardCharsets.UTF_8)` gives `2022-06-15T16%3A05%3A43%2B02%3A00`, which should be safe when the other party percent-decodes it at the other end. – Ole V.V. Jun 15 '22 at 17:27
  • 2
    @Ole ?? they want an output without hyphen - I do not know their requirement, what the URL is used for,... - so I asked what is expected for time zones like `-02:00` – user16320675 Jun 15 '22 at 17:31
  • 1
    Given an example of the output you'd want for noon, Eastern Daylight Time (United States). The point is, that zone has a negative offset, but you don't want to use the hyphen commonly used to represent that. What do you want? – erickson Jun 15 '22 at 19:21

2 Answers2

1

How do I print a ZonedDateTime with timezone but without hyphen?

Try it like this.

First, create a ZonedDateTime using the example in your question.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssz");
ZonedDateTime zdt = ZonedDateTime.parse("20220615T160543Z",dtf);

Then print it with a ZoneId as follows to give you your zone.

String dt = zdt.format(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssVV")
           .withZone(ZoneId.of("Asia/Qatar")));
System.out.println(dt);

prints

20220615T190543Asia/Qatar

Note: Unfortunately a small portion of the ZoneId's contain hyphens. You can see them by doing the following:

ZoneId.getAvailableZoneIds().stream()
           .filter(str->str.contains("-"))
           .forEach(System.out::println);

WJS
  • 36,363
  • 4
  • 24
  • 39
  • It’s not perfectly clear, but I read the question as one about formatting and printing rather than parsing. In any case your code is fine for what you are doing. – Ole V.V. Jun 15 '22 at 19:14
  • I figured I had to parse it to properly format it to include some aspect of a zone. Clearly someone didn't like it, lol. – WJS Jun 15 '22 at 19:36
  • 2
    Thanks for the answer. It looks like my suggested approach is not doable, because it could be like `20220615T160543-2:00` where there is a hyphen... Apparently percentage-encoding is the only way to do it. Thanks again! – powerseed Jun 17 '22 at 13:58
-1

Please use something like this:

    public class Main {
    public static void main(String[] args) throws IOException {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("US/Eastern"));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHmmssXXX");
        String formattedString = zonedDateTime.format(formatter);
        System.out.println(formattedString);
    }
}
Eskandar Abedini
  • 2,090
  • 2
  • 13
  • 2
    from question: "*... I need it in some other time zone ...*" – user16320675 Jun 15 '22 at 17:12
  • 1
    Also as an aside this conversion is wrong: `ZonedDateTime.of(localDateTime, ZoneId.of("UTC"))`. Just use one of the `ZonedDateTime.now` methods (there are three of them) and save the conversion. – Ole V.V. Jun 15 '22 at 17:13
  • 1
    You can set any time zone that you need – Eskandar Abedini Jun 15 '22 at 17:14
  • That is minus symbol for time zone! – Eskandar Abedini Jun 15 '22 at 17:29
  • 1
    This is an argument about words. When one person calls it a hyphen and another person calls it a minus, it is always the same char. And while allowed in a URL, the questioner did ask to avoid it. – Ole V.V. Jun 15 '22 at 17:32
  • *`Eskandar Abedini -That is minus symbol for time zone!`* It's both. But I doubt the URL parser will care what you call it or how important it is. It isn't wanted. – WJS Jun 15 '22 at 17:33
  • Another option for zone formatting is `yyyyMMddHmmsszzzz` that gives something like this: `20220615221005Central European Summer Time` – Eskandar Abedini Jun 15 '22 at 17:41
  • Here [Java: Adding TimeZone to DateTimeFormatter](https://stackoverflow.com/questions/49458878/java-adding-timezone-to-datetimeformatter) some formatters given, please check `yyyyMMddHmmssz` – Eskandar Abedini Jun 15 '22 at 17:50
  • *`20220615221005Central European Summer Time`* yeah, but even though the OP didn’t state that, a space poses problems in a URL too. Also it just could be that the receiver understands `+02:00` but does not understand time zone names. – Ole V.V. Jun 15 '22 at 17:55
  • 1
    Your sample has positive difference with reference time zone and other zones have negative difference. – Eskandar Abedini Jun 15 '22 at 18:03
  • Another solution is separating zone part of formatted time and formatting it manually! – Eskandar Abedini Jun 15 '22 at 18:04