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
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.