1

I wanted to practice prayer times, all the codes are correct, but the result is wrong, there may be a problem in the sinuses. Sunset and sunrise times formulas and at noon the sun is the peak time formulas could be wrong.

public void onLocationChanged(Location loc) {
    GPSEnlem = loc.getLatitude();
    GPSBoylam = loc.getLongitude();

    String Text = "Bulunduğunuz konum bilgileri : \n" + "Latitud = " + loc.getLatitude() + "\nLongitud = " + loc.getLongitude();
    konumText.setText(Text);

    String pattern = "dd_MM_yyyy";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    String date = simpleDateFormat.format(new Date());

    String day = date.substring(0, 1);
    String month = date.substring(3, 4);
    String year = date.substring(6, 9);

    double dayy = Double.valueOf(day);
    double monthh = Double.valueOf(month);
    double yearr = Double.valueOf(year);

    double I = yearr;
    double J = monthh;
    double K = dayy;

    double jd = 665176 + (1461 * (I + 4800 + (J - 14) / 12)) / 4 + (367 * (J - 2 - 12 * ((J - 14) / 12))) / 12 - (3 * ((I + 4900 + (J - 14) / 12) / 100)) / 4 + K - 32075;

    double d = jd - 2451545; // jd verilen Jülyen tarihidir

    double g = 357.529 + 0.98560028 * d;
    double q = 280.459 + 0.98564736 * d;
    double L = q + 1.915 * sin(Math.toRadians(g)) + 0.020 * sin(Math.toRadians(2 * g));

    double R = 1.00014 - 0.01671 * cos(Math.toRadians(g)) - 0.00014 * cos(Math.toRadians(2 * g));
    double e = 23.439 - 0.00000036 * d;
    double RA = atan2(cos(Math.toRadians(e)) * sin(Math.toRadians(L)), cos(Math.toRadians(L))) / 15;

    double D = asin(sin(Math.toRadians(e)) * sin(Math.toRadians(L))); // Güneş'in sapması
    double EqT = q / 15 - RA; // zaman denklemi

    double Lng = 30.0;
    double Ltd = 39.0;
    double SaatDilimi = 3;
    double oglevakti = 12.0 + SaatDilimi - (Lng / 15.0) - EqT;
    double a = 0;

    double n = d;
    double Jk = n - (Lng / 360.0);
    double M = (357.5291 + 0.98560028 * Jk) % 360.0;

    double C = 1.9148 * sin(M) + 0.0200 * sin(2.0 * M) + 0.0003 * sin(3.0 * M);

    double lambda = (M + C + 180 + 102.9372) % 360.0;

    double si = asin(sin(lambda) * sin(23.4397));

    double Ta = (acos(-sin(a) - sin(Ltd) * sin(si))) / 15.0;

    double T0833 = acos(-sin(0.833) - sin(GPSEnlem) * sin(si)) / 15.0;

    double T17 = acos(Math.toRadians(-sin(17) - sin(GPSEnlem) * sin(si))) / 15.0;

    double T18 = acos(Math.toRadians(-sin(18) - sin(GPSEnlem) * sin(si))) / 15.0;

    double gunesvakti = oglevakti - T0833;
    double aksamvakti = oglevakti + T0833;
    double imsakvakti = oglevakti - T18;
    double yatsivakti = oglevakti + T17;
    double AL = 2 + abs(tan(GPSEnlem) - si);
    double A2 = 1 / AL;
    double ikindivakti = oglevakti + A2;
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • see [calculate the time when the sun is X degrees below/above the Horizon](https://stackoverflow.com/a/25722336/2521214) for some inspiration – Spektre Aug 31 '23 at 06:39
  • I strongly recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are long outdated and were always troublesome, which is why they got supplanted. Use `LocalDate` from jave.time, the modern Java date and time API. Also please learn to use a debugger, it will help you a whole lot. – Ole V.V. Aug 31 '23 at 09:18
  • You might want to look at [Time4J](https://github.com/MenoData/Time4J) which can do all this for you. – greg-449 Aug 31 '23 at 10:43

1 Answers1

2

Your code to extract year, month and day is wrong.

One thing is that substring() returns the characters from beginIndex to endIndex-1 and that means that

String day = date.substring(0, 1);

only returns the first character of the day. The correct usage would be

String day = date.substring(0, 2);
String month = date.substring(3, 5);
String year = date.substring(6, 10);

But the whole code is unnecessarily convoluted. You should ignore the java.util.Date class and instead use the better classes from the java.time package:

LocalDate today = LocalDate.now();
int day = today.getDayOfMonth();
int month = today.getMonthValue();
int year = today.getYear();
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34