SunriseSunset icon indicating copy to clipboard operation
SunriseSunset copied to clipboard

Uses previous day when setting hours, minutes, seconds and nanoseconds to 0

Open jbaptperez opened this issue 5 years ago • 0 comments

When I call SunriseSunset.getSunriseSunset method without setting the calendar hours to noon, sunrise and sunset are calculated according to the day that precedes the targetted one.

This is easy to produce by using a Calendar instance, explicitly settings the date and 0 as hours, minutes and seconds. Example using a Java 8 LocalDate conversion:

final Calendar calendar = Calendar.getInstance();
calendar.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth(), 0, 0, 0);

In the SunriseSunsetCLI class, the hour of the calendar is explicitly set to noon and this avoids the problem (leaving minutes, seconds and nanoseconds to the "now" value).

// Create a Calendar for the given date at noon.
TimeZone tz = TimeZone.getTimeZone(timeZoneString);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setTimeZone(tz);
Date date = sdf.parse(inputDayString);
Calendar day = Calendar.getInstance(tz);
day.setTime(date);
day.set(Calendar.HOUR_OF_DAY, 12);

After several tests, I have to add 1 hour to my calendar as a workaround to fix the issue, using Europe/Paris time zone (+01:00).

Is that a bug or noon (or anything else) should be explicitly set in the Calendar instance?

jbaptperez avatar Nov 06 '19 13:11 jbaptperez