Convert the given Julian Day to Gregorian Date (in UT time zone). - Android java.util

Android examples for java.util:Day

Description

Convert the given Julian Day to Gregorian Date (in UT time zone).

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
//package com.java2s;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**/* w ww .ja  v  a2 s.  c  o m*/
     * Convert the given Julian Day to Gregorian Date (in UT time zone).
     * Based on the formula given in the Explanitory Supplement to the
     * Astronomical Almanac, pg 604.
     */
    public static Date calculateGregorianDate(double jd) {
        int l = (int) jd + 68569;
        int n = (4 * l) / 146097;
        l = l - (146097 * n + 3) / 4;
        int i = (4000 * (l + 1)) / 1461001;
        l = l - (1461 * i) / 4 + 31;
        int j = (80 * l) / 2447;
        int d = l - (2447 * j) / 80;
        l = j / 11;
        int m = j + 2 - 12 * l;
        int y = 100 * (n - 49) + i + l;

        double fraction = jd - Math.floor(jd);
        double dHours = fraction * 24.0;
        int hours = (int) dHours;
        double dMinutes = (dHours - hours) * 60.0;
        int minutes = (int) dMinutes;
        int seconds = (int) ((dMinutes - minutes) * 60.0);

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UT"));
        cal.set(y, m - 1, d, hours + 12, minutes, seconds);
        return cal.getTime();
    }
}

Related Tutorials