Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**
     * Calculate the number of Julian Centuries from the epoch 2000.0
     * (equivalent to Julian Day 2451545.0).
     */
    public static double julianCenturies(Date date) {
        double jd = calculateJulianDay(date);
        double delta = jd - 2451545.0;
        return delta / 36525.0;
    }

    /**
     * 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;
    }
}