Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Java's BST ZoneId represent?

I have stored in the DB this Time Frame: any day from 15:00 to 16:00 in LONDON (BST)

I need to execute a program IF when I receive an event is between this Time Frame.

I am running the Test now in Paris (16:22) where in London is 15:22 (so in between the Time Frame stored in the DB).

SO this is my code

// create Local Date Time from what I have stored in the DB

LocalDateTime dateTime1 = LocalDateTime.of(2017, Month.JUNE, 15, 15, 00);
LocalDateTime dateTime2 = LocalDateTime.of(2017, Month.JUNE, 15, 16, 00);

Instant now = Instant.now();

System.out.println (now.isAfter (dateTime1.atZone(ZoneId.of("BST", ZoneId.SHORT_IDS)).toInstant()));
System.out.println (now.isBefore(dateTime2.atZone(ZoneId.of("BST", ZoneId.SHORT_IDS)).toInstant()));

theoretically now (16:22 in PARIS / 15:22 in LONDON) is after dateTime1 in LONDON (15:00) and before dateTime2 (16:00) in LONDON

but I got that now is not before that dateTime2

like image 401
La Carbonell Avatar asked Jun 15 '17 14:06

La Carbonell


People also ask

What is ZoneId?

A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime .

What is the ZoneId for GMT?

What is the ZoneId for GMT? The ZoneId class is used to identify a time zone and provide the conversion rules between LocalDateTime and Instant. In terms of offset rules, ZoneId is divided into 2 types: ZoneId with a fixed time zone offset, such as "UTC+07", "GMT-05:40", "UT-03", "+05:50".

What is ZoneId systemDefault ()?

The systemDefault() method of the ZoneId class in Java is used to return the system default time-zone. Syntax: public String systemDefault() Parameters: This method does not accepts any parameters. Return Value: This method returns the zone ID.

What are the different between OffsetDateTime and ZonedDateTime?

ZonedDateTime handles a date and time with a corresponding time zone with a time zone offset from Greenwich/UTC. OffsetDateTime handles a date and time with a corresponding time zone offset from Greenwich/UTC, without a time zone ID.


1 Answers

As indicated in the javadoc of ZonedId.SHORT_IDS, “BST” is not British Summer Time but Bangladesh Standard Time (Asia/Dhaka).

You can check the value with:

System.out.println(ZoneId.of("BST", ZoneId.SHORT_IDS));

So I suggest using full time zone names to avoid any confusion:

ZoneId london = ZoneId.of("Europe/London")
like image 87
assylias Avatar answered Sep 20 '22 00:09

assylias