Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

date - Determining the next daylight saving day by providing year in java


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I want to determine the day of the next daylight saving

That is not an answerable question. Daylight savings doesn't work like that. A sensible question would be:

"What is the next day that the clocks change in the current locale" or perhaps "What is the next day that the clocks change in Amsterdam".

There is no one global 'this is the day' across the world.

SimpleDateFormat

This is obsolete old API that you really, really don't want to use to answer this question. The right way to do date stuff in java, is with the java.time package.

ZoneId ams = ZoneId.of("Europe/Amsterdam");
ZoneOffsetTransition trans = ams.getRules().nextTransition(Instant.now());
LocalDateTime date = trans.getDateTimeBefore();
System.out.println(date);

> 2021-03-28T02:00

next time amsterdam switches clocks, it'll be 2 at night, on march 28th, 2021.

Some facts:

  • Some zones don't switch at 02:00 at night, but at some other time.
  • Some zones will cease switching soon (most of europe, for example).
  • When you drive in a single straight line within a single state in the US, you can cross 8 (or possibly more, I can't recall) zones, because (some) reservations do not observe daylight savings but the rest of the state does.
  • Countries sometimes announce changes to when they will transition or if they will mere weeks in advance.
  • Sometimes, a transition doesn't change 1 hour, it changes 24 hours (such as when a peloponesian island decides they'd rather be on the other side of the international date line).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...