0

I want to determine the day of the next daylight saving but I only can determine any dates so far I m new to java

public void iGenerateDSTOfFormatDaysFromToday(String format, int delta,String date) throws Throwable {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    Date currentDate = new Date();
    String Date = dateFormat.format(addDays(currentDate,delta));
    SetToGlobal.run(Date,date);
}
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
Shaq
  • 13
  • 1
  • 6

1 Answers1

3

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).
rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
  • Pardon me, but I don't understand this sentence: _Some zones will cease switching soon (most of europe, for example)._ Do you mean they will have daylight saving all year round? – Abra Jan 22 '21 at 05:34
  • @Abra yes. They set the clock back and forwards once a year each in 2020 and probably 2021, but will stop doing that soon, EU commission adopted a resolution to get europe off of switching clocks entirely. – rzwitserloot Jan 22 '21 at 06:44
  • Also note: the changes are delivered by OS, or by the language itself, so you may have an old database (and some countries announced changes just a week before the change, OTOH in this case inhabitants of that region know about the chaos because every system has a different view). it is just the "leap second" that it is added at the same time in all UTC based times. (but also leap seconds may disappear soon). Note for EU: every country may decide which time (regular, daylight) to use [and different by zone] – Giacomo Catenazzi Jan 22 '21 at 09:29
  • @Abra As far as I have understood, EU has only decided to stop switching. Then each member state will have to decide whether to be on standard time all year or on summer time (DST) all year. As far as I know most members have not decided yet. We may be in for yet one more case of the rules being changed on a short notice. – Ole V.V. Jan 22 '21 at 09:40
  • 1
    I mentioned the EU thing to highlight that answering the question 'when is the next transition day' either [A] queries the OS or some other auto-updated DB or [B] is going to give a lot of wrong answers. – rzwitserloot Jan 22 '21 at 11:00