commons-suncalc

A Java library for calculation of sun and moon positions and phases.

The source code can be found at GitHub and is distributed under the terms of Apache License 2.0.

Accuracy

Astronomical calculations are far more complex than throwing a few numbers into an obscure formula and then getting a fully accurate result. There is always a tradeoff between accuracy and computing time.

This library has its focus on getting acceptable results at low cost, so it can also run on mobile devices, or devices with a low computing power. The results have an accuracy of about a minute, which should be good enough for common applications (like sunrise/sunset timers), but is probably not sufficient for astronomical purposes.

Installation

commons-suncalc is available at Maven Central. Just add this snippet to your pom.xml:

<dependency>
  <groupId>org.shredzone.commons</groupId>
  <artifactId>commons-suncalc</artifactId>
  <version>2.3</version>
</dependency>

Or use this snippet in your build.gradle (e.g. in Android Studio):

dependencies {
    compile('org.shredzone.commons:commons-suncalc:2.3')
}

Quick Start

This library consists of a few models, all of them are invoked the same way:

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());

Dependencies and Requirements

commons-suncalc requires at least Java 7, but has no other dependencies. It can also be used on Android (API level 19 or higher).