Calculate the Julian Day for a given date using the following formula: JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9) + D + 1721013.5 + UT/24 Note that this is only valid for the year range 1900 - 2099. - Android java.util

Android examples for java.util:Day

Description

Calculate the Julian Day for a given date using the following formula: JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9) + D + 1721013.5 + UT/24 Note that this is only valid for the year range 1900 - 2099.

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 {
    /**//from  w  w  w.  j av  a  2s. com
     * Calculate the Julian Day for a given date using the following formula:
     * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
     *      + D + 1721013.5 + UT/24
     *
     * Note that this is only valid for the year range 1900 - 2099.
     */
    public static double calculateJulianDay(Date date) {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTime(date);

        double hour = cal.get(Calendar.HOUR_OF_DAY)
                + cal.get(Calendar.MINUTE) / 60.0f
                + cal.get(Calendar.SECOND) / 3600.0f;

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

        double jd = 367.0
                * year
                - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
                + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour
                / 24.0;
        return jd;
    }
}

Related Tutorials