Julian day of the year in Java

后端 未结 9 992
挽巷
挽巷 2020-11-30 12:12

I have seen the \"solution\" at http://www.rgagnon.com/javadetails/java-0506.html, but it doesn\'t work correctly. E.g. yesterday (June 8) should have been 159, but it said

相关标签:
9条回答
  • 2020-11-30 13:02

    I've read all the posts and something's not very clear I think.

    user912567 mentionned Jean Meeus, and he's absolutely right

    The most accurate definition I've found is given by Jean Meeus in its "Astronomical Algorithms" book (a must have, really...).

    Julian Date is a date, expressed as usual, with a year, a month and a day.

    Julian Day is a number (a real number), counted from year -4712 and is "...a continuous count of days..." (and fraction of day). A usefull time scale used for accurate astronomical calculations.

    Jean Meeus : "The Julian Day has nothing to do with the Julian calendar" ("Astronomical Algorithms", 2nd Edition, p.59)

    0 讨论(0)
  • 2020-11-30 13:03

    If you're looking for Julian Day as in the day count since 4713 BC, then you can use the following code instead:

    private static int daysSince1900(Date date) {
        Calendar c = new GregorianCalendar();
        c.setTime(date);
    
        int year = c.get(Calendar.YEAR);
        if (year < 1900 || year > 2099) {
            throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");
        }
        year -= 1900;
        int month = c.get(Calendar.MONTH) + 1;
        int days = c.get(Calendar.DAY_OF_MONTH);
    
        if (month < 3) {
            month += 12;
            year--;
        }
        int yearDays = (int) (year * 365.25);
        int monthDays = (int) ((month + 1) * 30.61);
    
        return (yearDays + monthDays + days - 63);
    }
    
    /**
     * Get day count since Monday, January 1, 4713 BC
     * https://en.wikipedia.org/wiki/Julian_day
     * @param date
     * @param time_of_day percentage past midnight, i.e. noon is 0.5
     * @param timezone in hours, i.e. IST (+05:30) is 5.5
     * @return
     */
    private static double julianDay(Date date, double time_of_day, double timezone) {
        return daysSince1900(date) + 2415018.5 + time_of_day - timezone / 24;
    }
    

    The above code is based on https://stackoverflow.com/a/9593736, and so is limited to dates between 1900 and 2099.

    0 讨论(0)
  • 2020-11-30 13:07
    DateFormat d = new SimpleDateFormat("D");
    System.out.println(d.format(date));
    
    0 讨论(0)
提交回复
热议问题