0

I want to know the number of hours in a day when the DST (daylight saving time, summer time) is begins and ends.

Am willing to use java.time. Zone ID is Europe/London. The main intention is:

  • when DST begins in the spring, we will have 23 hours in one day because clocks are turned forward
  • conversely when DST ends, we will have 25 hours in one day.

I have an epoch value from which I should find the number of hours. How is it possible?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Venkat
  • 73
  • 1
  • 8
  • The answer is 23 when switching to DST and 25 when switching back. What's the remaining question here? – Henry Jul 27 '18 at 04:34
  • I have a epoch value from which i should find the number of hours. How is it possible? – Venkat Jul 27 '18 at 04:38
  • Something like this: https://stackoverflow.com/questions/36130669/how-to-find-the-duration-of-difference-between-two-dates ? – Henry Jul 27 '18 at 04:53
  • 2
    I never saw anything unclear about this question. I hope my edits helped those who did. – Ole V.V. Jul 27 '18 at 07:58

1 Answers1

2
    ZoneId zone = ZoneId.of("Europe/London");
    long epochMillis = 1_540_700_000_000L;
    LocalDate date = Instant.ofEpochMilli(epochMillis)
            .atZone(zone)
            .toLocalDate();
    int hoursInDay = (int) ChronoUnit.HOURS.between(
            date.atStartOfDay(zone),
            date.plusDays(1).atStartOfDay(zone));
    System.out.println(date + " is " + hoursInDay + " hours");

I didn’t choose the milliseconds since the epoch at random. In this case the output is:

2018-10-28 is 25 hours

Transition to standard time will happen on the last Sunday in October, this year on October 28.

The Australia/Lord_Howe time zone uses 30-minute DST transitions, so in that time zone the day would be either 23.5 hours or 24.5 hours, which in the above will be truncated to 23 and 24. But for London you should be OK until those crazy British politicians decide to do someting similar. :-)

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks for this. It really helped. :) – Venkat Jul 27 '18 at 05:50
  • Using a `LocalDate(Time)` is probably an easier way to specify the date so you know it is on a DST change date. – Andy Turner Jul 27 '18 at 07:03
  • @OleV.V. ah right. It might just be clearer to state "// this is 2018/10/28 UTC", or just say "...", rather than using a magic number. – Andy Turner Jul 27 '18 at 07:38
  • 1
    It was always my plan to do something similar once I got a chance to try my own code, @AndyTurner. Thanks for inspiring the wording I ended up with. – Ole V.V. Jul 27 '18 at 07:48