PrayerTimes { public PrayerTimes(Coordinates coordinates, DateComponents date, CalculationParameters parameters) { this.coordinates = coordinates; this.dateComponents = date; this.calculationParameters = parameters; Date tempFajr = null; Date tempSunrise = null; Date tempDhuhr = null; Date tempAsr = null; Date tempMaghrib = null; Date tempIsha = null; final Date prayerDate = CalendarUtil.resolveTime(date); Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.setTime(prayerDate); final int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); final Date tomorrowDate = CalendarUtil.add(prayerDate, 1, Calendar.DATE); final DateComponents tomorrow = DateComponents.fromUTC(tomorrowDate); SolarTime solarTime = new SolarTime(date, coordinates); TimeComponents timeComponents = TimeComponents.fromDouble(solarTime.transit); Date transit = timeComponents == null ? null : timeComponents.dateComponents(date); timeComponents = TimeComponents.fromDouble(solarTime.sunrise); Date sunriseComponents = timeComponents == null ? null : timeComponents.dateComponents(date); timeComponents = TimeComponents.fromDouble(solarTime.sunset); Date sunsetComponents = timeComponents == null ? null : timeComponents.dateComponents(date); final SolarTime tomorrowSolarTime = new SolarTime(tomorrow, coordinates); final TimeComponents tomorrowSunriseComponents = TimeComponents.fromDouble(tomorrowSolarTime.sunrise); boolean error = transit == null || sunriseComponents == null || sunsetComponents == null || tomorrowSunriseComponents == null; if (!error) { tempDhuhr = transit; tempSunrise = sunriseComponents; tempMaghrib = sunsetComponents; timeComponents = TimeComponents.fromDouble( solarTime.afternoon(parameters.madhab.getShadowLength())); if (timeComponents != null) { tempAsr = timeComponents.dateComponents(date); } final Date tomorrowSunrise = tomorrowSunriseComponents.dateComponents(tomorrow); long night = tomorrowSunrise.getTime() - sunsetComponents.getTime(); timeComponents = TimeComponents .fromDouble(solarTime.hourAngle(-parameters.fajrAngle, false)); if (timeComponents != null) { tempFajr = timeComponents.dateComponents(date); } if (parameters.method == CalculationMethod.MOON_SIGHTING_COMMITTEE && coordinates.latitude >= 55) { tempFajr = CalendarUtil.add( sunriseComponents, -1 * (int) (night / 7000), Calendar.SECOND); } final CalculationParameters.NightPortions nightPortions = parameters.nightPortions(); final Date safeFajr; if (parameters.method == CalculationMethod.MOON_SIGHTING_COMMITTEE) { safeFajr = seasonAdjustedMorningTwilight(coordinates.latitude, dayOfYear, date.year, sunriseComponents); } else { double portion = nightPortions.fajr; long nightFraction = (long) (portion * night / 1000); safeFajr = CalendarUtil.add( sunriseComponents, -1 * (int) nightFraction, Calendar.SECOND); } if (tempFajr == null || tempFajr.before(safeFajr)) { tempFajr = safeFajr; } if (parameters.ishaInterval > 0) { tempIsha = CalendarUtil.add(tempMaghrib, parameters.ishaInterval * 60, Calendar.SECOND); } else { timeComponents = TimeComponents.fromDouble( solarTime.hourAngle(-parameters.ishaAngle, true)); if (timeComponents != null) { tempIsha = timeComponents.dateComponents(date); } if (parameters.method == CalculationMethod.MOON_SIGHTING_COMMITTEE && coordinates.latitude >= 55) { long nightFraction = night / 7000; tempIsha = CalendarUtil.add(sunsetComponents, (int) nightFraction, Calendar.SECOND); } final Date safeIsha; if (parameters.method == CalculationMethod.MOON_SIGHTING_COMMITTEE) { safeIsha = PrayerTimes.seasonAdjustedEveningTwilight( coordinates.latitude, dayOfYear, date.year, sunsetComponents); } else { double portion = nightPortions.isha; long nightFraction = (long) (portion * night / 1000); safeIsha = CalendarUtil.add(sunsetComponents, (int) nightFraction, Calendar.SECOND); } if (tempIsha == null || (tempIsha.after(safeIsha))) { tempIsha = safeIsha; } } } if (error || tempAsr == null) { this.fajr = null; this.sunrise = null; this.dhuhr = null; this.asr = null; this.maghrib = null; this.isha = null; } else { this.fajr = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempFajr, parameters.adjustments.fajr, Calendar.MINUTE), parameters.methodAdjustments.fajr, Calendar.MINUTE)); this.sunrise = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempSunrise, parameters.adjustments.sunrise, Calendar.MINUTE), parameters.methodAdjustments.sunrise, Calendar.MINUTE)); this.dhuhr = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempDhuhr, parameters.adjustments.dhuhr, Calendar.MINUTE), parameters.methodAdjustments.dhuhr, Calendar.MINUTE)); this.asr = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempAsr, parameters.adjustments.asr, Calendar.MINUTE), parameters.methodAdjustments.asr, Calendar.MINUTE)); this.maghrib = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempMaghrib, parameters.adjustments.maghrib, Calendar.MINUTE), parameters.methodAdjustments.maghrib, Calendar.MINUTE)); this.isha = CalendarUtil.roundedMinute( CalendarUtil.add( CalendarUtil.add(tempIsha, parameters.adjustments.isha, Calendar.MINUTE), parameters.methodAdjustments.isha, Calendar.MINUTE)); } } PrayerTimes(Coordinates coordinates, DateComponents date, CalculationParameters parameters); Prayer currentPrayer(); Prayer currentPrayer(Date time); Prayer nextPrayer(); Prayer nextPrayer(Date time); Date timeForPrayer(Prayer prayer); final Date fajr; final Date sunrise; final Date dhuhr; final Date asr; final Date maghrib; final Date isha; }
@Test public void testPrayerTimes() { DateComponents date = new DateComponents(2015, 7, 12); CalculationParameters params = CalculationMethod.NORTH_AMERICA.getParameters(); params.madhab = Madhab.HANAFI; Coordinates coordinates = new Coordinates(35.7750, -78.6336); PrayerTimes prayerTimes = new PrayerTimes(coordinates, date, params); SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a"); formatter.setTimeZone(TimeZone.getTimeZone("America/New_York")); assertThat(formatter.format(prayerTimes.fajr)).isEqualTo("04:42 AM"); assertThat(formatter.format(prayerTimes.sunrise)).isEqualTo("06:08 AM"); assertThat(formatter.format(prayerTimes.dhuhr)).isEqualTo("01:21 PM"); assertThat(formatter.format(prayerTimes.asr)).isEqualTo("06:22 PM"); assertThat(formatter.format(prayerTimes.maghrib)).isEqualTo("08:32 PM"); assertThat(formatter.format(prayerTimes.isha)).isEqualTo("09:57 PM"); }