Here's the conversion of the Kotlin code to Java:

package com.lb.twilight_calculator;

import android.text.format.DateUtils;

import static java.lang.Math.*;

/**
 * Imported from frameworks/base/services/core/java/com/android/server/TwilightCalculator.java
 *
 *
 * Calculates the sunrise and sunsets times for a given location.
 */
public class TwilightCalculator {
    private static final double DEGREES_TO_RADIANS = (Math.PI / 180.0);
    // element for calculating solar transit.
    private static final double J0 = 0.0009;
    // correction for civil twilight
    private static final double ALTITUDE_CORRECTION_CIVIL_TWILIGHT = -0.104719755;
    // coefficients for calculating Equation of Center.
    private static final double C1 = 0.0334196;
    private static final double C2 = 0.000349066;
    private static final double C3 = 0.000005236;
    private static final double OBLIQUITY = 0.40927971;
    // Java time on Jan 1, 2000 12:00 UTC.
    private static final long UTC_2000 = 946728000000L;

    /**
     * @param sunset  Time of sunset (civil twilight) in milliseconds or -1 in the case the day or night never ends
     * @param sunrise Time of sunrise (civil twilight) in milliseconds or -1 in the case the day or night never ends.
     */
    public static class TwilightResult {
        private long time;
        private long sunset;
        private long sunrise;
        private boolean isDay;

        public TwilightResult(long time, long sunset, long sunrise, boolean isDay) {
            this.time = time;
            this.sunset = sunset;
            this.sunrise = sunrise;
            this.isDay = isDay;
        }

        public long getTime() {
            return time;
        }

        public long getSunset() {
            return sunset;
        }

        public long getSunrise() {
            return sunrise;
        }

        public boolean isDay() {
            return isDay;
        }
    }

    /**
     * calculates the civil twilight bases on time and geo-coordinates.
     *
     * @param time      time in milliseconds.
     * @param latitude  latitude in degrees.
     * @param longitude latitude in degrees.
     */
    public static TwilightResult calculateTwilight(long time, double latitude, double longitude) {
        double daysSince2000 = (time - UTC_2000) / (double)DateUtils.DAY_IN_MILLIS;
        // mean anomaly
        double meanAnomaly = 6.240059968 + daysSince2000 * 0.01720197;
        // true anomaly
        double trueAnomaly = 
                meanAnomaly + C1 * sin(meanAnomaly) + C2 * sin((2.0 * meanAnomaly)) + C3 * sin((3.0 * meanAnomaly));
        // ecliptic longitude
        double solarLng = trueAnomaly + 1.796593063 + Math.PI;
        // solar transit in days since 2000
        double arcLongitude = -longitude / 360.0;
        double n = round((daysSince2000 - J0 - arcLongitude));
        double solarTransitJ2000 = (n + J0 + arcLongitude + 0.0053 * sin(meanAnomaly)
                + -0.0069 * sin(2.0 * solarLng));
        // declination of sun
        double solarDec = asin(sin(solarLng) * sin(OBLIQUITY));
        double latRad = latitude * DEGREES_TO_RADIANS;
        double cosHourAngle = 
                (sin(ALTITUDE_CORRECTION_CIVIL_TWILIGHT) - sin(latRad) * sin(solarDec)) / (cos(latRad) * cos(solarDec));
        // The day or night never ends for the given date and location, if this value is out of
        // range.
        if (cosHourAngle >= 1.0) {
            return new TwilightResult(time, -1, -1, false);
        } else if (cosHourAngle <= -1.0) {
            return new TwilightResult(time, -1, -1, true);
        }
        double hourAngle = (acos(cosHourAngle) / (2.0 * Math.PI));
        long sunset = round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
        long sunrise = round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
        boolean isDay = (time > (sunrise + 1) && time < sunset);
        return new TwilightResult(time, sunset, sunrise, isDay);
    }
}

Note: The conversion assumes that the necessary imports have been added.

Kotlin to Java Code Conversion: Twilight Calculator

原文地址: https://www.cveoy.top/t/topic/fADG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录