tags:

views:

196

answers:

2

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 it was 245.

So, does someone have a solution in Java for getting the current date's three digit Julian day (not Julian date - I need the day this year)?

Thanks! Mark

+2  A: 
import java.util.Calendar;
// ...
final int julianDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);

Note that this doesn't take into account the "starts at noon" deal claimed by that weird site you referenced. That could be fixed by just checking the time of course.

Pointy
On my system `System.out.println(Calendar.getInstance().getClass());` prints `class java.util.GregorianCalendar` but perhaps that's the same as julian.
aioobe
The "Julian Date" or "Julian Day" has always meant to me the day of the year. I think that's true for everybody except perhaps serious Classical scholars. If somebody is looking for the date as reckoned by a pre-Gregorian calendar, my solution won't work. Note that the question includes the sentence, "I need the day this year."
Pointy
Before doing calculation do `setGregorianChange( new Date( Long.MAX_VALUE) )`. Then it will be a `Julian` day.
Alexander Pogrebnyak
I think you're all misunderstanding what "Julian Day" means in this context. It is *not* about historical calendars. The term "Julian Day" or "Julian Date" in modern data processing applications means simply the "day of year". Google it if you don't believe me.
Pointy
@Pointy No, "Julian Date" spans several years. The Julian *day of the year* is not a standard term. And Orthodox Christians care what the date is by the Julian calendar, as they don't accept the authority of the Roman Pope Gregory.
Pete Kirkham
Well "Mark", the user who posed the question, is missing in action, but seriously the Julian Day is something that became popular thirty or so years ago for storing dates in databases. That, in fact, was a big aspect of the "Y2K" problem: databases stored dates as 5-digit numbers, with 3 digits for the Julian day and 2 digits for the year. Old libraries had "toJulianDate" and "fromJulianDate" routines (of course punctuated stylistically for COBOL :-) It is my suspicion that *that* is what the original question was about.
Pointy
Note also that the original question says that 8 June 2010 should be "159", which is correct if "Julian Day" means simply, "day of year."
Pointy
Yes, I meant Julian DAY of the year (three digits, like 159 for June 8, 2010). There IS a Julian DATE, which is from some BC date and is a large number - that is NOT what I'm looking for.
Mark
sorry folks - been very busyThe answer is: Calendar today = GregorianCalendar.getInstance(); int todayJulian = today.get(GregorianCalendar.DAY_OF_YEAR);This gives the day of the year, aka Julian day (not date)
Mark
A: 

If all you want is the day-of-year, why don'you just use GregorianCalendars DAY_OF_YEAR field?

import java.util.GregorianCalendar;
public class CalTest {
    public static void main(String[] argv) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.set(GregorianCalendar.DAY_OF_MONTH, 8);
        gc.set(GregorianCalendar.MONTH, GregorianCalendar.JUNE);
        gc.set(GregorianCalendar.YEAR, 2010);
        System.out.println(gc.get(GregorianCalendar.DAY_OF_YEAR));
}

}

Alternatively, you could calculate the difference between today's Julian date and that of Jan 1st of this year. But be sure to add 1 to the result, since Jan 1st is not the zeroth day of the year:

int[] now = {2010, 6, 8};
int[] janFirst = {2010, 1, 1};
double dayOfYear = toJulian(now) - toJulian(janFirst) + 1
System.out.println(Double.valueOf(dayOfYear).intValue());
wallenborn
Thanks so much! That worked perfectly - I knew there had to be a simple answer.
Mark