Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating sunrise and sunset with java

I search a way to calculate sunrise and -set for a given timezone aka location. I would like something similar to Zend_Date.

The idea is to have an application where you can select a location and based on that you get the actual time and also sunrise, -set time.

Cheers and thanks,
-lony

like image 405
lony Avatar asked Feb 08 '11 17:02

lony


2 Answers

Take a look at sunrisesunsetlib-java which calculates the sunrise/sunset times from GPS coordinates and a date.

like image 95
dogbane Avatar answered Sep 21 '22 18:09

dogbane


You might want to use commons-suncalc, a dependency-free library for Java ⩾7:

Date date = // date of calculation
double lat, lng = // geolocation
SunTimes times = SunTimes.compute()
            .on(date)       // set a date
            .at(lat, lng)   // set a location
            .execute();     // get the results
System.out.println("Sunrise: " + times.getRise());
System.out.println("Sunset: " + times.getSet());
like image 39
simon04 Avatar answered Sep 21 '22 18:09

simon04